🧹 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 { Decimal, Dimension, NumeralRepr, Unit, Value } from "./types";
import { Lex, lexLine } from "./lexer"; import { Lex, lexLine } from "./lexer";
import * as vocab from "./vocab"; 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"; import { EXTRA_UNITS, CRYPTO } from "./extraunits";
/** Operators for percentage calculations. */ /** 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 }); 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 function registerPrefixesForUnit(
// area/volume templates need to build "square meter" / "cubic foot" etc. reg: Registry,
function registerUnits(reg: Registry): LengthPhrase[] { d: UnitData,
const lengthPhrases: LengthPhrase[] = []; unit: Unit,
/** lowercased composed symbol -> unit; null = ambiguous (mm vs Mm) */ variants: string[],
const lenientSym = new Map<string, Unit | null>(); symbol: string | undefined,
format: string,
for (const d of UNIT_DATA) { symbolsAll: string[],
const variants = vocab.variants(d.category, `${d.id}.variants`); lenientSym: Map<string, Unit | null>,
const format = vocab.entryEn(d.category, `${d.id}.format`) ?? d.format ?? d.id; lengthPhrases: LengthPhrase[]
const symbol = vocab.entryEn(d.category, `${d.id}.symbol`); ): void {
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) {
let prefixes = d.prefixes === "si" ? SI_PREFIXES : [...DATA_SI_PREFIXES, ...IEC_PREFIXES]; let prefixes = d.prefixes === "si" ? SI_PREFIXES : [...DATA_SI_PREFIXES, ...IEC_PREFIXES];
if (d.id === "second") { if (d.id === "second") {
// only sub-second prefixes are useful; "as" (attosecond) would // only sub-second prefixes are useful; "as" (attosecond) would
@ -344,6 +330,34 @@ function registerUnits(reg: Registry): LengthPhrase[] {
} }
} }
} }
// 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);
}
} }
for (const [lower, unit] of lenientSym) { for (const [lower, unit] of lenientSym) {