🧹 Refactor overly long function registerUnits

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-09-04 07:13:26 +00:00
parent 37cfd2144b
commit d20d76aa35

View file

@ -5,7 +5,7 @@
import { Decimal, Dimension, NumeralRepr, Unit, Value } from "./types";
import { Lex, lexLine } from "./lexer";
import * as vocab from "./vocab";
import { UNIT_DATA, SI_PREFIXES, DATA_SI_PREFIXES, IEC_PREFIXES, SCALE_DATA } from "./unitdata";
import { UNIT_DATA, SI_PREFIXES, DATA_SI_PREFIXES, IEC_PREFIXES, SCALE_DATA, UnitData } from "./unitdata";
import { EXTRA_UNITS, CRYPTO } from "./extraunits";
/** Operators for percentage calculations. */
@ -270,31 +270,17 @@ function registerCoreVocab(reg: Registry): void {
for (const f of BUILTIN_FUNCS) reg.addPhrase(f, { t: "func", name: f }, { caseSensitive: false });
}
// UNIT_DATA + SI/IEC prefixes; returns the length-dimension phrases the
// area/volume templates need to build "square meter" / "cubic foot" etc.
function registerUnits(reg: Registry): LengthPhrase[] {
const lengthPhrases: LengthPhrase[] = [];
/** lowercased composed symbol -> unit; null = ambiguous (mm vs Mm) */
const lenientSym = new Map<string, Unit | null>();
for (const d of UNIT_DATA) {
const variants = vocab.variants(d.category, `${d.id}.variants`);
const format = vocab.entryEn(d.category, `${d.id}.format`) ?? d.format ?? d.id;
const symbol = vocab.entryEn(d.category, `${d.id}.symbol`);
const symbolsAll = vocab.entriesAll(d.category, `${d.id}.symbol`);
const unit: Unit = {
id: d.id,
dimension: d.dimension,
ratio: new Decimal(d.ratio),
...(d.offset ? { offset: new Decimal(d.offset) } : {}),
format,
};
addUnitPhrases(reg, variants, unit);
if (d.dimension === "length") {
for (const v of variants) lengthPhrases.push({ phrase: v, unit });
}
if (d.prefixes) {
function registerPrefixesForUnit(
reg: Registry,
d: UnitData,
unit: Unit,
variants: string[],
symbol: string | undefined,
format: string,
symbolsAll: string[],
lenientSym: Map<string, Unit | null>,
lengthPhrases: LengthPhrase[]
): void {
let prefixes = d.prefixes === "si" ? SI_PREFIXES : [...DATA_SI_PREFIXES, ...IEC_PREFIXES];
if (d.id === "second") {
// only sub-second prefixes are useful; "as" (attosecond) would
@ -343,6 +329,34 @@ function registerUnits(reg: Registry): LengthPhrase[] {
for (const pw of pWords) for (const wv of wordVariants) lengthPhrases.push({ phrase: pw + wv, unit: pUnit, caseSensitive: false });
}
}
}
// UNIT_DATA + SI/IEC prefixes; returns the length-dimension phrases the
// area/volume templates need to build "square meter" / "cubic foot" etc.
function registerUnits(reg: Registry): LengthPhrase[] {
const lengthPhrases: LengthPhrase[] = [];
/** lowercased composed symbol -> unit; null = ambiguous (mm vs Mm) */
const lenientSym = new Map<string, Unit | null>();
for (const d of UNIT_DATA) {
const variants = vocab.variants(d.category, `${d.id}.variants`);
const format = vocab.entryEn(d.category, `${d.id}.format`) ?? d.format ?? d.id;
const symbol = vocab.entryEn(d.category, `${d.id}.symbol`);
const symbolsAll = vocab.entriesAll(d.category, `${d.id}.symbol`);
const unit: Unit = {
id: d.id,
dimension: d.dimension,
ratio: new Decimal(d.ratio),
...(d.offset ? { offset: new Decimal(d.offset) } : {}),
format,
};
addUnitPhrases(reg, variants, unit);
if (d.dimension === "length") {
for (const v of variants) lengthPhrases.push({ phrase: v, unit });
}
if (d.prefixes) {
registerPrefixesForUnit(reg, d, unit, variants, symbol, format, symbolsAll, lenientSym, lengthPhrases);
}
}