All files / chart-parser/src/core event-utils.ts

88.88% Statements 104/117
89.38% Branches 101/113
92.3% Functions 12/13
88.78% Lines 95/107

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                                              33526x 23929x   9597x                                         444833x 16921x 16921x     16921x               427912x 444833x 444833x 444833x 444833x 1703274x   1703274x 1703271x 1703271x 1703270x 851636x 851636x   851634x 519266x             851634x 851634x     519266x 427912x 7x   427905x                       29689x 29689x 340766x 340766x 340765x       29689x 29689x                 3227x 3227x 34x 34x   3193x 3193x 3193x 3193x 3193x                             340766x 340766x 340766x 340766x 340766x 340766x 1x   340765x 340765x     340765x 340765x 79173x   340765x 340765x 340765x       340766x     340766x       340766x     340766x       340766x     340766x       340766x 1x   340765x 340765x     340765x 340765x     340765x       340765x     340765x       340765x     340765x       340765x 335597x   5168x 5168x 5168x 5168x 5168x 10336x 10336x   5168x 5168x 4824x   5168x 5168x 4786x     5168x 129x       5168x 135x   5168x    
import { cloneJSONFields } from '@be-music/chart-source';
import { compareEvents } from '@be-music/chart-model';
import {
  normalizeChannel,
  normalizeObjectKey,
  type BeMusicEvent,
  type BeMusicJSON,
  type BeMusicPosition,
  type BMSObjectLineEntry,
} from '@be-music/chart-source';
import {
  normalizeASCIIBase36Code,
  normalizeASCIIBase62Code,
  normalizeFractionNumerator,
  normalizeNonNegativeInt,
  normalizePositiveInt,
} from '@be-music/shared-utils/core';
 
import { normalizeLRXEventExtensions } from './lrx-extensions.ts';
 
type MeasureLengthEntry = BeMusicJSON['measures'][number];
 
export function normalizeBmsonNoteLength(value: unknown): number | undefined {
  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
    return undefined;
  }
  return Math.floor(value);
}
 
/**
 * Builds the canonical entry for one BMS object data line (`#mmmcc:data`).
 *
 * Single implementation of the line → entry rule shared by the strict parse path and the control-flow capture path
 * (`#RANDOM` / `#IF` / `#SWITCH` bodies), so both interpret a line identically:
 *
 * - Channel `02` carries a measure-length factor — parsed as float, non-positive / non-finite values are dropped.
 * - Every other channel is a token stream — `00` is a rest, non-zero 2-char tokens become events positioned as
 *   `[tokenIndex, tokenCount]`.
 *
 * Returns `undefined` when the line contributes nothing (invalid measure length / only rests).
 */
export function buildBMSObjectLineEntry(
  measure: number,
  channel: string,
  data: string,
  base: 36 | 62 = 36,
): BMSObjectLineEntry | undefined {
  if (channel === '02') {
    const measureLength = Number.parseFloat(data);
    Iif (!Number.isFinite(measureLength) || measureLength <= 0) {
      return undefined;
    }
    return {
      measure,
      channel,
      events: [],
      measureLength,
    };
  }
 
  const normalize = base === 62 ? normalizeASCIIBase62Code : normalizeASCIIBase36Code;
  const events: Array<Omit<BeMusicEvent, 'position'> & { position: [number, number] }> = [];
  let tokenCount = 0;
  let highCode = -1;
  for (let index = 0; index < data.length; index += 1) {
    const code = data.charCodeAt(index);
    // BMS reads one contiguous run of slots. Text after whitespace takes no space in the measure.
    if (code === 0x20 || code === 0x09 || code === 0x0b || code === 0x0c) break;
    const normalizedCode = normalize(code);
    if (normalizedCode < 0) continue;
    if (highCode < 0) {
      highCode = normalizedCode;
      continue;
    }
    if (highCode !== 0x30 || normalizedCode !== 0x30) {
      events.push({
        measure,
        channel,
        position: [tokenCount, 0],
        value: String.fromCharCode(highCode, normalizedCode),
      });
    }
    tokenCount += 1;
    highCode = -1;
  }
  // The denominator counts every complete slot, including rests.
  for (const event of events) event.position[1] = tokenCount;
  if (events.length === 0) {
    return undefined;
  }
  return {
    measure,
    channel,
    events,
  };
}
 
export function sortAndNormalizeEvents(
  events: Array<BeMusicEvent | Record<string, unknown>>,
  base: 36 | 62 = 36,
  allowLongKeys = false,
): BeMusicEvent[] {
  const normalized: BeMusicEvent[] = [];
  for (const event of events) {
    const parsed = normalizeRawEvent(event, base, allowLongKeys);
    if (parsed) {
      normalized.push(parsed);
    }
  }
 
  normalized.sort(compareEvents);
  return normalized;
}
 
export function upsertMeasureLength(
  json: BeMusicJSON,
  measure: number,
  length: number,
  measureByIndex?: Map<number, MeasureLengthEntry>,
): void {
  const cached = measureByIndex?.get(measure);
  if (cached) {
    cached.length = length;
    return;
  }
  Eif (measureByIndex) {
    const created = { index: measure, length };
    json.measures.push(created);
    measureByIndex.set(measure, created);
    return;
  }
  const found = json.measures.find((item) => item.index === measure);
  if (found) {
    found.length = length;
  } else {
    json.measures.push({ index: measure, length });
  }
}
 
function normalizeRawEvent(
  event: BeMusicEvent | Record<string, unknown>,
  base: 36 | 62 = 36,
  allowLongKeys = false,
): BeMusicEvent | undefined {
  const raw = event as Record<string, unknown>;
  const measure = normalizeMeasure(raw.measure);
  const channel = normalizeEventChannel(raw.channel);
  const value = normalizeEventValue(raw.value, base, allowLongKeys);
  const position = normalizePosition(raw.position);
  if (measure === undefined || channel === undefined || value === undefined || position === undefined) {
    return undefined;
  }
  const bmson = normalizeEventBmsonExtension(raw.bmson);
  const lrx = normalizeLRXEventExtensions(raw.lrx);
  // Optional fields are assigned rather than spread, so every event shares the hidden classes the parsers' plain event
  // literals start from; spreads give each call site its own, and the extra shapes slow every event comparison.
  const normalized: BeMusicEvent = { measure, channel, position, value };
  if (typeof raw.bmsLineOrder === 'number' && Number.isSafeInteger(raw.bmsLineOrder) && raw.bmsLineOrder >= 0) {
    normalized.bmsLineOrder = raw.bmsLineOrder;
  }
  if (bmson) normalized.bmson = bmson;
  if (lrx) normalized.lrx = lrx;
  return normalized;
}
 
function normalizeMeasure(value: unknown): number | undefined {
  Iif (typeof value !== 'number' || !Number.isFinite(value)) {
    return undefined;
  }
  return normalizeNonNegativeInt(value);
}
 
function normalizeEventChannel(value: unknown): string | undefined {
  Iif (typeof value !== 'string') {
    return undefined;
  }
  return normalizeChannel(value);
}
 
function normalizeEventValue(value: unknown, base: 36 | 62 = 36, allowLongKeys = false): string | undefined {
  Iif (typeof value !== 'string') {
    return undefined;
  }
  return normalizeObjectKey(value, base, allowLongKeys);
}
 
function normalizePosition(position: unknown): BeMusicPosition | undefined {
  if (!Array.isArray(position) || position.length < 2) {
    return undefined;
  }
  const denominator = normalizePositionDenominator(position[1]);
  Iif (denominator === undefined) {
    return undefined;
  }
  const numerator = normalizePositionNumerator(position[0], denominator);
  Iif (numerator === undefined) {
    return undefined;
  }
  return [numerator, denominator];
}
 
function normalizePositionDenominator(value: unknown): number | undefined {
  Iif (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
    return undefined;
  }
  return normalizePositiveInt(value);
}
 
function normalizePositionNumerator(value: unknown, denominator: number): number | undefined {
  Iif (typeof value !== 'number' || !Number.isFinite(value)) {
    return undefined;
  }
  return normalizeFractionNumerator(value, denominator);
}
 
function normalizeEventBmsonExtension(value: unknown): BeMusicEvent['bmson'] | undefined {
  if (!value || typeof value !== 'object') {
    return undefined;
  }
  const raw = value as Record<string, unknown>;
  const extension: NonNullable<BeMusicEvent['bmson']> = {};
  const extraFields = cloneJSONFields(raw.extraFields);
  if (extraFields) extension.extraFields = extraFields;
  for (const key of ['sampleOffsetSeconds', 'sampleDurationSeconds'] as const) {
    const seconds = raw[key];
    if (typeof seconds === 'number' && Number.isFinite(seconds) && seconds >= 0) extension[key] = seconds;
  }
  const length = normalizeBmsonNoteLength(raw.l);
  if (length !== undefined) {
    extension.l = length;
  }
  if (typeof raw.up === 'boolean') extension.up = raw.up;
  if (typeof raw.c === 'boolean') {
    extension.c = raw.c;
  }
  // beatoraja bmson extension — per-note long-note type (1: LN, 2: CN, 3: HCN).
  if (raw.t === 1 || raw.t === 2 || raw.t === 3) {
    extension.t = raw.t;
  }
  // Per-mine gauge damage — sourced from bmson `mine_channels[].notes[].damage`. 0 is a valid value (the chart authored
  // a no-damage decoration mine), so the guard checks `Number.isFinite` rather than truthiness.
  if (typeof raw.damage === 'number' && Number.isFinite(raw.damage) && raw.damage >= 0) {
    extension.damage = raw.damage;
  }
  return Object.keys(extension).length > 0 ? extension : undefined;
}