All files / player-scene-model/src/browse sort.ts

94.44% Statements 34/36
87.23% Branches 41/47
100% Functions 7/7
96.42% Lines 27/28

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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71              191x     191x             48x 112x 44x 100x 100x   112x       100x 100x 67x 1x       66x   20x 20x 20x 3x       26x   5x   5x   5x   5x                       40x 14x 8x 8x   6x    
import type { LibraryChart, LibraryEntry } from '../library/entries.ts';
import type { BrowseSort } from './list-options.ts';
 
/**
 * Case-insensitive text order in the runtime's locale: `a.localeCompare(b, undefined, { sensitivity: 'base' })` as one
 * collator, so sorting thousands of bars does not set one up per comparison.
 */
const TEXT_COLLATOR = new Intl.Collator(undefined, { sensitivity: 'base' });
 
/** Folders sort before charts, charts before courses (a list never mixes them today; this keeps the order sensible). */
const KIND_RANK: Readonly<Record<LibraryEntry['kind'], number>> = { folder: 0, chart: 1, course: 2 };
 
/**
 * The bars in `sort` order ({@link BrowseSort}), as a new array. Folders sort by label under every order but `OFF` and
 * `LASTPLAY`; courses keep their order. The sort is stable: bars that compare equal keep the library's order.
 */
export function sortLibraryEntries(entries: readonly LibraryEntry[], sort: BrowseSort): LibraryEntry[] {
  if (sort === 'OFF' || sort === 'LASTPLAY') return [...entries];
  const indexed = entries.map((entry, index) => ({ entry, index }));
  indexed.sort((a, b) => {
    const cmp = compareEntries(a.entry, b.entry, sort);
    return cmp !== 0 ? cmp : a.index - b.index;
  });
  return indexed.map((item) => item.entry);
}
 
function compareEntries(a: LibraryEntry, b: LibraryEntry, sort: BrowseSort): number {
  Iif (a.kind !== b.kind) return KIND_RANK[a.kind] - KIND_RANK[b.kind];
  if (a.kind === 'folder' && b.kind === 'folder') return TEXT_COLLATOR.compare(a.folder.label, b.folder.label);
  if (a.kind === 'chart' && b.kind === 'chart') return compareCharts(a.chart, b.chart, sort);
  return 0;
}
 
function compareCharts(a: LibraryChart, b: LibraryChart, sort: BrowseSort): number {
  switch (sort) {
    case 'LEVEL': {
      const levelA = sortablePlayLevel(a.playLevel);
      const levelB = sortablePlayLevel(b.playLevel);
      if (levelA !== levelB) return levelA - levelB;
      return TEXT_COLLATOR.compare(a.title, b.title);
    }
    case 'TITLE':
    case 'CLEAR':
      return TEXT_COLLATOR.compare(a.title, b.title);
    case 'ARTIST':
      return TEXT_COLLATOR.compare(a.artist ?? '', b.artist ?? '');
    case 'GENRE':
      return TEXT_COLLATOR.compare(a.genre ?? '', b.genre ?? '');
    case 'BPM':
      return (a.bpm ?? 0) - (b.bpm ?? 0);
    case 'NOTES':
      return a.totalNotes - b.totalNotes;
    case 'OFF':
    case 'LASTPLAY':
      return 0;
  }
}
 
/**
 * A `#PLAYLEVEL` (a number, or text in some bmson files and BMS variants) as a sort key; a missing or non-numeric level
 * sorts after every other.
 */
function sortablePlayLevel(value: number | string | undefined): number {
  if (typeof value === 'number') return Number.isFinite(value) ? value : Number.POSITIVE_INFINITY;
  if (typeof value === 'string') {
    const parsed = Number.parseFloat(value);
    return Number.isFinite(parsed) ? parsed : Number.POSITIVE_INFINITY;
  }
  return Number.POSITIVE_INFINITY;
}