@10x-media/fields 0.1.0-beta.1 → 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +120 -0
- package/README.md +7 -3
- package/dist/exports/client.d.ts +10 -1
- package/dist/exports/client.js +9 -1
- package/dist/exports/color-utils.d.ts +2 -1
- package/dist/exports/color-utils.js +2 -1
- package/dist/exports/color.d.ts +2 -1
- package/dist/exports/color.js +2 -1
- package/dist/exports/encrypted.d.ts +7 -4
- package/dist/exports/encrypted.js +6 -3
- package/dist/exports/measurement-utils.d.ts +9 -0
- package/dist/exports/measurement-utils.js +8 -0
- package/dist/exports/measurement.d.ts +11 -0
- package/dist/exports/measurement.js +10 -0
- package/dist/exports/phone-utils.d.ts +4 -0
- package/dist/exports/phone-utils.js +4 -0
- package/dist/exports/phone.d.ts +6 -0
- package/dist/exports/phone.js +3 -0
- package/dist/exports/rsc.d.ts +5 -1
- package/dist/exports/rsc.js +5 -1
- package/dist/exports/types.d.ts +3 -2
- package/dist/fields/color/client/ColorField.d.ts +2 -0
- package/dist/fields/color/client/ColorField.js +35 -293
- package/dist/fields/color/client/ColorField.js.map +1 -1
- package/dist/fields/color/client/ColorInput.d.ts +44 -0
- package/dist/fields/color/client/ColorInput.js +374 -0
- package/dist/fields/color/client/ColorInput.js.map +1 -0
- package/dist/fields/color/client/ColorPickerPanel.d.ts +3 -1
- package/dist/fields/color/client/ColorPickerPanel.js +29 -4
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -1
- package/dist/fields/color/client/colorField.css +30 -0
- package/dist/fields/color/colorField.js +36 -8
- package/dist/fields/color/colorField.js.map +1 -1
- package/dist/fields/color/presetChip.js +10 -9
- package/dist/fields/color/presetChip.js.map +1 -1
- package/dist/fields/color/presetReference.d.ts +13 -0
- package/dist/fields/color/presetReference.js +94 -0
- package/dist/fields/color/presetReference.js.map +1 -0
- package/dist/fields/color/server/ColorCell.js +7 -1
- package/dist/fields/color/server/ColorCell.js.map +1 -1
- package/dist/fields/color/server/colorCell.css +5 -0
- package/dist/fields/color/valueIssue.js +23 -0
- package/dist/fields/color/valueIssue.js.map +1 -0
- package/dist/fields/encrypted/boot.js +2 -1
- package/dist/fields/encrypted/boot.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedInput.js +4 -1
- package/dist/fields/encrypted/components/EncryptedInput.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedTextarea.js +6 -2
- package/dist/fields/encrypted/components/EncryptedTextarea.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedCell.d.ts +15 -6
- package/dist/fields/encrypted/components/ProtectedCell.js +37 -8
- package/dist/fields/encrypted/components/ProtectedCell.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedField.css +134 -39
- package/dist/fields/encrypted/components/ProtectedField.d.ts +3 -0
- package/dist/fields/encrypted/components/ProtectedField.js +14 -0
- package/dist/fields/encrypted/components/ProtectedField.js.map +1 -1
- package/dist/fields/encrypted/components/WriteOnlyActions.js +65 -0
- package/dist/fields/encrypted/components/WriteOnlyActions.js.map +1 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js +286 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js.map +1 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js +69 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js.map +1 -0
- package/dist/fields/encrypted/crypto/wire.d.ts +10 -1
- package/dist/fields/encrypted/crypto/wire.js +1 -1
- package/dist/fields/encrypted/encryptedField.js +63 -6
- package/dist/fields/encrypted/encryptedField.js.map +1 -1
- package/dist/fields/encrypted/generateSecret.d.ts +10 -0
- package/dist/fields/encrypted/generateSecret.js +41 -0
- package/dist/fields/encrypted/generateSecret.js.map +1 -0
- package/dist/fields/encrypted/hint.js +67 -0
- package/dist/fields/encrypted/hint.js.map +1 -0
- package/dist/fields/encrypted/hooks.js +59 -31
- package/dist/fields/encrypted/hooks.js.map +1 -1
- package/dist/fields/encrypted/queryRewrite.d.ts +10 -2
- package/dist/fields/encrypted/queryRewrite.js +34 -8
- package/dist/fields/encrypted/queryRewrite.js.map +1 -1
- package/dist/fields/encrypted/rawRead.d.ts +24 -0
- package/dist/fields/encrypted/rawRead.js +40 -0
- package/dist/fields/encrypted/rawRead.js.map +1 -0
- package/dist/fields/encrypted/scan.js +21 -4
- package/dist/fields/encrypted/scan.js.map +1 -1
- package/dist/fields/encrypted/types.d.ts +67 -2
- package/dist/fields/encrypted/types.js +8 -1
- package/dist/fields/encrypted/types.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptAllData.js +2 -2
- package/dist/fields/encrypted/utils/decryptAllData.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptFieldValue.d.ts +30 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js +56 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js.map +1 -0
- package/dist/fields/encrypted/utils/readEncryptedField.d.ts +51 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js +96 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -1
- package/dist/fields/icon/client/icon-field.css +4 -2
- package/dist/fields/icon/codegen/emit.js +2 -2
- package/dist/fields/icon/codegen/emit.js.map +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/shared/iconLabel.js +1 -1
- package/dist/fields/icon/shared/iconLabel.js.map +1 -1
- package/dist/fields/measurement/client/MeasurementCell.d.ts +18 -0
- package/dist/fields/measurement/client/MeasurementCell.js +47 -0
- package/dist/fields/measurement/client/MeasurementCell.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementField.d.ts +13 -0
- package/dist/fields/measurement/client/MeasurementField.js +70 -0
- package/dist/fields/measurement/client/MeasurementField.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementInput.d.ts +42 -0
- package/dist/fields/measurement/client/MeasurementInput.js +327 -0
- package/dist/fields/measurement/client/MeasurementInput.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.d.ts +24 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js +61 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js.map +1 -0
- package/dist/fields/measurement/client/editModel.js +124 -0
- package/dist/fields/measurement/client/editModel.js.map +1 -0
- package/dist/fields/measurement/client/measurementCell.css +4 -0
- package/dist/fields/measurement/client/measurementField.css +215 -0
- package/dist/fields/measurement/client/useMeasurementModel.d.ts +19 -0
- package/dist/fields/measurement/client/useMeasurementModel.js +85 -0
- package/dist/fields/measurement/client/useMeasurementModel.js.map +1 -0
- package/dist/fields/measurement/engine/convert.d.ts +8 -0
- package/dist/fields/measurement/engine/convert.js +21 -0
- package/dist/fields/measurement/engine/convert.js.map +1 -0
- package/dist/fields/measurement/engine/format.d.ts +37 -0
- package/dist/fields/measurement/engine/format.js +156 -0
- package/dist/fields/measurement/engine/format.js.map +1 -0
- package/dist/fields/measurement/engine/locale.d.ts +25 -0
- package/dist/fields/measurement/engine/locale.js +79 -0
- package/dist/fields/measurement/engine/locale.js.map +1 -0
- package/dist/fields/measurement/engine/precision.d.ts +34 -0
- package/dist/fields/measurement/engine/precision.js +63 -0
- package/dist/fields/measurement/engine/precision.js.map +1 -0
- package/dist/fields/measurement/engine/registry.d.ts +55 -0
- package/dist/fields/measurement/engine/registry.js +117 -0
- package/dist/fields/measurement/engine/registry.js.map +1 -0
- package/dist/fields/measurement/engine/units.d.ts +178 -0
- package/dist/fields/measurement/engine/units.js +198 -0
- package/dist/fields/measurement/engine/units.js.map +1 -0
- package/dist/fields/measurement/measurementField.d.ts +17 -0
- package/dist/fields/measurement/measurementField.js +139 -0
- package/dist/fields/measurement/measurementField.js.map +1 -0
- package/dist/fields/measurement/options.d.ts +76 -0
- package/dist/fields/measurement/options.js +9 -0
- package/dist/fields/measurement/options.js.map +1 -0
- package/dist/fields/measurement/presets.d.ts +117 -0
- package/dist/fields/measurement/presets.js +136 -0
- package/dist/fields/measurement/presets.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementCellServer.d.ts +21 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js +53 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.d.ts +16 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js +45 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js.map +1 -0
- package/dist/fields/measurement/server/getUserUnits.js +39 -0
- package/dist/fields/measurement/server/getUserUnits.js.map +1 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js +24 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js.map +1 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js +26 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/client/editModel.js +86 -0
- package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
- package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
- package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
- package/dist/fields/phoneNumber/engine/countries.js +68 -0
- package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
- package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
- package/dist/fields/phoneNumber/engine/draft.js +72 -0
- package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
- package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
- package/dist/fields/phoneNumber/engine/metadata.js +32 -0
- package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
- package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
- package/dist/fields/phoneNumber/engine/phone.js +59 -0
- package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
- package/dist/fields/phoneNumber/options.d.ts +72 -0
- package/dist/fields/phoneNumber/options.js +37 -0
- package/dist/fields/phoneNumber/options.js.map +1 -0
- package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
- package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
- package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/dist/translations/ar.js +62 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +19 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +19 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +62 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +62 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +62 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +19 -1
- package/dist/translations/keys.js +20 -2
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +62 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +62 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +62 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +62 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +62 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/types.d.ts +41 -1
- package/dist/utils/resolveStaticLabel.js +13 -0
- package/dist/utils/resolveStaticLabel.js.map +1 -0
- package/package.json +28 -6
- package/dist/fields/icon/shared/resolveStaticLabel.js +0 -16
- package/dist/fields/icon/shared/resolveStaticLabel.js.map +0 -1
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//#region src/fields/measurement/engine/locale.ts
|
|
2
|
+
/** Final fallback in the resolution chain: locale-system default per built-in dimension. */
|
|
3
|
+
const DIMENSION_LOCALE_DEFAULTS = {
|
|
4
|
+
mass: {
|
|
5
|
+
metric: "kg",
|
|
6
|
+
uk: "lb",
|
|
7
|
+
us: "lb"
|
|
8
|
+
},
|
|
9
|
+
length: {
|
|
10
|
+
metric: "cm",
|
|
11
|
+
uk: "in",
|
|
12
|
+
us: "in"
|
|
13
|
+
},
|
|
14
|
+
volume: {
|
|
15
|
+
metric: "l",
|
|
16
|
+
uk: "l",
|
|
17
|
+
us: "fl-oz"
|
|
18
|
+
},
|
|
19
|
+
temperature: {
|
|
20
|
+
metric: "c",
|
|
21
|
+
uk: "c",
|
|
22
|
+
us: "f"
|
|
23
|
+
},
|
|
24
|
+
speed: {
|
|
25
|
+
metric: "km/h",
|
|
26
|
+
uk: "mph",
|
|
27
|
+
us: "mph"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* CLDR supplemental measurementData, complete: world default metric, US system
|
|
32
|
+
* for US and Liberia, UK system for Great Britain and Myanmar. No shipped Intl
|
|
33
|
+
* API exposes this, so the five-entry table is vendored.
|
|
34
|
+
*/
|
|
35
|
+
const US_REGIONS = new Set(["US", "LR"]);
|
|
36
|
+
const UK_REGIONS = new Set(["GB", "MM"]);
|
|
37
|
+
const systemForLocale = (locale) => {
|
|
38
|
+
let region;
|
|
39
|
+
try {
|
|
40
|
+
const parsed = new Intl.Locale(locale);
|
|
41
|
+
region = parsed.region ?? parsed.maximize().region;
|
|
42
|
+
} catch {
|
|
43
|
+
return "metric";
|
|
44
|
+
}
|
|
45
|
+
if (region && US_REGIONS.has(region)) return "us";
|
|
46
|
+
if (region && UK_REGIONS.has(region)) return "uk";
|
|
47
|
+
return "metric";
|
|
48
|
+
};
|
|
49
|
+
/** Widened for lookup by any field dimension, including custom dimensions the table has no entry for. */
|
|
50
|
+
const dimensionLocaleDefaults = DIMENSION_LOCALE_DEFAULTS;
|
|
51
|
+
/** Locale step of the resolution chain: field-declared defaults first, then the per-dimension table. */
|
|
52
|
+
const localeCandidatesFor = (args) => [args.localeDefaults?.[args.system], dimensionLocaleDefaults[args.dimension]?.[args.system]].filter((unit) => typeof unit === "string");
|
|
53
|
+
/**
|
|
54
|
+
* Display-unit resolution chain, in order: user preference, field fallback, plugin
|
|
55
|
+
* registry default, then the locale steps (field locale defaults, then the per-dimension
|
|
56
|
+
* table). First candidate the field actually offers wins; the field's first unit is the
|
|
57
|
+
* floor. Pure and admin-free so both the client field and measurement-utils can use it.
|
|
58
|
+
*/
|
|
59
|
+
const resolveDisplayUnit = (args) => {
|
|
60
|
+
const localeCandidates = args.system ? localeCandidatesFor({
|
|
61
|
+
dimension: args.dimension,
|
|
62
|
+
localeDefaults: args.localeDefaults,
|
|
63
|
+
system: args.system
|
|
64
|
+
}) : [];
|
|
65
|
+
const candidates = [
|
|
66
|
+
args.preferenceUnit,
|
|
67
|
+
args.fallbackUnit,
|
|
68
|
+
args.registryDefault,
|
|
69
|
+
...localeCandidates
|
|
70
|
+
];
|
|
71
|
+
for (const candidate of candidates) if (candidate && args.units.includes(candidate)) return candidate;
|
|
72
|
+
const first = args.units[0];
|
|
73
|
+
if (!first) throw new Error("resolveDisplayUnit: empty units list");
|
|
74
|
+
return first;
|
|
75
|
+
};
|
|
76
|
+
//#endregion
|
|
77
|
+
export { DIMENSION_LOCALE_DEFAULTS, resolveDisplayUnit, systemForLocale };
|
|
78
|
+
|
|
79
|
+
//# sourceMappingURL=locale.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locale.js","names":[],"sources":["../../../../src/fields/measurement/engine/locale.ts"],"sourcesContent":["import type { CoreDimension, MeasurementUnitId, UnitId } from './units'\n\nexport type MeasurementSystem = 'metric' | 'uk' | 'us'\n\n/** Final fallback in the resolution chain: locale-system default per built-in dimension. */\nexport const DIMENSION_LOCALE_DEFAULTS: Record<CoreDimension, Record<MeasurementSystem, UnitId>> = {\n\tmass: { metric: 'kg', uk: 'lb', us: 'lb' },\n\tlength: { metric: 'cm', uk: 'in', us: 'in' },\n\tvolume: { metric: 'l', uk: 'l', us: 'fl-oz' },\n\ttemperature: { metric: 'c', uk: 'c', us: 'f' },\n\tspeed: { metric: 'km/h', uk: 'mph', us: 'mph' },\n}\n\n/**\n * CLDR supplemental measurementData, complete: world default metric, US system\n * for US and Liberia, UK system for Great Britain and Myanmar. No shipped Intl\n * API exposes this, so the five-entry table is vendored.\n */\nconst US_REGIONS = new Set(['US', 'LR'])\nconst UK_REGIONS = new Set(['GB', 'MM'])\n\nexport const systemForLocale = (locale: string): MeasurementSystem => {\n\tlet region: string | undefined\n\ttry {\n\t\tconst parsed = new Intl.Locale(locale)\n\t\tregion = parsed.region ?? parsed.maximize().region\n\t} catch {\n\t\treturn 'metric'\n\t}\n\tif (region && US_REGIONS.has(region)) return 'us'\n\tif (region && UK_REGIONS.has(region)) return 'uk'\n\treturn 'metric'\n}\n\n/** Widened for lookup by any field dimension, including custom dimensions the table has no entry for. */\nconst dimensionLocaleDefaults: Partial<\n\tRecord<string, Record<MeasurementSystem, MeasurementUnitId>>\n> = DIMENSION_LOCALE_DEFAULTS\n\n/** Locale step of the resolution chain: field-declared defaults first, then the per-dimension table. */\nconst localeCandidatesFor = (args: {\n\tdimension: string\n\tsystem: MeasurementSystem\n\tlocaleDefaults?: Partial<Record<MeasurementSystem, MeasurementUnitId>>\n}): MeasurementUnitId[] =>\n\t[\n\t\targs.localeDefaults?.[args.system],\n\t\tdimensionLocaleDefaults[args.dimension]?.[args.system],\n\t].filter((unit): unit is MeasurementUnitId => typeof unit === 'string')\n\n/**\n * Display-unit resolution chain, in order: user preference, field fallback, plugin\n * registry default, then the locale steps (field locale defaults, then the per-dimension\n * table). First candidate the field actually offers wins; the field's first unit is the\n * floor. Pure and admin-free so both the client field and measurement-utils can use it.\n */\nexport const resolveDisplayUnit = (args: {\n\tdimension: string\n\tpreferenceUnit?: MeasurementUnitId | null\n\tfallbackUnit?: MeasurementUnitId\n\tregistryDefault?: MeasurementUnitId\n\t/** Null until the browser locale is read post-hydration, which skips both locale steps. */\n\tsystem?: MeasurementSystem | null\n\tlocaleDefaults?: Partial<Record<MeasurementSystem, MeasurementUnitId>>\n\tunits: readonly MeasurementUnitId[]\n}): MeasurementUnitId => {\n\tconst localeCandidates = args.system\n\t\t? localeCandidatesFor({\n\t\t\t\tdimension: args.dimension,\n\t\t\t\tlocaleDefaults: args.localeDefaults,\n\t\t\t\tsystem: args.system,\n\t\t\t})\n\t\t: []\n\tconst candidates = [\n\t\targs.preferenceUnit,\n\t\targs.fallbackUnit,\n\t\targs.registryDefault,\n\t\t...localeCandidates,\n\t]\n\tfor (const candidate of candidates) {\n\t\tif (candidate && args.units.includes(candidate)) return candidate\n\t}\n\tconst first = args.units[0]\n\tif (!first) throw new Error('resolveDisplayUnit: empty units list')\n\treturn first\n}\n"],"mappings":";;AAKA,MAAa,4BAAsF;CAClG,MAAM;EAAE,QAAQ;EAAM,IAAI;EAAM,IAAI;CAAK;CACzC,QAAQ;EAAE,QAAQ;EAAM,IAAI;EAAM,IAAI;CAAK;CAC3C,QAAQ;EAAE,QAAQ;EAAK,IAAI;EAAK,IAAI;CAAQ;CAC5C,aAAa;EAAE,QAAQ;EAAK,IAAI;EAAK,IAAI;CAAI;CAC7C,OAAO;EAAE,QAAQ;EAAQ,IAAI;EAAO,IAAI;CAAM;AAC/C;;;;;;AAOA,MAAM,aAAa,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC;AACvC,MAAM,aAAa,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC;AAEvC,MAAa,mBAAmB,WAAsC;CACrE,IAAI;CACJ,IAAI;EACH,MAAM,SAAS,IAAI,KAAK,OAAO,MAAM;EACrC,SAAS,OAAO,UAAU,OAAO,SAAS,EAAE;CAC7C,QAAQ;EACP,OAAO;CACR;CACA,IAAI,UAAU,WAAW,IAAI,MAAM,GAAG,OAAO;CAC7C,IAAI,UAAU,WAAW,IAAI,MAAM,GAAG,OAAO;CAC7C,OAAO;AACR;;AAGA,MAAM,0BAEF;;AAGJ,MAAM,uBAAuB,SAK5B,CACC,KAAK,iBAAiB,KAAK,SAC3B,wBAAwB,KAAK,aAAa,KAAK,OAChD,EAAE,QAAQ,SAAoC,OAAO,SAAS,QAAQ;;;;;;;AAQvE,MAAa,sBAAsB,SASV;CACxB,MAAM,mBAAmB,KAAK,SAC3B,oBAAoB;EACpB,WAAW,KAAK;EAChB,gBAAgB,KAAK;EACrB,QAAQ,KAAK;CACd,CAAC,IACA,CAAC;CACJ,MAAM,aAAa;EAClB,KAAK;EACL,KAAK;EACL,KAAK;EACL,GAAG;CACJ;CACA,KAAK,MAAM,aAAa,YACvB,IAAI,aAAa,KAAK,MAAM,SAAS,SAAS,GAAG,OAAO;CAEzD,MAAM,QAAQ,KAAK,MAAM;CACzB,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,sCAAsC;CAClE,OAAO;AACR"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/fields/measurement/engine/precision.d.ts
|
|
2
|
+
type PrecisionMode = 'readable' | 'exact';
|
|
3
|
+
/**
|
|
4
|
+
* A precision layer as authored by a plugin or a field: every knob optional, so
|
|
5
|
+
* `resolvePrecision` can tell "not set" apart from an explicit choice while merging
|
|
6
|
+
* layers. `display` is unit-id keyed; a narrower per-dimension key union comes from
|
|
7
|
+
* the factory's option type, not from this engine-level shape.
|
|
8
|
+
*/
|
|
9
|
+
type MeasurementPrecision = {
|
|
10
|
+
mode?: PrecisionMode; /** Fraction digits kept in the storage unit, applied to every write. */
|
|
11
|
+
storage?: number; /** Entry-unit typing behavior: 'quantize' rounds as you type, 'free' keeps exact input. */
|
|
12
|
+
entry?: 'quantize' | 'free'; /** Draft-repaint policy after a save/refresh. */
|
|
13
|
+
draft?: 'display' | 'faithful';
|
|
14
|
+
display?: Partial<Record<string, number>>;
|
|
15
|
+
};
|
|
16
|
+
/** A `MeasurementPrecision` with every knob but `display` filled in. */
|
|
17
|
+
type ResolvedPrecision = {
|
|
18
|
+
mode: PrecisionMode;
|
|
19
|
+
storage: number;
|
|
20
|
+
entry: 'quantize' | 'free';
|
|
21
|
+
draft: 'display' | 'faithful';
|
|
22
|
+
display?: Partial<Record<string, number>>;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Merges precision layers low-to-high priority: pass `[pluginDefault, fieldOption]`
|
|
26
|
+
* so a field's own choice always wins. A bare mode string expands to its preset
|
|
27
|
+
* (readable = quantize entry + display drafts, exact = free entry + faithful
|
|
28
|
+
* drafts); an explicit entry/draft/storage/display knob on any layer overrides
|
|
29
|
+
* whatever the final mode's preset would otherwise imply for that knob.
|
|
30
|
+
*/
|
|
31
|
+
declare const resolvePrecision: (layers: Array<MeasurementPrecision | PrecisionMode | undefined>) => ResolvedPrecision;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { MeasurementPrecision, PrecisionMode, ResolvedPrecision, resolvePrecision };
|
|
34
|
+
//# sourceMappingURL=precision.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import "./units.js";
|
|
2
|
+
//#region src/fields/measurement/engine/precision.ts
|
|
3
|
+
const MODE_PRESETS = {
|
|
4
|
+
readable: {
|
|
5
|
+
entry: "quantize",
|
|
6
|
+
draft: "display"
|
|
7
|
+
},
|
|
8
|
+
exact: {
|
|
9
|
+
entry: "free",
|
|
10
|
+
draft: "faithful"
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const DEFAULT_MODE = "readable";
|
|
14
|
+
/**
|
|
15
|
+
* Merges precision layers low-to-high priority: pass `[pluginDefault, fieldOption]`
|
|
16
|
+
* so a field's own choice always wins. A bare mode string expands to its preset
|
|
17
|
+
* (readable = quantize entry + display drafts, exact = free entry + faithful
|
|
18
|
+
* drafts); an explicit entry/draft/storage/display knob on any layer overrides
|
|
19
|
+
* whatever the final mode's preset would otherwise imply for that knob.
|
|
20
|
+
*/
|
|
21
|
+
const resolvePrecision = (layers) => {
|
|
22
|
+
let mode = DEFAULT_MODE;
|
|
23
|
+
let storage = 6;
|
|
24
|
+
let entry;
|
|
25
|
+
let draft;
|
|
26
|
+
let display;
|
|
27
|
+
for (const raw of layers) {
|
|
28
|
+
if (raw === void 0) continue;
|
|
29
|
+
const layer = typeof raw === "string" ? { mode: raw } : raw;
|
|
30
|
+
if (layer.mode !== void 0) mode = layer.mode;
|
|
31
|
+
if (layer.storage !== void 0) storage = layer.storage;
|
|
32
|
+
if (layer.entry !== void 0) entry = layer.entry;
|
|
33
|
+
if (layer.draft !== void 0) draft = layer.draft;
|
|
34
|
+
if (layer.display !== void 0) display = {
|
|
35
|
+
...display,
|
|
36
|
+
...layer.display
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (!Object.hasOwn(MODE_PRESETS, mode)) throw new Error(`resolvePrecision: mode "${mode}" must be "readable" or "exact"`);
|
|
40
|
+
if (!Number.isInteger(storage) || storage < 0 || storage > 12) throw new Error(`resolvePrecision: storage must be an integer between 0 and 12, got ${storage}`);
|
|
41
|
+
const preset = MODE_PRESETS[mode];
|
|
42
|
+
return {
|
|
43
|
+
mode,
|
|
44
|
+
storage,
|
|
45
|
+
entry: entry ?? preset.entry,
|
|
46
|
+
draft: draft ?? preset.draft,
|
|
47
|
+
...display !== void 0 ? { display } : {}
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Exact mode reads back at full storage fidelity instead of display digits, for
|
|
52
|
+
* both cells and bound messages, so the two always agree. An explicit per-unit
|
|
53
|
+
* override for the display unit itself still wins, since the caller asked for it
|
|
54
|
+
* by name; every other unit's override (if any) passes through untouched.
|
|
55
|
+
*/
|
|
56
|
+
const exactModePrecisionOverride = (resolved, displayUnit) => resolved.mode === "exact" ? {
|
|
57
|
+
...resolved.display,
|
|
58
|
+
[displayUnit]: resolved.display?.[displayUnit] ?? resolved.storage
|
|
59
|
+
} : resolved.display;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { exactModePrecisionOverride, resolvePrecision };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=precision.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"precision.js","names":[],"sources":["../../../../src/fields/measurement/engine/precision.ts"],"sourcesContent":["import { STORAGE_FRACTION_DIGITS } from './units'\n\nexport type PrecisionMode = 'readable' | 'exact'\n\n/**\n * A precision layer as authored by a plugin or a field: every knob optional, so\n * `resolvePrecision` can tell \"not set\" apart from an explicit choice while merging\n * layers. `display` is unit-id keyed; a narrower per-dimension key union comes from\n * the factory's option type, not from this engine-level shape.\n */\nexport type MeasurementPrecision = {\n\tmode?: PrecisionMode\n\t/** Fraction digits kept in the storage unit, applied to every write. */\n\tstorage?: number\n\t/** Entry-unit typing behavior: 'quantize' rounds as you type, 'free' keeps exact input. */\n\tentry?: 'quantize' | 'free'\n\t/** Draft-repaint policy after a save/refresh. */\n\tdraft?: 'display' | 'faithful'\n\tdisplay?: Partial<Record<string, number>>\n}\n\n/** A `MeasurementPrecision` with every knob but `display` filled in. */\nexport type ResolvedPrecision = {\n\tmode: PrecisionMode\n\tstorage: number\n\tentry: 'quantize' | 'free'\n\tdraft: 'display' | 'faithful'\n\tdisplay?: Partial<Record<string, number>>\n}\n\nconst MODE_PRESETS: Record<PrecisionMode, Pick<ResolvedPrecision, 'draft' | 'entry'>> = {\n\treadable: { entry: 'quantize', draft: 'display' },\n\texact: { entry: 'free', draft: 'faithful' },\n}\n\nconst DEFAULT_MODE: PrecisionMode = 'readable'\n\n/**\n * Merges precision layers low-to-high priority: pass `[pluginDefault, fieldOption]`\n * so a field's own choice always wins. A bare mode string expands to its preset\n * (readable = quantize entry + display drafts, exact = free entry + faithful\n * drafts); an explicit entry/draft/storage/display knob on any layer overrides\n * whatever the final mode's preset would otherwise imply for that knob.\n */\nexport const resolvePrecision = (\n\tlayers: Array<MeasurementPrecision | PrecisionMode | undefined>\n): ResolvedPrecision => {\n\tlet mode: PrecisionMode = DEFAULT_MODE\n\tlet storage: number = STORAGE_FRACTION_DIGITS\n\tlet entry: ResolvedPrecision['entry'] | undefined\n\tlet draft: ResolvedPrecision['draft'] | undefined\n\tlet display: Partial<Record<string, number>> | undefined\n\n\tfor (const raw of layers) {\n\t\tif (raw === undefined) continue\n\t\tconst layer = typeof raw === 'string' ? { mode: raw } : raw\n\t\tif (layer.mode !== undefined) mode = layer.mode\n\t\tif (layer.storage !== undefined) storage = layer.storage\n\t\tif (layer.entry !== undefined) entry = layer.entry\n\t\tif (layer.draft !== undefined) draft = layer.draft\n\t\tif (layer.display !== undefined) display = { ...display, ...layer.display }\n\t}\n\n\tif (!Object.hasOwn(MODE_PRESETS, mode)) {\n\t\tthrow new Error(`resolvePrecision: mode \"${mode}\" must be \"readable\" or \"exact\"`)\n\t}\n\tif (!Number.isInteger(storage) || storage < 0 || storage > 12) {\n\t\tthrow new Error(`resolvePrecision: storage must be an integer between 0 and 12, got ${storage}`)\n\t}\n\n\tconst preset = MODE_PRESETS[mode]\n\treturn {\n\t\tmode,\n\t\tstorage,\n\t\tentry: entry ?? preset.entry,\n\t\tdraft: draft ?? preset.draft,\n\t\t...(display !== undefined ? { display } : {}),\n\t}\n}\n\n/**\n * Exact mode reads back at full storage fidelity instead of display digits, for\n * both cells and bound messages, so the two always agree. An explicit per-unit\n * override for the display unit itself still wins, since the caller asked for it\n * by name; every other unit's override (if any) passes through untouched.\n */\nexport const exactModePrecisionOverride = (\n\tresolved: ResolvedPrecision,\n\tdisplayUnit: string\n): Partial<Record<string, number>> | undefined =>\n\tresolved.mode === 'exact'\n\t\t? { ...resolved.display, [displayUnit]: resolved.display?.[displayUnit] ?? resolved.storage }\n\t\t: resolved.display\n"],"mappings":";;AA8BA,MAAM,eAAkF;CACvF,UAAU;EAAE,OAAO;EAAY,OAAO;CAAU;CAChD,OAAO;EAAE,OAAO;EAAQ,OAAO;CAAW;AAC3C;AAEA,MAAM,eAA8B;;;;;;;;AASpC,MAAa,oBACZ,WACuB;CACvB,IAAI,OAAsB;CAC1B,IAAI,UAAA;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CAEJ,KAAK,MAAM,OAAO,QAAQ;EACzB,IAAI,QAAQ,KAAA,GAAW;EACvB,MAAM,QAAQ,OAAO,QAAQ,WAAW,EAAE,MAAM,IAAI,IAAI;EACxD,IAAI,MAAM,SAAS,KAAA,GAAW,OAAO,MAAM;EAC3C,IAAI,MAAM,YAAY,KAAA,GAAW,UAAU,MAAM;EACjD,IAAI,MAAM,UAAU,KAAA,GAAW,QAAQ,MAAM;EAC7C,IAAI,MAAM,UAAU,KAAA,GAAW,QAAQ,MAAM;EAC7C,IAAI,MAAM,YAAY,KAAA,GAAW,UAAU;GAAE,GAAG;GAAS,GAAG,MAAM;EAAQ;CAC3E;CAEA,IAAI,CAAC,OAAO,OAAO,cAAc,IAAI,GACpC,MAAM,IAAI,MAAM,2BAA2B,KAAK,gCAAgC;CAEjF,IAAI,CAAC,OAAO,UAAU,OAAO,KAAK,UAAU,KAAK,UAAU,IAC1D,MAAM,IAAI,MAAM,sEAAsE,SAAS;CAGhG,MAAM,SAAS,aAAa;CAC5B,OAAO;EACN;EACA;EACA,OAAO,SAAS,OAAO;EACvB,OAAO,SAAS,OAAO;EACvB,GAAI,YAAY,KAAA,IAAY,EAAE,QAAQ,IAAI,CAAC;CAC5C;AACD;;;;;;;AAQA,MAAa,8BACZ,UACA,gBAEA,SAAS,SAAS,UACf;CAAE,GAAG,SAAS;EAAU,cAAc,SAAS,UAAU,gBAAgB,SAAS;AAAQ,IAC1F,SAAS"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { CompoundUnitId } from "./units.js";
|
|
2
|
+
import { LocalizedLabel } from "./format.js";
|
|
3
|
+
|
|
4
|
+
//#region src/fields/measurement/engine/registry.d.ts
|
|
5
|
+
type CustomUnitDef = {
|
|
6
|
+
dimension: string;
|
|
7
|
+
factor: number;
|
|
8
|
+
offset?: number; /** ECMA-402 unit identifier, or null when there is none: formatting falls back to plain decimal + shortLabel. */
|
|
9
|
+
intlUnit: string | null; /** A plain string applies to every locale; a map resolves exact locale, then language prefix, then 'en'. */
|
|
10
|
+
shortLabel: LocalizedLabel; /** Long-form label, same resolution as shortLabel. Defaults to shortLabel when omitted. */
|
|
11
|
+
longLabel?: LocalizedLabel; /** Display fraction digits for this unit. Defaults to 2 when omitted. */
|
|
12
|
+
precision?: number;
|
|
13
|
+
};
|
|
14
|
+
type CustomDimensionDef = {
|
|
15
|
+
/** Must name one of `custom.units` for this dimension, with factor 1 and no offset. */canonicalUnit: string;
|
|
16
|
+
};
|
|
17
|
+
type MeasurementCustomConfig = {
|
|
18
|
+
units?: Record<string, CustomUnitDef>;
|
|
19
|
+
dimensions?: Record<string, CustomDimensionDef>;
|
|
20
|
+
};
|
|
21
|
+
type EngineFormatMeasurementOptions = {
|
|
22
|
+
storageUnit: string;
|
|
23
|
+
displayUnit: string;
|
|
24
|
+
locale: string;
|
|
25
|
+
unitDisplay?: 'long' | 'narrow' | 'short';
|
|
26
|
+
precision?: Partial<Record<string, number>>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Builds a measurement engine over the built-in units merged with a serializable custom
|
|
30
|
+
* config. Compound units (ft-in, st-lb) are always built-in: custom units are scalar-only,
|
|
31
|
+
* so decompose/compose run on the built-in table, bridged for a custom storage unit.
|
|
32
|
+
*/
|
|
33
|
+
declare const createEngine: (custom?: MeasurementCustomConfig) => {
|
|
34
|
+
compose: (parts: {
|
|
35
|
+
major: number;
|
|
36
|
+
minor: number;
|
|
37
|
+
}, compound: CompoundUnitId, to: string) => number;
|
|
38
|
+
convert: (value: number, from: string, to: string) => number;
|
|
39
|
+
decompose: (value: number, from: string, compound: CompoundUnitId, minorDigits?: number) => {
|
|
40
|
+
major: number;
|
|
41
|
+
minor: number;
|
|
42
|
+
};
|
|
43
|
+
dimensionOf: (unit: string) => string;
|
|
44
|
+
formatMeasurement: (value: number, opts: EngineFormatMeasurementOptions) => string;
|
|
45
|
+
isCompoundUnit: (unit: string) => unit is CompoundUnitId;
|
|
46
|
+
isScalarUnit: (unit: string) => boolean;
|
|
47
|
+
precisionFor: (unit: string, overrides?: Partial<Record<string, number>>) => number;
|
|
48
|
+
unitLabel: (unit: string, locale: string, style: "long" | "short") => string;
|
|
49
|
+
unitsOfDimension: (dimension: string) => string[];
|
|
50
|
+
};
|
|
51
|
+
type MeasurementEngine = ReturnType<typeof createEngine>;
|
|
52
|
+
declare const defaultEngine: MeasurementEngine;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { CustomDimensionDef, CustomUnitDef, EngineFormatMeasurementOptions, MeasurementCustomConfig, MeasurementEngine, createEngine, defaultEngine };
|
|
55
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { COMPOUNDS, COMPOUND_MINOR_PRECISION, DISPLAY_PRECISION, UNITS, isScalarUnit, unitsOfDimension } from "./units.js";
|
|
2
|
+
import { DIMENSION_LOCALE_DEFAULTS } from "./locale.js";
|
|
3
|
+
import { convertUnit, roundTo } from "./convert.js";
|
|
4
|
+
import { compose, decompose, formatMeasurement, formatScalarValue, scalarLongLabelFor, scalarShortLabelFor, unitLabel } from "./format.js";
|
|
5
|
+
//#region src/fields/measurement/engine/registry.ts
|
|
6
|
+
const CORE_DIMENSIONS = new Set(Object.keys(DIMENSION_LOCALE_DEFAULTS));
|
|
7
|
+
const ALLOWED_CUSTOM_CONFIG_KEYS = new Set(["units", "dimensions"]);
|
|
8
|
+
const isCompoundUnitId = (unit) => Object.hasOwn(COMPOUNDS, unit);
|
|
9
|
+
const mergeUnits = (custom) => {
|
|
10
|
+
if (custom !== void 0) {
|
|
11
|
+
for (const key of Object.keys(custom)) if (!ALLOWED_CUSTOM_CONFIG_KEYS.has(key)) throw new Error(`Custom config key "${key}" is not recognized; only "units" and "dimensions" are allowed`);
|
|
12
|
+
}
|
|
13
|
+
const customUnits = custom?.units ?? {};
|
|
14
|
+
const customDimensions = custom?.dimensions ?? {};
|
|
15
|
+
for (const dimensionId of Object.keys(customDimensions)) if (CORE_DIMENSIONS.has(dimensionId)) throw new Error(`Custom dimension "${dimensionId}" collides with a built-in dimension`);
|
|
16
|
+
const units = {};
|
|
17
|
+
for (const id of Object.keys(UNITS)) units[id] = {
|
|
18
|
+
...UNITS[id],
|
|
19
|
+
precision: DISPLAY_PRECISION[id]
|
|
20
|
+
};
|
|
21
|
+
for (const [id, def] of Object.entries(customUnits)) {
|
|
22
|
+
if (Object.hasOwn(UNITS, id) || Object.hasOwn(COMPOUNDS, id)) throw new Error(`Custom unit "${id}" collides with a built-in unit id`);
|
|
23
|
+
if (Object.hasOwn(units, id)) throw new Error(`Custom unit "${id}" collides with another custom unit id`);
|
|
24
|
+
if (!CORE_DIMENSIONS.has(def.dimension) && !Object.hasOwn(customDimensions, def.dimension)) throw new Error(`Custom unit "${id}" references unknown dimension "${def.dimension}": declare it in custom.dimensions or use a built-in dimension`);
|
|
25
|
+
units[id] = {
|
|
26
|
+
dimension: def.dimension,
|
|
27
|
+
factor: def.factor,
|
|
28
|
+
offset: def.offset,
|
|
29
|
+
intlUnit: def.intlUnit,
|
|
30
|
+
shortLabel: def.shortLabel,
|
|
31
|
+
longLabel: def.longLabel,
|
|
32
|
+
precision: def.precision ?? 2
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
for (const [dimensionId, def] of Object.entries(customDimensions)) {
|
|
36
|
+
const canonical = units[def.canonicalUnit];
|
|
37
|
+
if (!canonical || canonical.dimension !== dimensionId) throw new Error(`Custom dimension "${dimensionId}" canonicalUnit "${def.canonicalUnit}" must be a custom unit declared for dimension "${dimensionId}"`);
|
|
38
|
+
if (canonical.factor !== 1 || typeof canonical.offset !== "undefined") throw new Error(`Custom dimension "${dimensionId}" canonicalUnit "${def.canonicalUnit}" must have factor 1 and no offset`);
|
|
39
|
+
}
|
|
40
|
+
return units;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Builds a measurement engine over the built-in units merged with a serializable custom
|
|
44
|
+
* config. Compound units (ft-in, st-lb) are always built-in: custom units are scalar-only,
|
|
45
|
+
* so decompose/compose run on the built-in table, bridged for a custom storage unit.
|
|
46
|
+
*/
|
|
47
|
+
const createEngine = (custom) => {
|
|
48
|
+
const units = mergeUnits(custom);
|
|
49
|
+
const getUnit = (unit) => {
|
|
50
|
+
const def = units[unit];
|
|
51
|
+
if (!def) throw new Error(`Unknown unit "${unit}"`);
|
|
52
|
+
return def;
|
|
53
|
+
};
|
|
54
|
+
const convert = (value, from, to) => convertUnit(units, value, from, to);
|
|
55
|
+
const isScalarUnit$1 = (unit) => Object.hasOwn(units, unit);
|
|
56
|
+
const decompose$1 = (value, from, compound, minorDigits) => {
|
|
57
|
+
if (isScalarUnit(from)) return decompose(value, from, compound, minorDigits);
|
|
58
|
+
const bridge = COMPOUNDS[compound].major;
|
|
59
|
+
return decompose(convert(value, from, bridge), bridge, compound, minorDigits);
|
|
60
|
+
};
|
|
61
|
+
const compose$1 = (parts, compound, to) => {
|
|
62
|
+
if (isScalarUnit(to)) return compose(parts, compound, to);
|
|
63
|
+
const bridge = COMPOUNDS[compound].major;
|
|
64
|
+
return convert(compose(parts, compound, bridge), bridge, to);
|
|
65
|
+
};
|
|
66
|
+
const dimensionOf = (unit) => isCompoundUnitId(unit) ? UNITS[COMPOUNDS[unit].major].dimension : getUnit(unit).dimension;
|
|
67
|
+
const unitsOfDimension$1 = (dimension) => {
|
|
68
|
+
const customIds = Object.entries(custom?.units ?? {}).filter(([, def]) => def.dimension === dimension).map(([id]) => id);
|
|
69
|
+
return [...unitsOfDimension(dimension), ...customIds];
|
|
70
|
+
};
|
|
71
|
+
const precisionFor = (unit, overrides) => {
|
|
72
|
+
const override = overrides?.[unit];
|
|
73
|
+
if (typeof override === "number") return override;
|
|
74
|
+
return isCompoundUnitId(unit) ? COMPOUND_MINOR_PRECISION[unit] : getUnit(unit).precision;
|
|
75
|
+
};
|
|
76
|
+
const unitLabel$1 = (unit, locale, style) => {
|
|
77
|
+
if (isCompoundUnitId(unit)) return unitLabel(unit, locale, style);
|
|
78
|
+
const def = getUnit(unit);
|
|
79
|
+
return style === "short" ? scalarShortLabelFor(def, locale) : scalarLongLabelFor(def, locale);
|
|
80
|
+
};
|
|
81
|
+
const formatMeasurement$1 = (value, opts) => {
|
|
82
|
+
const { displayUnit, locale, precision, storageUnit, unitDisplay = "short" } = opts;
|
|
83
|
+
if (isCompoundUnitId(displayUnit)) {
|
|
84
|
+
const bridge = COMPOUNDS[displayUnit].major;
|
|
85
|
+
const builtInStorage = isScalarUnit(storageUnit);
|
|
86
|
+
return formatMeasurement(builtInStorage ? value : convert(value, storageUnit, bridge), {
|
|
87
|
+
...opts,
|
|
88
|
+
storageUnit: builtInStorage ? storageUnit : bridge,
|
|
89
|
+
displayUnit
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const digits = precisionFor(displayUnit, precision);
|
|
93
|
+
return formatScalarValue(roundTo(convert(value, storageUnit, displayUnit), digits), {
|
|
94
|
+
digits,
|
|
95
|
+
locale,
|
|
96
|
+
unit: getUnit(displayUnit),
|
|
97
|
+
unitDisplay
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
compose: compose$1,
|
|
102
|
+
convert,
|
|
103
|
+
decompose: decompose$1,
|
|
104
|
+
dimensionOf,
|
|
105
|
+
formatMeasurement: formatMeasurement$1,
|
|
106
|
+
isCompoundUnit: isCompoundUnitId,
|
|
107
|
+
isScalarUnit: isScalarUnit$1,
|
|
108
|
+
precisionFor,
|
|
109
|
+
unitLabel: unitLabel$1,
|
|
110
|
+
unitsOfDimension: unitsOfDimension$1
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
const defaultEngine = createEngine();
|
|
114
|
+
//#endregion
|
|
115
|
+
export { createEngine, defaultEngine };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","names":["isScalarUnit","decompose","isBuiltInScalarUnit","decomposeBuiltIn","compose","composeBuiltIn","unitsOfDimension","unitsOfDimensionBuiltIn","unitLabel","unitLabelBuiltIn","formatMeasurement","formatMeasurementBuiltIn"],"sources":["../../../../src/fields/measurement/engine/registry.ts"],"sourcesContent":["import { convertUnit, roundTo } from './convert'\nimport {\n\tcompose as composeBuiltIn,\n\tdecompose as decomposeBuiltIn,\n\ttype FormattableUnit,\n\tformatMeasurement as formatMeasurementBuiltIn,\n\tformatScalarValue,\n\ttype LocalizedLabel,\n\tscalarLongLabelFor,\n\tscalarShortLabelFor,\n\tunitLabel as unitLabelBuiltIn,\n} from './format'\nimport { DIMENSION_LOCALE_DEFAULTS } from './locale'\nimport {\n\tCOMPOUND_MINOR_PRECISION,\n\tCOMPOUNDS,\n\ttype CompoundUnitId,\n\tDISPLAY_PRECISION,\n\tisScalarUnit as isBuiltInScalarUnit,\n\ttype ScalarUnitId,\n\tUNITS,\n\tunitsOfDimension as unitsOfDimensionBuiltIn,\n} from './units'\n\nexport type CustomUnitDef = {\n\tdimension: string\n\tfactor: number\n\toffset?: number\n\t/** ECMA-402 unit identifier, or null when there is none: formatting falls back to plain decimal + shortLabel. */\n\tintlUnit: string | null\n\t/** A plain string applies to every locale; a map resolves exact locale, then language prefix, then 'en'. */\n\tshortLabel: LocalizedLabel\n\t/** Long-form label, same resolution as shortLabel. Defaults to shortLabel when omitted. */\n\tlongLabel?: LocalizedLabel\n\t/** Display fraction digits for this unit. Defaults to 2 when omitted. */\n\tprecision?: number\n}\n\nexport type CustomDimensionDef = {\n\t/** Must name one of `custom.units` for this dimension, with factor 1 and no offset. */\n\tcanonicalUnit: string\n}\n\nexport type MeasurementCustomConfig = {\n\tunits?: Record<string, CustomUnitDef>\n\tdimensions?: Record<string, CustomDimensionDef>\n}\n\nexport type EngineFormatMeasurementOptions = {\n\tstorageUnit: string\n\tdisplayUnit: string\n\tlocale: string\n\tunitDisplay?: 'long' | 'narrow' | 'short'\n\tprecision?: Partial<Record<string, number>>\n}\n\ntype MergedUnit = FormattableUnit & {\n\tdimension: string\n\tfactor: number\n\toffset?: number\n\tprecision: number\n}\n\nconst CORE_DIMENSIONS = new Set<string>(Object.keys(DIMENSION_LOCALE_DEFAULTS))\n\nconst ALLOWED_CUSTOM_CONFIG_KEYS = new Set<string>(['units', 'dimensions'])\n\nconst isCompoundUnitId = (unit: string): unit is CompoundUnitId => Object.hasOwn(COMPOUNDS, unit)\n\nconst mergeUnits = (custom?: MeasurementCustomConfig): Record<string, MergedUnit> => {\n\tif (custom !== undefined) {\n\t\tfor (const key of Object.keys(custom)) {\n\t\t\tif (!ALLOWED_CUSTOM_CONFIG_KEYS.has(key)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Custom config key \"${key}\" is not recognized; only \"units\" and \"dimensions\" are allowed`\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t}\n\n\tconst customUnits = custom?.units ?? {}\n\tconst customDimensions = custom?.dimensions ?? {}\n\n\tfor (const dimensionId of Object.keys(customDimensions)) {\n\t\tif (CORE_DIMENSIONS.has(dimensionId)) {\n\t\t\tthrow new Error(`Custom dimension \"${dimensionId}\" collides with a built-in dimension`)\n\t\t}\n\t}\n\n\tconst units: Record<string, MergedUnit> = {}\n\tfor (const id of Object.keys(UNITS) as ScalarUnitId[]) {\n\t\tunits[id] = { ...UNITS[id], precision: DISPLAY_PRECISION[id] }\n\t}\n\n\tfor (const [id, def] of Object.entries(customUnits)) {\n\t\tif (Object.hasOwn(UNITS, id) || Object.hasOwn(COMPOUNDS, id)) {\n\t\t\tthrow new Error(`Custom unit \"${id}\" collides with a built-in unit id`)\n\t\t}\n\t\t// Unreachable while customUnits is a plain object (keys are already unique), but\n\t\t// kept as a guard against a future custom-unit source that permits duplicates.\n\t\tif (Object.hasOwn(units, id)) {\n\t\t\tthrow new Error(`Custom unit \"${id}\" collides with another custom unit id`)\n\t\t}\n\t\tif (!CORE_DIMENSIONS.has(def.dimension) && !Object.hasOwn(customDimensions, def.dimension)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Custom unit \"${id}\" references unknown dimension \"${def.dimension}\": declare it in custom.dimensions or use a built-in dimension`\n\t\t\t)\n\t\t}\n\t\tunits[id] = {\n\t\t\tdimension: def.dimension,\n\t\t\tfactor: def.factor,\n\t\t\toffset: def.offset,\n\t\t\tintlUnit: def.intlUnit,\n\t\t\tshortLabel: def.shortLabel,\n\t\t\tlongLabel: def.longLabel,\n\t\t\tprecision: def.precision ?? 2,\n\t\t}\n\t}\n\n\tfor (const [dimensionId, def] of Object.entries(customDimensions)) {\n\t\tconst canonical = units[def.canonicalUnit]\n\t\tif (!canonical || canonical.dimension !== dimensionId) {\n\t\t\tthrow new Error(\n\t\t\t\t`Custom dimension \"${dimensionId}\" canonicalUnit \"${def.canonicalUnit}\" must be a custom unit declared for dimension \"${dimensionId}\"`\n\t\t\t)\n\t\t}\n\t\tif (canonical.factor !== 1 || typeof canonical.offset !== 'undefined') {\n\t\t\tthrow new Error(\n\t\t\t\t`Custom dimension \"${dimensionId}\" canonicalUnit \"${def.canonicalUnit}\" must have factor 1 and no offset`\n\t\t\t)\n\t\t}\n\t}\n\n\treturn units\n}\n\n/**\n * Builds a measurement engine over the built-in units merged with a serializable custom\n * config. Compound units (ft-in, st-lb) are always built-in: custom units are scalar-only,\n * so decompose/compose run on the built-in table, bridged for a custom storage unit.\n */\nexport const createEngine = (custom?: MeasurementCustomConfig) => {\n\tconst units = mergeUnits(custom)\n\n\tconst getUnit = (unit: string): MergedUnit => {\n\t\tconst def = units[unit]\n\t\tif (!def) throw new Error(`Unknown unit \"${unit}\"`)\n\t\treturn def\n\t}\n\n\tconst convert = (value: number, from: string, to: string): number =>\n\t\tconvertUnit(units, value, from, to)\n\n\tconst isScalarUnit = (unit: string): boolean => Object.hasOwn(units, unit)\n\n\t// Compound units are built-in and split on the built-in table, so a custom\n\t// storage unit is bridged through the compound's own major unit.\n\t// biome-ignore lint/complexity/useMaxParams: mirrors the built-in decompose signature\n\tconst decompose = (\n\t\tvalue: number,\n\t\tfrom: string,\n\t\tcompound: CompoundUnitId,\n\t\tminorDigits?: number\n\t): { major: number; minor: number } => {\n\t\tif (isBuiltInScalarUnit(from)) return decomposeBuiltIn(value, from, compound, minorDigits)\n\t\tconst bridge = COMPOUNDS[compound].major\n\t\treturn decomposeBuiltIn(convert(value, from, bridge), bridge, compound, minorDigits)\n\t}\n\n\tconst compose = (\n\t\tparts: { major: number; minor: number },\n\t\tcompound: CompoundUnitId,\n\t\tto: string\n\t): number => {\n\t\tif (isBuiltInScalarUnit(to)) return composeBuiltIn(parts, compound, to)\n\t\tconst bridge = COMPOUNDS[compound].major\n\t\treturn convert(composeBuiltIn(parts, compound, bridge), bridge, to)\n\t}\n\n\tconst dimensionOf = (unit: string): string =>\n\t\tisCompoundUnitId(unit) ? UNITS[COMPOUNDS[unit].major].dimension : getUnit(unit).dimension\n\n\tconst unitsOfDimension = (dimension: string): string[] => {\n\t\tconst customIds = Object.entries(custom?.units ?? {})\n\t\t\t.filter(([, def]) => def.dimension === dimension)\n\t\t\t.map(([id]) => id)\n\t\treturn [...unitsOfDimensionBuiltIn(dimension), ...customIds]\n\t}\n\n\tconst precisionFor = (unit: string, overrides?: Partial<Record<string, number>>): number => {\n\t\tconst override = overrides?.[unit]\n\t\tif (typeof override === 'number') return override\n\t\treturn isCompoundUnitId(unit) ? COMPOUND_MINOR_PRECISION[unit] : getUnit(unit).precision\n\t}\n\n\tconst unitLabel = (unit: string, locale: string, style: 'long' | 'short'): string => {\n\t\tif (isCompoundUnitId(unit)) return unitLabelBuiltIn(unit, locale, style)\n\t\tconst def = getUnit(unit)\n\t\treturn style === 'short' ? scalarShortLabelFor(def, locale) : scalarLongLabelFor(def, locale)\n\t}\n\n\tconst formatMeasurement = (value: number, opts: EngineFormatMeasurementOptions): string => {\n\t\tconst { displayUnit, locale, precision, storageUnit, unitDisplay = 'short' } = opts\n\t\tif (isCompoundUnitId(displayUnit)) {\n\t\t\tconst bridge = COMPOUNDS[displayUnit].major\n\t\t\tconst builtInStorage = isBuiltInScalarUnit(storageUnit)\n\t\t\treturn formatMeasurementBuiltIn(\n\t\t\t\tbuiltInStorage ? value : convert(value, storageUnit, bridge),\n\t\t\t\t{\n\t\t\t\t\t...opts,\n\t\t\t\t\tstorageUnit: builtInStorage ? storageUnit : bridge,\n\t\t\t\t\tdisplayUnit,\n\t\t\t\t}\n\t\t\t)\n\t\t}\n\t\tconst digits = precisionFor(displayUnit, precision)\n\t\treturn formatScalarValue(roundTo(convert(value, storageUnit, displayUnit), digits), {\n\t\t\tdigits,\n\t\t\tlocale,\n\t\t\tunit: getUnit(displayUnit),\n\t\t\tunitDisplay,\n\t\t})\n\t}\n\n\treturn {\n\t\tcompose,\n\t\tconvert,\n\t\tdecompose,\n\t\tdimensionOf,\n\t\tformatMeasurement,\n\t\tisCompoundUnit: isCompoundUnitId,\n\t\tisScalarUnit,\n\t\tprecisionFor,\n\t\tunitLabel,\n\t\tunitsOfDimension,\n\t}\n}\n\nexport type MeasurementEngine = ReturnType<typeof createEngine>\n\nexport const defaultEngine: MeasurementEngine = createEngine()\n"],"mappings":"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"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
//#region src/fields/measurement/engine/units.d.ts
|
|
2
|
+
type CoreDimension = 'length' | 'mass' | 'speed' | 'temperature' | 'volume';
|
|
3
|
+
type Dimension = CoreDimension | (string & {});
|
|
4
|
+
type ScalarUnitId = 'c' | 'cm' | 'f' | 'fl-oz' | 'ft' | 'g' | 'gal' | 'in' | 'kg' | 'km' | 'km/h' | 'l' | 'lb' | 'm' | 'm/s' | 'mi' | 'ml' | 'mm' | 'mph' | 'oz' | 'st' | 'yd';
|
|
5
|
+
type CompoundUnitId = 'ft-in' | 'st-lb';
|
|
6
|
+
type UnitId = CompoundUnitId | ScalarUnitId;
|
|
7
|
+
/**
|
|
8
|
+
* Any unit id an engine may know: the built-ins, plus whatever a field declares in
|
|
9
|
+
* `custom`. Keeps built-in ids autocompleting while custom ids stay assignable.
|
|
10
|
+
*/
|
|
11
|
+
type MeasurementUnitId = UnitId | (string & {});
|
|
12
|
+
declare const UNITS: {
|
|
13
|
+
readonly g: {
|
|
14
|
+
readonly dimension: "mass";
|
|
15
|
+
readonly factor: 0.001;
|
|
16
|
+
readonly intlUnit: "gram";
|
|
17
|
+
readonly shortLabel: "g";
|
|
18
|
+
};
|
|
19
|
+
readonly kg: {
|
|
20
|
+
readonly dimension: "mass";
|
|
21
|
+
readonly factor: 1;
|
|
22
|
+
readonly intlUnit: "kilogram";
|
|
23
|
+
readonly shortLabel: "kg";
|
|
24
|
+
};
|
|
25
|
+
readonly oz: {
|
|
26
|
+
readonly dimension: "mass";
|
|
27
|
+
readonly factor: number;
|
|
28
|
+
readonly intlUnit: "ounce";
|
|
29
|
+
readonly shortLabel: "oz";
|
|
30
|
+
};
|
|
31
|
+
readonly lb: {
|
|
32
|
+
readonly dimension: "mass";
|
|
33
|
+
readonly factor: 0.45359237;
|
|
34
|
+
readonly intlUnit: "pound";
|
|
35
|
+
readonly shortLabel: "lb";
|
|
36
|
+
};
|
|
37
|
+
readonly st: {
|
|
38
|
+
readonly dimension: "mass";
|
|
39
|
+
readonly factor: number;
|
|
40
|
+
readonly intlUnit: "stone";
|
|
41
|
+
readonly shortLabel: "st";
|
|
42
|
+
};
|
|
43
|
+
readonly mm: {
|
|
44
|
+
readonly dimension: "length";
|
|
45
|
+
readonly factor: 0.001;
|
|
46
|
+
readonly intlUnit: "millimeter";
|
|
47
|
+
readonly shortLabel: "mm";
|
|
48
|
+
};
|
|
49
|
+
readonly cm: {
|
|
50
|
+
readonly dimension: "length";
|
|
51
|
+
readonly factor: 0.01;
|
|
52
|
+
readonly intlUnit: "centimeter";
|
|
53
|
+
readonly shortLabel: "cm";
|
|
54
|
+
};
|
|
55
|
+
readonly m: {
|
|
56
|
+
readonly dimension: "length";
|
|
57
|
+
readonly factor: 1;
|
|
58
|
+
readonly intlUnit: "meter";
|
|
59
|
+
readonly shortLabel: "m";
|
|
60
|
+
};
|
|
61
|
+
readonly km: {
|
|
62
|
+
readonly dimension: "length";
|
|
63
|
+
readonly factor: 1000;
|
|
64
|
+
readonly intlUnit: "kilometer";
|
|
65
|
+
readonly shortLabel: "km";
|
|
66
|
+
};
|
|
67
|
+
readonly in: {
|
|
68
|
+
readonly dimension: "length";
|
|
69
|
+
readonly factor: 0.0254;
|
|
70
|
+
readonly intlUnit: "inch";
|
|
71
|
+
readonly shortLabel: "in";
|
|
72
|
+
};
|
|
73
|
+
readonly ft: {
|
|
74
|
+
readonly dimension: "length";
|
|
75
|
+
readonly factor: number;
|
|
76
|
+
readonly intlUnit: "foot";
|
|
77
|
+
readonly shortLabel: "ft";
|
|
78
|
+
};
|
|
79
|
+
readonly yd: {
|
|
80
|
+
readonly dimension: "length";
|
|
81
|
+
readonly factor: number;
|
|
82
|
+
readonly intlUnit: "yard";
|
|
83
|
+
readonly shortLabel: "yd";
|
|
84
|
+
};
|
|
85
|
+
readonly mi: {
|
|
86
|
+
readonly dimension: "length";
|
|
87
|
+
readonly factor: 1609.344;
|
|
88
|
+
readonly intlUnit: "mile";
|
|
89
|
+
readonly shortLabel: "mi";
|
|
90
|
+
};
|
|
91
|
+
readonly ml: {
|
|
92
|
+
readonly dimension: "volume";
|
|
93
|
+
readonly factor: 0.001;
|
|
94
|
+
readonly intlUnit: "milliliter";
|
|
95
|
+
readonly shortLabel: "ml";
|
|
96
|
+
};
|
|
97
|
+
readonly l: {
|
|
98
|
+
readonly dimension: "volume";
|
|
99
|
+
readonly factor: 1;
|
|
100
|
+
readonly intlUnit: "liter";
|
|
101
|
+
readonly shortLabel: "l";
|
|
102
|
+
};
|
|
103
|
+
readonly 'fl-oz': {
|
|
104
|
+
readonly dimension: "volume";
|
|
105
|
+
readonly factor: 0.0295735295625;
|
|
106
|
+
readonly intlUnit: "fluid-ounce";
|
|
107
|
+
readonly shortLabel: "fl oz";
|
|
108
|
+
};
|
|
109
|
+
readonly gal: {
|
|
110
|
+
readonly dimension: "volume";
|
|
111
|
+
readonly factor: 3.785411784;
|
|
112
|
+
readonly intlUnit: "gallon";
|
|
113
|
+
readonly shortLabel: "gal";
|
|
114
|
+
};
|
|
115
|
+
readonly c: {
|
|
116
|
+
readonly dimension: "temperature";
|
|
117
|
+
readonly factor: 1;
|
|
118
|
+
readonly intlUnit: "celsius";
|
|
119
|
+
readonly shortLabel: "°C";
|
|
120
|
+
};
|
|
121
|
+
readonly f: {
|
|
122
|
+
readonly dimension: "temperature";
|
|
123
|
+
readonly factor: number;
|
|
124
|
+
readonly offset: number;
|
|
125
|
+
readonly intlUnit: "fahrenheit";
|
|
126
|
+
readonly shortLabel: "°F";
|
|
127
|
+
};
|
|
128
|
+
readonly 'km/h': {
|
|
129
|
+
readonly dimension: "speed";
|
|
130
|
+
readonly factor: number;
|
|
131
|
+
readonly intlUnit: "kilometer-per-hour";
|
|
132
|
+
readonly shortLabel: "km/h";
|
|
133
|
+
};
|
|
134
|
+
readonly mph: {
|
|
135
|
+
readonly dimension: "speed";
|
|
136
|
+
readonly factor: 0.44704;
|
|
137
|
+
readonly intlUnit: "mile-per-hour";
|
|
138
|
+
readonly shortLabel: "mph";
|
|
139
|
+
};
|
|
140
|
+
readonly 'm/s': {
|
|
141
|
+
readonly dimension: "speed";
|
|
142
|
+
readonly factor: 1;
|
|
143
|
+
readonly intlUnit: "meter-per-second";
|
|
144
|
+
readonly shortLabel: "m/s";
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
declare const COMPOUNDS: {
|
|
148
|
+
readonly 'ft-in': {
|
|
149
|
+
readonly major: "ft";
|
|
150
|
+
readonly minor: "in";
|
|
151
|
+
readonly ratio: 12;
|
|
152
|
+
};
|
|
153
|
+
readonly 'st-lb': {
|
|
154
|
+
readonly major: "st";
|
|
155
|
+
readonly minor: "lb";
|
|
156
|
+
readonly ratio: 14;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
declare const isCompoundUnit: (unit: UnitId) => unit is CompoundUnitId;
|
|
160
|
+
declare const isScalarUnit: (unit: string) => unit is ScalarUnitId;
|
|
161
|
+
declare const dimensionOf: (unit: UnitId) => Dimension;
|
|
162
|
+
/** Dimension of a built-in scalar unit, literal when S is one and the full union otherwise. */
|
|
163
|
+
type DimOf<S extends ScalarUnitId> = (typeof UNITS)[S]['dimension'];
|
|
164
|
+
/** Scalar unit ids whose dimension is (a member of) D. */
|
|
165
|
+
type ScalarOfDimension<D extends CoreDimension> = { [K in ScalarUnitId]: (typeof UNITS)[K]['dimension'] extends D ? K : never }[ScalarUnitId];
|
|
166
|
+
/** Compound unit ids whose major scalar's dimension is (a member of) D. */
|
|
167
|
+
type CompoundOfDimension<D extends CoreDimension> = { [K in CompoundUnitId]: (typeof UNITS)[(typeof COMPOUNDS)[K]['major']]['dimension'] extends D ? K : never }[CompoundUnitId];
|
|
168
|
+
/** Every unit (scalar or compound) selectable for D: scalars of D plus compounds whose major is D. */
|
|
169
|
+
type UnitOfDimension<D extends CoreDimension> = ScalarOfDimension<D> | CompoundOfDimension<D>;
|
|
170
|
+
/** Built-in scalars and compounds belonging to a dimension, compounds keyed by their major. */
|
|
171
|
+
declare const unitsOfDimension: (dimension: string) => UnitId[];
|
|
172
|
+
/** Values round-trip through display without visible drift at these precisions. */
|
|
173
|
+
declare const DISPLAY_PRECISION: Record<ScalarUnitId, number>;
|
|
174
|
+
declare const STORAGE_FRACTION_DIGITS = 6;
|
|
175
|
+
declare const precisionFor: (unit: UnitId, overrides?: Partial<Record<UnitId, number>>) => number;
|
|
176
|
+
//#endregion
|
|
177
|
+
export { COMPOUNDS, CompoundUnitId, CoreDimension, DISPLAY_PRECISION, DimOf, Dimension, MeasurementUnitId, STORAGE_FRACTION_DIGITS, ScalarOfDimension, ScalarUnitId, UNITS, UnitId, UnitOfDimension, dimensionOf, isCompoundUnit, isScalarUnit, precisionFor, unitsOfDimension };
|
|
178
|
+
//# sourceMappingURL=units.d.ts.map
|