MCPcopy Index your code
hub / github.com/formatjs/formatjs / loadUnits

Function loadUnits

packages/intl-numberformat/scripts/extract-units.ts:32–81  ·  view source on GitHub ↗
(locale: string)

Source from the content-addressed store, hash-verified

30}
31
32async function loadUnits(locale: string): Promise<UnitDataTable> {
33 const units = (
34 (await import(`cldr-units-full/main/${locale}/units.json`, {
35 with: {type: 'json'},
36 })) as {default: typeof UnitsData}
37 ).default.main[locale as 'en'].units
38
39 invariant(
40 !!(
41 units.long.per.compoundUnitPattern &&
42 units.short.per.compoundUnitPattern &&
43 units.narrow.per.compoundUnitPattern
44 ),
45 `Missing "per" compound pattern in locale ${locale}`
46 )
47
48 const validUnits = Object.keys(units.long).filter(unit => {
49 return IsWellFormedUnitIdentifier(removeUnitNamespace(unit))
50 })
51
52 const simpleUnitEntries: [string, UnitData][] = validUnits.map(unit => {
53 if (!units.long[unit as 'digital-bit']) {
54 throw new Error(`${unit} does not have any data`)
55 }
56 return [
57 removeUnitNamespace(unit),
58 {
59 // displayName: units.long[unit as 'digital-bit'].displayName,
60 long: extractUnitPattern(units.long[unit as 'volume-gallon']),
61 short: extractUnitPattern(units.short[unit as 'volume-gallon']),
62 narrow: extractUnitPattern(units.narrow[unit as 'volume-gallon']),
63 perUnit: {
64 long: units.long[unit as 'volume-gallon'].perUnitPattern,
65 short: units.short[unit as 'volume-gallon'].perUnitPattern,
66 narrow: units.narrow[unit as 'volume-gallon'].perUnitPattern,
67 },
68 },
69 ]
70 })
71
72 const compoundUnits = {
73 per: {
74 long: units.long.per.compoundUnitPattern,
75 short: units.short.per.compoundUnitPattern,
76 narrow: units.narrow.per.compoundUnitPattern,
77 },
78 }
79
80 return {simple: fromPairs(simpleUnitEntries), compound: compoundUnits}
81}
82
83export async function generateDataForLocales(
84 locales: string[] = AVAILABLE_LOCALES.availableLocales.full

Callers

nothing calls this directly

Calls 4

removeUnitNamespaceFunction · 0.85
extractUnitPatternFunction · 0.85
invariantFunction · 0.50

Tested by

no test coverage detected