All files / chart-parser/src/core lrx-extensions.ts

63.07% Statements 41/65
50.84% Branches 30/59
50% Functions 3/6
63.46% Lines 33/52

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112    442x                       442x 442x       340767x 40x 40x 40x 400x 400x   40x 120x   40x 40x 40x   40x     442x                           442x                   62x 1x 1x 1x 12x 12x   1x 5x 5x   1x 2x   1x             1x     442x     442x   442x                                            
import type { LRXBGACue, LRXEventExtensions, LRXExtensions } from '@be-music/chart-source';
 
const EVENT_NUMBERS = [
  'length',
  'volume',
  'pan',
  'frequency',
  'attackMs',
  'releaseMs',
  'offsetMs',
  'lengthMs',
  'damage',
  'recover',
] as const satisfies ReadonlyArray<keyof LRXEventExtensions>;
const EVENT_FLAGS = ['chain', 'reverse', 'keyVolume'] as const satisfies ReadonlyArray<keyof LRXEventExtensions>;
const EVENT_STRINGS = ['lane'] as const satisfies ReadonlyArray<keyof LRXEventExtensions>;
 
/** The LRX values of an event read back from Be-Music JSON: known fields of the right type, nothing else. */
export function normalizeLRXEventExtensions(value: unknown): LRXEventExtensions | undefined {
  if (!value || typeof value !== 'object') return undefined;
  const raw = value as Record<string, unknown>;
  const extension: LRXEventExtensions = {};
  for (const key of EVENT_NUMBERS) {
    const field = raw[key];
    if (typeof field === 'number' && Number.isFinite(field)) extension[key] = field;
  }
  for (const key of EVENT_FLAGS) {
    if (raw[key] === true) extension[key] = true;
  }
  for (const key of EVENT_STRINGS) {
    const field = raw[key];
    if (typeof field === 'string' && field.length > 0) extension[key] = field;
  }
  return Object.keys(extension).length > 0 ? extension : undefined;
}
 
const CHART_NUMBERS = [
  'laneType',
  'category',
  'level',
  'bmsLevel',
  'minBPM',
  'maxBPM',
  'baseBPM',
  'startBPM',
  'phase',
  'unlock',
  'songId',
  'endTick',
] as const satisfies ReadonlyArray<keyof LRXExtensions>;
const CHART_STRINGS = [
  'sequenceTitle',
  'sequenceArtist',
  'movieArtist',
  'labelImage',
  'moviePath',
] as const satisfies ReadonlyArray<keyof LRXExtensions>;
 
/** The chart-level LRX block read back from Be-Music JSON. */
export function normalizeLRXExtensions(value: unknown): LRXExtensions | undefined {
  if (!value || typeof value !== 'object') return undefined;
  const raw = value as Record<string, unknown>;
  const lrx: LRXExtensions = {};
  for (const key of CHART_NUMBERS) {
    const field = raw[key];
    if (typeof field === 'number' && Number.isFinite(field)) lrx[key] = field;
  }
  for (const key of CHART_STRINGS) {
    const field = raw[key];
    if (typeof field === 'string') lrx[key] = field;
  }
  Eif (Array.isArray(raw.barTicks)) {
    lrx.barTicks = raw.barTicks.filter((tick): tick is number => typeof tick === 'number' && Number.isFinite(tick));
  }
  Iif (Array.isArray(raw.bga)) {
    const bga = raw.bga.flatMap((value) => {
      const cue = normalizeLRXBGACue(value);
      return cue === undefined ? [] : [cue];
    });
    if (bga.length > 0) lrx.bga = bga;
  }
  return lrx;
}
 
const BGA_NUMBERS = ['endTick', 'blend', 'rotation', 'center', 'accel'] as const satisfies ReadonlyArray<
  keyof LRXBGACue
>;
const BGA_QUADS = ['src', 'dst', 'color'] as const satisfies ReadonlyArray<keyof LRXBGACue>;
 
const isNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value);
 
/** One LRX BGA cue read back from Be-Music JSON: its slot, tick, image and key, and the known fields of the right type. */
function normalizeLRXBGACue(value: unknown): LRXBGACue | undefined {
  if (!value || typeof value !== 'object') return undefined;
  const raw = value as Record<string, unknown>;
  const { slot, tick, image, key } = raw;
  if (!isNumber(slot) || slot < 0 || slot > 5 || !isNumber(tick) || !isNumber(image) || typeof key !== 'string')
    return undefined;
  const cue: LRXBGACue = { slot, tick, image, key };
  if (raw.chain === true) cue.chain = true;
  for (const name of BGA_NUMBERS) {
    const field = raw[name];
    if (isNumber(field)) cue[name] = field;
  }
  for (const name of BGA_QUADS) {
    const field = raw[name];
    if (Array.isArray(field) && field.length === 4 && field.every(isNumber))
      cue[name] = [field[0], field[1], field[2], field[3]];
  }
  return cue;
}