All files / skin-beatoraja/src/elements fields.ts

100% Statements 40/40
100% Branches 40/40
100% Functions 10/10
100% Lines 30/30

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            768x 768x       13x 13x 2x 2x 5x   2x       20x 20x               70x       75x 8x 3x       53x 53x     51x 47x       131x 131x 25x 25x       25x 24x 23x       11x       41x 81x 81x   29x    
import { isBeatorajaLuaFunctionValue, type BeatorajaLuaFunctionValue } from '../lua.ts';
import type { BeatorajaSkinSourceId } from '../types.ts';
 
export type BeatorajaNumericPropertyRef = number | BeatorajaLuaFunctionValue;
 
export function numberField(record: Readonly<Record<string, unknown>>, key: string, fallback: number): number {
  const v = record[key];
  return typeof v === 'number' && Number.isFinite(v) ? v : fallback;
}
 
export function numberArrayField(record: Readonly<Record<string, unknown>>, key: string): ReadonlyArray<number> {
  const v = record[key];
  if (!Array.isArray(v)) return [];
  const out: number[] = [];
  for (const x of v) {
    if (typeof x === 'number' && Number.isFinite(x)) out.push(x);
  }
  return out;
}
 
export function stringField(record: Readonly<Record<string, unknown>>, key: string, fallback: string): string {
  const v = record[key];
  return typeof v === 'string' ? v : fallback;
}
 
export function sourceIdField(
  record: Readonly<Record<string, unknown>>,
  key: string,
  fallback: BeatorajaSkinSourceId,
): BeatorajaSkinSourceId {
  return sourceIdValueField(record[key], fallback);
}
 
export function sourceIdValueField(value: unknown, fallback: BeatorajaSkinSourceId): BeatorajaSkinSourceId {
  if (typeof value === 'number' && Number.isFinite(value)) return value;
  if (typeof value === 'string' && value.length > 0) return value;
  return fallback;
}
 
export function boolField(record: Readonly<Record<string, unknown>>, key: string, fallback: boolean): boolean {
  const v = record[key];
  if (typeof v === 'boolean') return v;
  // Lua serializes booleans into JS booleans, but JSON skin authors sometimes write `1` / `0`
  // for boolean fields. Honor both shapes — `1` truthy, `0` falsy, anything else falls back.
  if (typeof v === 'number') return v !== 0;
  return fallback;
}
 
export function positiveIntField(record: Readonly<Record<string, unknown>>, key: string, fallback: number): number {
  const v = record[key];
  if (typeof v !== 'number' || !Number.isFinite(v)) return fallback;
  const truncated = Math.trunc(v);
  return truncated >= 1 ? truncated : fallback;
}
 
export function integerPropertyField(value: unknown): BeatorajaNumericPropertyRef | undefined {
  if (typeof value === 'number' && Number.isFinite(value)) return value;
  if (isBeatorajaLuaFunctionValue(value)) return value;
  return undefined;
}
 
export function floatPropertyField(value: unknown): BeatorajaNumericPropertyRef | undefined {
  return integerPropertyField(value);
}
 
export function pickHex(record: Readonly<Record<string, unknown>>, keys: ReadonlyArray<string>): string {
  for (const key of keys) {
    const v = record[key];
    if (typeof v === 'string' && v.length > 0) return v;
  }
  return '';
}