All files / app-play/src/embed chart-match.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24        24x                       9x 9x 8x 15x 15x      
import type { LibraryChart } from '@be-music/player-scene-model';
 
/** A chart path as compared: `/`-separated, without a leading `./` or `/`, lowercased. */
function normalizeChartPath(path: string): string {
  return path
    .replaceAll('\\', '/')
    .replace(/^(?:\.\/|\/)+/u, '')
    .toLowerCase();
}
 
/**
 * The chart `?chart=` names in a track: the first whose path inside the archive is `query` or ends with `/` and `query`,
 * ignoring case and `\` / `/`, so a file name (`codebmp.bms`) or a longer path (`robita/codebmp.bms`) picks it.
 * `undefined` when none matches or the query is empty.
 */
export function findEmbedChart(charts: readonly LibraryChart[], query: string): LibraryChart | undefined {
  const wanted = normalizeChartPath(query.trim());
  if (wanted === '') return undefined;
  return charts.find((chart) => {
    const path = normalizeChartPath(chart.chartPath);
    return path === wanted || path.endsWith(`/${wanted}`);
  });
}