@ni/unit-format 0.0.1

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.
Files changed (48) hide show
  1. package/README.md +72 -0
  2. package/dist/esm/decimal/index.d.ts +19 -0
  3. package/dist/esm/decimal/index.js +46 -0
  4. package/dist/esm/decimal/index.js.map +1 -0
  5. package/dist/esm/default/index.d.ts +22 -0
  6. package/dist/esm/default/index.js +70 -0
  7. package/dist/esm/default/index.js.map +1 -0
  8. package/dist/esm/index.d.ts +16 -0
  9. package/dist/esm/index.js +21 -0
  10. package/dist/esm/index.js.map +1 -0
  11. package/dist/esm/scaled-unit/index.d.ts +16 -0
  12. package/dist/esm/scaled-unit/index.js +13 -0
  13. package/dist/esm/scaled-unit/index.js.map +1 -0
  14. package/dist/esm/scaled-unit-format/index.d.ts +10 -0
  15. package/dist/esm/scaled-unit-format/index.js +10 -0
  16. package/dist/esm/scaled-unit-format/index.js.map +1 -0
  17. package/dist/esm/scaled-unit-format/intl-number-format/index.d.ts +11 -0
  18. package/dist/esm/scaled-unit-format/intl-number-format/index.js +21 -0
  19. package/dist/esm/scaled-unit-format/intl-number-format/index.js.map +1 -0
  20. package/dist/esm/scaled-unit-format/manually-translated/index.d.ts +43 -0
  21. package/dist/esm/scaled-unit-format/manually-translated/index.js +63 -0
  22. package/dist/esm/scaled-unit-format/manually-translated/index.js.map +1 -0
  23. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  24. package/dist/esm/unit-scale/byte/index.d.ts +9 -0
  25. package/dist/esm/unit-scale/byte/index.js +25 -0
  26. package/dist/esm/unit-scale/byte/index.js.map +1 -0
  27. package/dist/esm/unit-scale/byte-1024/index.d.ts +9 -0
  28. package/dist/esm/unit-scale/byte-1024/index.js +31 -0
  29. package/dist/esm/unit-scale/byte-1024/index.js.map +1 -0
  30. package/dist/esm/unit-scale/celsius/index.d.ts +9 -0
  31. package/dist/esm/unit-scale/celsius/index.js +18 -0
  32. package/dist/esm/unit-scale/celsius/index.js.map +1 -0
  33. package/dist/esm/unit-scale/fahrenheit/index.d.ts +9 -0
  34. package/dist/esm/unit-scale/fahrenheit/index.js +18 -0
  35. package/dist/esm/unit-scale/fahrenheit/index.js.map +1 -0
  36. package/dist/esm/unit-scale/index.d.ts +15 -0
  37. package/dist/esm/unit-scale/index.js +49 -0
  38. package/dist/esm/unit-scale/index.js.map +1 -0
  39. package/dist/esm/unit-scale/passthrough/index.d.ts +9 -0
  40. package/dist/esm/unit-scale/passthrough/index.js +15 -0
  41. package/dist/esm/unit-scale/passthrough/index.js.map +1 -0
  42. package/dist/esm/unit-scale/utilities/metric-prefixes.d.ts +1 -0
  43. package/dist/esm/unit-scale/utilities/metric-prefixes.js +17 -0
  44. package/dist/esm/unit-scale/utilities/metric-prefixes.js.map +1 -0
  45. package/dist/esm/unit-scale/volt/index.d.ts +9 -0
  46. package/dist/esm/unit-scale/volt/index.js +24 -0
  47. package/dist/esm/unit-scale/volt/index.js.map +1 -0
  48. package/package.json +64 -0
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ <div align="center">
2
+ <p><b>ni | unit format</b></p>
3
+ </div>
4
+
5
+ # Unit Format
6
+
7
+ [![NPM Version](https://img.shields.io/npm/v/@ni/unit-format.svg)](https://www.npmjs.com/package/@ni/unit-format)
8
+
9
+ The `@ni/unit-format` library provides a way to format numbers with units. The library:
10
+
11
+ - Provides opinionated number formatters that are well-suited for:
12
+ - numbers that can appear in large ranges (very small or very large).
13
+ - numbers that are always decimal with a configurable number of digits.
14
+ - Can scale number values on a unit scale, for example, a metric scale of voltage.
15
+ - Provides internationalized number formatting and unit strings.
16
+
17
+ The library is intended to align well with the [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) API to make it relatively straightforward to swap in.
18
+
19
+ ## Getting Started
20
+
21
+ Choose a `UnitFormat` and create an instance with an optional `UnitScale` reference:
22
+
23
+ ```ts
24
+ const formatter = new UnitFormatDefault('en', {
25
+ unitScale: unitScaleByte
26
+ });
27
+ console.log(formatter.format(1000));
28
+ // Output: '1 kB'
29
+ ```
30
+
31
+ ## Concepts
32
+
33
+ ### UnitFormat
34
+
35
+ There are multiple `UnitFormat`s that represent different configurations for formatting numbers. These represent wrappers around `Intl.NumberFormat` with opinionated configurations.
36
+
37
+ For example, the `UnitFormatDefault` will represent "very large" and "very small" numbers with an exponential notation and numbers between those ranges as decimal. The different `UnitFormat`s take an optional `unitScale` that provides units and automatic conversions of numbers for that unit scale.
38
+
39
+ ### UnitScale
40
+
41
+ A `UnitScale` represents a collection of `ScaledUnits` where a scale factor is used to define each scaled unit. Each `UnitScale` must have a "base" `ScaledUnit` with scale factor 1. For example, a byte `UnitScale` may contain:
42
+
43
+ - a `ScaledUnit` of scale factor 1 representing bytes (the base `ScaledUnit`)
44
+ - a `ScaledUnit` of scale factor 1000 representing kilobytes.
45
+
46
+ Each `ScaledUnit` represents a scale factor and a factory function for creating a `ScaledUnitFormat` instance. This system is used so that the top-level `UnitFormat` objects can pass configuration settings to individual `ScaledUnitFormat` instances.
47
+
48
+ ## ScaledUnitFormat
49
+
50
+ A `ScaledUnitFormat` is an object for formatting a particular `ScaledUnit` on the `UnitScale`.
51
+
52
+ There are two main types of `ScaledUnitFormat`s:
53
+
54
+ - `ScaledUnitFormatIntlNumberFormat` which are scaled units can be translated by [`Intl.NumberFormat`](https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers).
55
+ - `ScaledUnitFormatManuallyTranslated` which are scaled units manually translated for a set of supported languages via `UnitTranslation` string collections.
56
+
57
+ The different `ScaledUnitFormat` objects have a static function to assist with their factory creation, for example:
58
+
59
+ ```ts
60
+ new ScaledUnit(
61
+ 10 ** 3,
62
+ ScaledUnitFormatIntlNumberFormat.createFactory({
63
+ style: 'unit',
64
+ unit: 'kilobyte',
65
+ unitDisplay: 'short'
66
+ })
67
+ ),
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ See `Getting Started` in [`CONTRIBUTING.md`](/packages/unit-format/CONTRIBUTING.md#getting-started).
@@ -0,0 +1,19 @@
1
+ import { UnitFormat, type UnitFormatOptions } from '../index.js';
2
+ interface UnitFormatDecimalOptions extends UnitFormatOptions {
3
+ minimumFractionDigits?: number;
4
+ maximumFractionDigits?: number;
5
+ }
6
+ type UnitFormatDecimalResolvedOptions = Required<UnitFormatDecimalOptions>;
7
+ /**
8
+ * Format for decimal numbers with units.
9
+ */
10
+ export declare class UnitFormatDecimal extends UnitFormat<UnitFormatDecimalOptions> {
11
+ private readonly unitScale;
12
+ private readonly minimumFractionDigits;
13
+ private readonly maximumFractionDigits;
14
+ private readonly scaledUnitFormatters;
15
+ constructor(locale: string, { minimumFractionDigits, maximumFractionDigits, unitScale }?: UnitFormatDecimalOptions);
16
+ resolvedOptions(): UnitFormatDecimalResolvedOptions;
17
+ protected tryFormat(number: number): string;
18
+ }
19
+ export {};
@@ -0,0 +1,46 @@
1
+ import { UnitFormat } from '../index.js';
2
+ import { unitScalePassthrough } from '../unit-scale/passthrough/index.js';
3
+ /**
4
+ * Format for decimal numbers with units.
5
+ */
6
+ export class UnitFormatDecimal extends UnitFormat {
7
+ constructor(locale, { minimumFractionDigits = 0, maximumFractionDigits = Math.max(3, minimumFractionDigits), unitScale = unitScalePassthrough } = {
8
+ minimumFractionDigits: 0,
9
+ maximumFractionDigits: 3,
10
+ unitScale: unitScalePassthrough
11
+ }) {
12
+ super();
13
+ this.scaledUnitFormatters = new Map();
14
+ // Workaround to avoid a ts error about signDisplay not accepting the value 'negative'.
15
+ // It has been supported by browsers since 8/23, but TypeScript still hasn't
16
+ // added it to the type definitions. See https://github.com/microsoft/TypeScript/issues/56269
17
+ const signDisplay = 'negative';
18
+ const intlNumberFormatOptions = {
19
+ maximumFractionDigits,
20
+ minimumFractionDigits,
21
+ signDisplay
22
+ };
23
+ for (const scaledUnit of unitScale.supportedScaledUnits) {
24
+ this.scaledUnitFormatters.set(scaledUnit.scaleFactor, scaledUnit.scaledUnitFormatFactory({
25
+ locale,
26
+ intlNumberFormatOptions
27
+ }));
28
+ }
29
+ this.unitScale = unitScale;
30
+ this.minimumFractionDigits = minimumFractionDigits;
31
+ this.maximumFractionDigits = maximumFractionDigits;
32
+ }
33
+ resolvedOptions() {
34
+ return {
35
+ unitScale: this.unitScale,
36
+ maximumFractionDigits: this.maximumFractionDigits,
37
+ minimumFractionDigits: this.minimumFractionDigits
38
+ };
39
+ }
40
+ tryFormat(number) {
41
+ const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);
42
+ const scaledUnitFormatter = this.scaledUnitFormatters.get(scaledUnit.scaleFactor);
43
+ return scaledUnitFormatter.format(scaledValue);
44
+ }
45
+ }
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decimal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,aAAa,CAAC;AAGjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAQ1E;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAoC;IAOvE,YACI,MAAc,EACd,EACI,qBAAqB,GAAG,CAAC,EACzB,qBAAqB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,qBAAqB,CAAC,EAC1D,SAAS,GAAG,oBAAoB,KACN;QAC1B,qBAAqB,EAAE,CAAC;QACxB,qBAAqB,EAAE,CAAC;QACxB,SAAS,EAAE,oBAAoB;KAClC;QAED,KAAK,EAAE,CAAC;QAdK,yBAAoB,GAAG,IAAI,GAAG,EAA4B,CAAC;QAexE,uFAAuF;QACvF,4EAA4E;QAC5E,6FAA6F;QAC7F,MAAM,WAAW,GAAG,UAAqD,CAAC;QAC1E,MAAM,uBAAuB,GAAG;YAC5B,qBAAqB;YACrB,qBAAqB;YACrB,WAAW;SACd,CAAC;QACF,KAAK,MAAM,UAAU,IAAI,SAAS,CAAC,oBAAoB,EAAE,CAAC;YACtD,IAAI,CAAC,oBAAoB,CAAC,GAAG,CACzB,UAAU,CAAC,WAAW,EACtB,UAAU,CAAC,uBAAuB,CAAC;gBAC/B,MAAM;gBACN,uBAAuB;aAC1B,CAAC,CACL,CAAC;QACN,CAAC;QACD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;QACnD,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;IACvD,CAAC;IAEe,eAAe;QAC3B,OAAO;YACH,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,qBAAqB,EAAE,IAAI,CAAC,qBAAqB;YACjD,qBAAqB,EAAE,IAAI,CAAC,qBAAqB;SACpD,CAAC;IACN,CAAC;IAES,SAAS,CAAC,MAAc;QAC9B,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACvE,MAAM,mBAAmB,GAAG,IAAI,CAAC,oBAAoB,CAAC,GAAG,CACrD,UAAU,CAAC,WAAW,CACxB,CAAC;QACH,OAAO,mBAAmB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACnD,CAAC;CACJ","sourcesContent":["import { UnitFormat, type UnitFormatOptions } from '../index.js';\r\nimport type { ScaledUnitFormat } from '../scaled-unit-format/index.js';\r\nimport type { UnitScale } from '../unit-scale/index.js';\r\nimport { unitScalePassthrough } from '../unit-scale/passthrough/index.js';\r\n\r\ninterface UnitFormatDecimalOptions extends UnitFormatOptions {\r\n minimumFractionDigits?: number;\r\n maximumFractionDigits?: number;\r\n}\r\ntype UnitFormatDecimalResolvedOptions = Required<UnitFormatDecimalOptions>;\r\n\r\n/**\r\n * Format for decimal numbers with units.\r\n */\r\nexport class UnitFormatDecimal extends UnitFormat<UnitFormatDecimalOptions> {\r\n private readonly unitScale: UnitScale;\r\n private readonly minimumFractionDigits: number;\r\n private readonly maximumFractionDigits: number;\r\n\r\n private readonly scaledUnitFormatters = new Map<number, ScaledUnitFormat>();\r\n\r\n public constructor(\r\n locale: string,\r\n {\r\n minimumFractionDigits = 0,\r\n maximumFractionDigits = Math.max(3, minimumFractionDigits),\r\n unitScale = unitScalePassthrough\r\n }: UnitFormatDecimalOptions = {\r\n minimumFractionDigits: 0,\r\n maximumFractionDigits: 3,\r\n unitScale: unitScalePassthrough\r\n }\r\n ) {\r\n super();\r\n // Workaround to avoid a ts error about signDisplay not accepting the value 'negative'.\r\n // It has been supported by browsers since 8/23, but TypeScript still hasn't\r\n // added it to the type definitions. See https://github.com/microsoft/TypeScript/issues/56269\r\n const signDisplay = 'negative' as Intl.NumberFormatOptions['signDisplay'];\r\n const intlNumberFormatOptions = {\r\n maximumFractionDigits,\r\n minimumFractionDigits,\r\n signDisplay\r\n };\r\n for (const scaledUnit of unitScale.supportedScaledUnits) {\r\n this.scaledUnitFormatters.set(\r\n scaledUnit.scaleFactor,\r\n scaledUnit.scaledUnitFormatFactory({\r\n locale,\r\n intlNumberFormatOptions\r\n })\r\n );\r\n }\r\n this.unitScale = unitScale;\r\n this.minimumFractionDigits = minimumFractionDigits;\r\n this.maximumFractionDigits = maximumFractionDigits;\r\n }\r\n\r\n public override resolvedOptions(): UnitFormatDecimalResolvedOptions {\r\n return {\r\n unitScale: this.unitScale,\r\n maximumFractionDigits: this.maximumFractionDigits,\r\n minimumFractionDigits: this.minimumFractionDigits\r\n };\r\n }\r\n\r\n protected tryFormat(number: number): string {\r\n const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);\r\n const scaledUnitFormatter = this.scaledUnitFormatters.get(\r\n scaledUnit.scaleFactor\r\n )!;\r\n return scaledUnitFormatter.format(scaledValue);\r\n }\r\n}\r\n"]}
@@ -0,0 +1,22 @@
1
+ import { UnitFormat, type UnitFormatOptions } from '../index.js';
2
+ interface UnitFormatDefaultOptions extends UnitFormatOptions {
3
+ }
4
+ type UnitFormatDefaultResolvedOptions = Required<UnitFormatDefaultOptions>;
5
+ /**
6
+ * Format for numbers with units to show in a tabular form.
7
+ * Large and tiny numbers are shown exponentially and the rest as decimal.
8
+ */
9
+ export declare class UnitFormatDefault extends UnitFormat {
10
+ private static readonly maximumDigits;
11
+ private static readonly exponentialLowerBound;
12
+ private static readonly exponentialUpperBound;
13
+ private readonly unitScale;
14
+ private readonly defaultIntlNumberFormatOptions;
15
+ private readonly defaultScaledUnitFormatters;
16
+ private readonly exponentialIntlNumberFormatOptions;
17
+ private readonly exponentialScaledUnitFormatter;
18
+ constructor(locale: string, { unitScale }?: UnitFormatDefaultOptions);
19
+ resolvedOptions(): UnitFormatDefaultResolvedOptions;
20
+ protected tryFormat(number: number): string;
21
+ }
22
+ export {};
@@ -0,0 +1,70 @@
1
+ import { UnitFormat } from '../index.js';
2
+ import { unitScalePassthrough } from '../unit-scale/passthrough/index.js';
3
+ // Workaround to avoid ts errors about signDisplay not accepting the value 'negative'.
4
+ // It has been supported by browsers since 8/23, but TypeScript still hasn't
5
+ // added it to the type definitions. See https://github.com/microsoft/TypeScript/issues/56269
6
+ const signDisplay = 'negative';
7
+ /**
8
+ * Format for numbers with units to show in a tabular form.
9
+ * Large and tiny numbers are shown exponentially and the rest as decimal.
10
+ */
11
+ export class UnitFormatDefault extends UnitFormat {
12
+ constructor(locale, { unitScale = unitScalePassthrough } = {
13
+ unitScale: unitScalePassthrough
14
+ }) {
15
+ super();
16
+ // Format options to use by default. It renders the number with a maximum of 6 signficant digits (including zero before decimal point).
17
+ this.defaultIntlNumberFormatOptions = {
18
+ maximumSignificantDigits: UnitFormatDefault.maximumDigits,
19
+ maximumFractionDigits: UnitFormatDefault.maximumDigits - 1,
20
+ roundingPriority: 'lessPrecision',
21
+ signDisplay
22
+ };
23
+ this.defaultScaledUnitFormatters = new Map();
24
+ // Format options for numbers that should be displayed in exponential notation. This should be used
25
+ // for numbers with magintudes over 'exponentialUpperBound' or under 'exponentialLowerBound'.
26
+ this.exponentialIntlNumberFormatOptions = {
27
+ maximumSignificantDigits: UnitFormatDefault.maximumDigits,
28
+ notation: 'scientific',
29
+ signDisplay
30
+ };
31
+ for (const unit of unitScale.supportedScaledUnits) {
32
+ this.defaultScaledUnitFormatters.set(unit.scaleFactor, unit.scaledUnitFormatFactory({
33
+ locale,
34
+ intlNumberFormatOptions: this.defaultIntlNumberFormatOptions
35
+ }));
36
+ }
37
+ this.exponentialScaledUnitFormatter = unitScale.baseScaledUnit.scaledUnitFormatFactory({
38
+ locale,
39
+ intlNumberFormatOptions: this.exponentialIntlNumberFormatOptions
40
+ });
41
+ this.unitScale = unitScale;
42
+ }
43
+ resolvedOptions() {
44
+ return {
45
+ unitScale: this.unitScale
46
+ };
47
+ }
48
+ tryFormat(number) {
49
+ const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);
50
+ const absoluteValue = Math.abs(scaledValue);
51
+ const useExponential = absoluteValue !== 0
52
+ && (absoluteValue >= UnitFormatDefault.exponentialUpperBound
53
+ || absoluteValue < UnitFormatDefault.exponentialLowerBound);
54
+ return useExponential
55
+ ? this.exponentialScaledUnitFormatter.format(number)
56
+ : this.defaultScaledUnitFormatters
57
+ .get(scaledUnit.scaleFactor)
58
+ .format(scaledValue);
59
+ }
60
+ }
61
+ // The maximum number of digits that should be rendered for any given value.
62
+ UnitFormatDefault.maximumDigits = 6;
63
+ // Use exponential notation for numbers that will be rendered with 3 leading 0s or more.
64
+ // Because a maximum of 6 digits are rendered, showing more than 3 leading 0s is not ideal
65
+ // because then at least half of the displayed digits will be leading 0s.
66
+ UnitFormatDefault.exponentialLowerBound = 0.000995;
67
+ // Use exponential formatting for numbers whose magnitude cannot otherwise be displayed
68
+ // with 6 digits or less.
69
+ UnitFormatDefault.exponentialUpperBound = 999999.5;
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/default/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,aAAa,CAAC;AAGjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E,sFAAsF;AACtF,4EAA4E;AAC5E,6FAA6F;AAC7F,MAAM,WAAW,GAAG,UAAqD,CAAC;AAO1E;;;GAGG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAqC7C,YACI,MAAc,EACd,EAAE,SAAS,GAAG,oBAAoB,KAA+B;QAC7D,SAAS,EAAE,oBAAoB;KAClC;QAED,KAAK,EAAE,CAAC;QA7BZ,uIAAuI;QACtH,mCAA8B,GAA6B;YACxE,wBAAwB,EAAE,iBAAiB,CAAC,aAAa;YACzD,qBAAqB,EAAE,iBAAiB,CAAC,aAAa,GAAG,CAAC;YAC1D,gBAAgB,EAAE,eAAe;YACjC,WAAW;SACd,CAAC;QAEe,gCAA2B,GAAG,IAAI,GAAG,EAGnD,CAAC;QAEJ,mGAAmG;QACnG,6FAA6F;QAC5E,uCAAkC,GAA6B;YAC5E,wBAAwB,EAAE,iBAAiB,CAAC,aAAa;YACzD,QAAQ,EAAE,YAAY;YACtB,WAAW;SACd,CAAC;QAWE,KAAK,MAAM,IAAI,IAAI,SAAS,CAAC,oBAAoB,EAAE,CAAC;YAChD,IAAI,CAAC,2BAA2B,CAAC,GAAG,CAChC,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,uBAAuB,CAAC;gBACzB,MAAM;gBACN,uBAAuB,EAAE,IAAI,CAAC,8BAA8B;aAC/D,CAAC,CACL,CAAC;QACN,CAAC;QACD,IAAI,CAAC,8BAA8B,GAAG,SAAS,CAAC,cAAc,CAAC,uBAAuB,CAAC;YACnF,MAAM;YACN,uBAAuB,EAAE,IAAI,CAAC,kCAAkC;SACnE,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;IAEe,eAAe;QAC3B,OAAO;YACH,SAAS,EAAE,IAAI,CAAC,SAAS;SAC5B,CAAC;IACN,CAAC;IAES,SAAS,CAAC,MAAc;QAC9B,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAEvE,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAC5C,MAAM,cAAc,GAAG,aAAa,KAAK,CAAC;eACnC,CAAC,aAAa,IAAI,iBAAiB,CAAC,qBAAqB;mBACrD,aAAa,GAAG,iBAAiB,CAAC,qBAAqB,CAAC,CAAC;QACpE,OAAO,cAAc;YACjB,CAAC,CAAC,IAAI,CAAC,8BAA8B,CAAC,MAAM,CAAC,MAAM,CAAC;YACpD,CAAC,CAAC,IAAI,CAAC,2BAA2B;iBAC7B,GAAG,CAAC,UAAU,CAAC,WAAW,CAAE;iBAC5B,MAAM,CAAC,WAAW,CAAC,CAAC;IACjC,CAAC;;AA7ED,4EAA4E;AACpD,+BAAa,GAAG,CAAC,AAAJ,CAAK;AAE1C,wFAAwF;AACxF,0FAA0F;AAC1F,yEAAyE;AACjD,uCAAqB,GAAG,QAAQ,AAAX,CAAY;AAEzD,uFAAuF;AACvF,yBAAyB;AACD,uCAAqB,GAAG,QAAQ,AAAX,CAAY","sourcesContent":["import { UnitFormat, type UnitFormatOptions } from '../index.js';\r\nimport type { ScaledUnitFormat } from '../scaled-unit-format/index.js';\r\nimport type { UnitScale } from '../unit-scale/index.js';\r\nimport { unitScalePassthrough } from '../unit-scale/passthrough/index.js';\r\n\r\n// Workaround to avoid ts errors about signDisplay not accepting the value 'negative'.\r\n// It has been supported by browsers since 8/23, but TypeScript still hasn't\r\n// added it to the type definitions. See https://github.com/microsoft/TypeScript/issues/56269\r\nconst signDisplay = 'negative' as Intl.NumberFormatOptions['signDisplay'];\r\n\r\n// Allow consistent pattern for defining Options and ResolvedOptions\r\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\r\ninterface UnitFormatDefaultOptions extends UnitFormatOptions {}\r\ntype UnitFormatDefaultResolvedOptions = Required<UnitFormatDefaultOptions>;\r\n\r\n/**\r\n * Format for numbers with units to show in a tabular form.\r\n * Large and tiny numbers are shown exponentially and the rest as decimal.\r\n */\r\nexport class UnitFormatDefault extends UnitFormat {\r\n // The maximum number of digits that should be rendered for any given value.\r\n private static readonly maximumDigits = 6;\r\n\r\n // Use exponential notation for numbers that will be rendered with 3 leading 0s or more.\r\n // Because a maximum of 6 digits are rendered, showing more than 3 leading 0s is not ideal\r\n // because then at least half of the displayed digits will be leading 0s.\r\n private static readonly exponentialLowerBound = 0.000995;\r\n\r\n // Use exponential formatting for numbers whose magnitude cannot otherwise be displayed\r\n // with 6 digits or less.\r\n private static readonly exponentialUpperBound = 999999.5;\r\n\r\n private readonly unitScale: UnitScale;\r\n // Format options to use by default. It renders the number with a maximum of 6 signficant digits (including zero before decimal point).\r\n private readonly defaultIntlNumberFormatOptions: Intl.NumberFormatOptions = {\r\n maximumSignificantDigits: UnitFormatDefault.maximumDigits,\r\n maximumFractionDigits: UnitFormatDefault.maximumDigits - 1,\r\n roundingPriority: 'lessPrecision',\r\n signDisplay\r\n };\r\n\r\n private readonly defaultScaledUnitFormatters = new Map<\r\n number,\r\n ScaledUnitFormat\r\n >();\r\n\r\n // Format options for numbers that should be displayed in exponential notation. This should be used\r\n // for numbers with magintudes over 'exponentialUpperBound' or under 'exponentialLowerBound'.\r\n private readonly exponentialIntlNumberFormatOptions: Intl.NumberFormatOptions = {\r\n maximumSignificantDigits: UnitFormatDefault.maximumDigits,\r\n notation: 'scientific',\r\n signDisplay\r\n };\r\n\r\n private readonly exponentialScaledUnitFormatter: ScaledUnitFormat;\r\n\r\n public constructor(\r\n locale: string,\r\n { unitScale = unitScalePassthrough }: UnitFormatDefaultOptions = {\r\n unitScale: unitScalePassthrough\r\n }\r\n ) {\r\n super();\r\n for (const unit of unitScale.supportedScaledUnits) {\r\n this.defaultScaledUnitFormatters.set(\r\n unit.scaleFactor,\r\n unit.scaledUnitFormatFactory({\r\n locale,\r\n intlNumberFormatOptions: this.defaultIntlNumberFormatOptions\r\n })\r\n );\r\n }\r\n this.exponentialScaledUnitFormatter = unitScale.baseScaledUnit.scaledUnitFormatFactory({\r\n locale,\r\n intlNumberFormatOptions: this.exponentialIntlNumberFormatOptions\r\n });\r\n this.unitScale = unitScale;\r\n }\r\n\r\n public override resolvedOptions(): UnitFormatDefaultResolvedOptions {\r\n return {\r\n unitScale: this.unitScale\r\n };\r\n }\r\n\r\n protected tryFormat(number: number): string {\r\n const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);\r\n\r\n const absoluteValue = Math.abs(scaledValue);\r\n const useExponential = absoluteValue !== 0\r\n && (absoluteValue >= UnitFormatDefault.exponentialUpperBound\r\n || absoluteValue < UnitFormatDefault.exponentialLowerBound);\r\n return useExponential\r\n ? this.exponentialScaledUnitFormatter.format(number)\r\n : this.defaultScaledUnitFormatters\r\n .get(scaledUnit.scaleFactor)!\r\n .format(scaledValue);\r\n }\r\n}\r\n"]}
@@ -0,0 +1,16 @@
1
+ import type { UnitScale } from './unit-scale/index.js';
2
+ export interface UnitFormatOptions {
3
+ unitScale?: UnitScale;
4
+ }
5
+ /**
6
+ * The base class for unit formats.
7
+ */
8
+ export declare abstract class UnitFormat<Options extends UnitFormatOptions = UnitFormatOptions> {
9
+ /**
10
+ * Formats a number value to a string.
11
+ * For nullish values or values that result in an exception being thrown, empty string is returned
12
+ */
13
+ format(value: number | undefined | null): string;
14
+ abstract resolvedOptions(): Required<Options>;
15
+ protected abstract tryFormat(number: number): string;
16
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The base class for unit formats.
3
+ */
4
+ export class UnitFormat {
5
+ /**
6
+ * Formats a number value to a string.
7
+ * For nullish values or values that result in an exception being thrown, empty string is returned
8
+ */
9
+ format(value) {
10
+ if (typeof value !== 'number') {
11
+ return '';
12
+ }
13
+ try {
14
+ return this.tryFormat(value);
15
+ }
16
+ catch {
17
+ return '';
18
+ }
19
+ }
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,OAAgB,UAAU;IAG5B;;;OAGG;IACI,MAAM,CAAC,KAAgC;QAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC5B,OAAO,EAAE,CAAC;QACd,CAAC;QAED,IAAI,CAAC;YACD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACL,OAAO,EAAE,CAAC;QACd,CAAC;IACL,CAAC;CAKJ","sourcesContent":["import type { UnitScale } from './unit-scale/index.js';\r\n\r\nexport interface UnitFormatOptions {\r\n unitScale?: UnitScale;\r\n}\r\n\r\n/**\r\n * The base class for unit formats.\r\n */\r\nexport abstract class UnitFormat<\r\n Options extends UnitFormatOptions = UnitFormatOptions\r\n> {\r\n /**\r\n * Formats a number value to a string.\r\n * For nullish values or values that result in an exception being thrown, empty string is returned\r\n */\r\n public format(value: number | undefined | null): string {\r\n if (typeof value !== 'number') {\r\n return '';\r\n }\r\n\r\n try {\r\n return this.tryFormat(value);\r\n } catch {\r\n return '';\r\n }\r\n }\r\n\r\n public abstract resolvedOptions(): Required<Options>;\r\n\r\n protected abstract tryFormat(number: number): string;\r\n}\r\n"]}
@@ -0,0 +1,16 @@
1
+ import type { ScaledUnitFormat } from '../scaled-unit-format/index.js';
2
+ export interface ScaledUnitFormatFactoryOptions {
3
+ readonly locale: string;
4
+ readonly intlNumberFormatOptions?: Intl.NumberFormatOptions;
5
+ }
6
+ type ScaledUnitFormatFactory = (scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions) => ScaledUnitFormat;
7
+ /**
8
+ * A unit that represents a scaled version of a base unit.
9
+ */
10
+ export declare class ScaledUnit {
11
+ readonly scaleFactor: number;
12
+ readonly scaledUnitFormatFactory: ScaledUnitFormatFactory;
13
+ constructor(scaleFactor: number, scaledUnitFormatFactory: ScaledUnitFormatFactory);
14
+ isBase(): boolean;
15
+ }
16
+ export {};
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A unit that represents a scaled version of a base unit.
3
+ */
4
+ export class ScaledUnit {
5
+ constructor(scaleFactor, scaledUnitFormatFactory) {
6
+ this.scaleFactor = scaleFactor;
7
+ this.scaledUnitFormatFactory = scaledUnitFormatFactory;
8
+ }
9
+ isBase() {
10
+ return this.scaleFactor === 1;
11
+ }
12
+ }
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/scaled-unit/index.ts"],"names":[],"mappings":"AAWA;;GAEG;AACH,MAAM,OAAO,UAAU;IACnB,YACoB,WAAmB,EACnB,uBAAgD;QADhD,gBAAW,GAAX,WAAW,CAAQ;QACnB,4BAAuB,GAAvB,uBAAuB,CAAyB;IACjE,CAAC;IAEG,MAAM;QACT,OAAO,IAAI,CAAC,WAAW,KAAK,CAAC,CAAC;IAClC,CAAC;CACJ","sourcesContent":["import type { ScaledUnitFormat } from '../scaled-unit-format/index.js';\r\n\r\nexport interface ScaledUnitFormatFactoryOptions {\r\n readonly locale: string;\r\n readonly intlNumberFormatOptions?: Intl.NumberFormatOptions;\r\n}\r\n\r\ntype ScaledUnitFormatFactory = (\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions\r\n) => ScaledUnitFormat;\r\n\r\n/**\r\n * A unit that represents a scaled version of a base unit.\r\n */\r\nexport class ScaledUnit {\r\n public constructor(\r\n public readonly scaleFactor: number,\r\n public readonly scaledUnitFormatFactory: ScaledUnitFormatFactory\r\n ) {}\r\n\r\n public isBase(): boolean {\r\n return this.scaleFactor === 1;\r\n }\r\n}\r\n"]}
@@ -0,0 +1,10 @@
1
+ import type { ScaledUnitFormatFactoryOptions } from '../scaled-unit/index.js';
2
+ /**
3
+ * A class that knows how to format a numeric value as a string that includes units.
4
+ */
5
+ export declare abstract class ScaledUnitFormat {
6
+ protected readonly locale: string;
7
+ protected readonly intlNumberFormatOptions?: Intl.NumberFormatOptions;
8
+ protected constructor(scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions);
9
+ abstract format(value: number): string;
10
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A class that knows how to format a numeric value as a string that includes units.
3
+ */
4
+ export class ScaledUnitFormat {
5
+ constructor(scaledUnitFormatFactoryOptions) {
6
+ this.locale = scaledUnitFormatFactoryOptions.locale;
7
+ this.intlNumberFormatOptions = scaledUnitFormatFactoryOptions.intlNumberFormatOptions;
8
+ }
9
+ }
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/scaled-unit-format/index.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,OAAgB,gBAAgB;IAIlC,YACI,8BAA8D;QAE9D,IAAI,CAAC,MAAM,GAAG,8BAA8B,CAAC,MAAM,CAAC;QACpD,IAAI,CAAC,uBAAuB,GAAG,8BAA8B,CAAC,uBAAuB,CAAC;IAC1F,CAAC;CAGJ","sourcesContent":["import type { ScaledUnitFormatFactoryOptions } from '../scaled-unit/index.js';\r\n\r\n/**\r\n * A class that knows how to format a numeric value as a string that includes units.\r\n */\r\nexport abstract class ScaledUnitFormat {\r\n protected readonly locale: string;\r\n protected readonly intlNumberFormatOptions?: Intl.NumberFormatOptions;\r\n\r\n protected constructor(\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions\r\n ) {\r\n this.locale = scaledUnitFormatFactoryOptions.locale;\r\n this.intlNumberFormatOptions = scaledUnitFormatFactoryOptions.intlNumberFormatOptions;\r\n }\r\n\r\n public abstract format(value: number): string;\r\n}\r\n"]}
@@ -0,0 +1,11 @@
1
+ import type { ScaledUnitFormatFactoryOptions } from '../../scaled-unit/index.js';
2
+ import { ScaledUnitFormat } from '../index.js';
3
+ /**
4
+ * A formatter for units that can be formatted/translated by Intl.NumberFormat
5
+ */
6
+ export declare class ScaledUnitFormatIntlNumberFormat extends ScaledUnitFormat {
7
+ private readonly formatter;
8
+ protected constructor(scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions, unitSpecificIntlNumberFormatOptions: Intl.NumberFormatOptions);
9
+ static createFactory(unitSpecificIntlNumberFormatOptions: Intl.NumberFormatOptions): (scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions) => ScaledUnitFormatIntlNumberFormat;
10
+ format(value: number): string;
11
+ }
@@ -0,0 +1,21 @@
1
+ import { ScaledUnitFormat } from '../index.js';
2
+ /**
3
+ * A formatter for units that can be formatted/translated by Intl.NumberFormat
4
+ */
5
+ export class ScaledUnitFormatIntlNumberFormat extends ScaledUnitFormat {
6
+ constructor(scaledUnitFormatFactoryOptions, unitSpecificIntlNumberFormatOptions) {
7
+ super(scaledUnitFormatFactoryOptions);
8
+ this.formatter = new Intl.NumberFormat(this.locale, {
9
+ ...unitSpecificIntlNumberFormatOptions,
10
+ // Application configured options override unit specific options
11
+ ...this.intlNumberFormatOptions
12
+ });
13
+ }
14
+ static createFactory(unitSpecificIntlNumberFormatOptions) {
15
+ return (scaledUnitFormatFactoryOptions) => new ScaledUnitFormatIntlNumberFormat(scaledUnitFormatFactoryOptions, unitSpecificIntlNumberFormatOptions);
16
+ }
17
+ format(value) {
18
+ return this.formatter.format(value);
19
+ }
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/scaled-unit-format/intl-number-format/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,gCAAiC,SAAQ,gBAAgB;IAGlE,YACI,8BAA8D,EAC9D,mCAA6D;QAE7D,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACtC,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE;YAChD,GAAG,mCAAmC;YACtC,gEAAgE;YAChE,GAAG,IAAI,CAAC,uBAAuB;SAClC,CAAC,CAAC;IACP,CAAC;IAEM,MAAM,CAAC,aAAa,CACvB,mCAA6D;QAE7D,OAAO,CACH,8BAA8D,EAC9B,EAAE,CAAC,IAAI,gCAAgC,CACvE,8BAA8B,EAC9B,mCAAmC,CACtC,CAAC;IACN,CAAC;IAEM,MAAM,CAAC,KAAa;QACvB,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;CACJ","sourcesContent":["import type { ScaledUnitFormatFactoryOptions } from '../../scaled-unit/index.js';\r\nimport { ScaledUnitFormat } from '../index.js';\r\n\r\n/**\r\n * A formatter for units that can be formatted/translated by Intl.NumberFormat\r\n */\r\nexport class ScaledUnitFormatIntlNumberFormat extends ScaledUnitFormat {\r\n private readonly formatter: Intl.NumberFormat;\r\n\r\n protected constructor(\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions,\r\n unitSpecificIntlNumberFormatOptions: Intl.NumberFormatOptions\r\n ) {\r\n super(scaledUnitFormatFactoryOptions);\r\n this.formatter = new Intl.NumberFormat(this.locale, {\r\n ...unitSpecificIntlNumberFormatOptions,\r\n // Application configured options override unit specific options\r\n ...this.intlNumberFormatOptions\r\n });\r\n }\r\n\r\n public static createFactory(\r\n unitSpecificIntlNumberFormatOptions: Intl.NumberFormatOptions\r\n ) {\r\n return (\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions\r\n ): ScaledUnitFormatIntlNumberFormat => new ScaledUnitFormatIntlNumberFormat(\r\n scaledUnitFormatFactoryOptions,\r\n unitSpecificIntlNumberFormatOptions\r\n );\r\n }\r\n\r\n public format(value: number): string {\r\n return this.formatter.format(value);\r\n }\r\n}\r\n"]}
@@ -0,0 +1,43 @@
1
+ import type { ScaledUnitFormatFactoryOptions } from '../../scaled-unit/index.js';
2
+ import { ScaledUnitFormat } from '../index.js';
3
+ /**
4
+ * Representations of a unit in a particular language
5
+ */
6
+ export declare class UnitTranslation {
7
+ readonly singular: string;
8
+ readonly plural: string;
9
+ readonly symbol: string;
10
+ constructor(singular: string, plural: string, symbol: string);
11
+ }
12
+ /**
13
+ * A map of locales of string format "[lang]" or "[lang]-[region]", for example "en" and / or "en-us", to UnitTranslation objects
14
+ */
15
+ export type UnitTranslations = ReadonlyMap<string, UnitTranslation>;
16
+ export interface ScaledUnitFormatManuallyTranslatedOptions {
17
+ /**
18
+ * Translations for the unit by locale string.
19
+ * The locale strings must be of the form [lang] or [lang]-[region], for example "en" and / or "en-us".
20
+ * Other subtags besides lang and region are not supported.
21
+ * Translations for "en" must be provided.
22
+ */
23
+ readonly unitTranslations: UnitTranslations;
24
+ /**
25
+ * String for prefix of this scaled unit, for example "k" (for kilo-).
26
+ * Assumed the same across languages.
27
+ * Base unit must use "", i.e. empty string, as the scaled prefix text.
28
+ */
29
+ readonly scaledPrefixText: string;
30
+ }
31
+ /**
32
+ * A formatter for units that are not supported by Intl.NumberFormat
33
+ */
34
+ export declare class ScaledUnitFormatManuallyTranslated extends ScaledUnitFormat {
35
+ private readonly pluralRules;
36
+ private readonly formatter;
37
+ private readonly unitTranslation;
38
+ private readonly scaledPrefixText;
39
+ protected constructor(scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions, { unitTranslations, scaledPrefixText }: ScaledUnitFormatManuallyTranslatedOptions);
40
+ static createFactory(scaledUnitFormatManuallyTranslatedOptions: ScaledUnitFormatManuallyTranslatedOptions): (scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions) => ScaledUnitFormatManuallyTranslated;
41
+ format(value: number): string;
42
+ private getTranslationToUse;
43
+ }
@@ -0,0 +1,63 @@
1
+ import { ScaledUnitFormat } from '../index.js';
2
+ /**
3
+ * Representations of a unit in a particular language
4
+ */
5
+ export class UnitTranslation {
6
+ constructor(singular, plural, symbol) {
7
+ this.singular = singular;
8
+ this.plural = plural;
9
+ this.symbol = symbol;
10
+ }
11
+ }
12
+ /**
13
+ * A formatter for units that are not supported by Intl.NumberFormat
14
+ */
15
+ export class ScaledUnitFormatManuallyTranslated extends ScaledUnitFormat {
16
+ constructor(scaledUnitFormatFactoryOptions, { unitTranslations, scaledPrefixText }) {
17
+ super(scaledUnitFormatFactoryOptions);
18
+ if (!unitTranslations.get('en')) {
19
+ throw new Error('English translations must exist with locale string "en"');
20
+ }
21
+ this.pluralRules = new Intl.PluralRules(this.locale);
22
+ this.formatter = new Intl.NumberFormat(this.locale, this.intlNumberFormatOptions);
23
+ this.unitTranslation = this.getTranslationToUse(unitTranslations, this.locale);
24
+ this.scaledPrefixText = scaledPrefixText;
25
+ }
26
+ static createFactory(scaledUnitFormatManuallyTranslatedOptions) {
27
+ return (scaledUnitFormatFactoryOptions) => new ScaledUnitFormatManuallyTranslated(scaledUnitFormatFactoryOptions, scaledUnitFormatManuallyTranslatedOptions);
28
+ }
29
+ format(value) {
30
+ const formatted = this.formatter.format(value);
31
+ // For non-base units (which are a scaled prefix text of empty string)
32
+ if (this.scaledPrefixText !== '') {
33
+ return `${formatted} ${this.scaledPrefixText}${this.unitTranslation.symbol}`;
34
+ }
35
+ // Some languages have more than two forms (singular/plural) of cardinal
36
+ // numbers, but we are treating anything other than the 'one' form as plural.
37
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules#description
38
+ //
39
+ // Because PluralRules.select() takes a number (not a string), it cannot differentiate
40
+ // between 1 and 1.0. When NumberFormat is configured to format with a set number
41
+ // of fractional digits, those fractional digits can have an effect on the pluralization
42
+ // of the unit. E.g. in English, it formats "1 byte" vs "1.0 bytes". Thus there is
43
+ // sometimes an inconsistency between unit pluralization for the same number, based
44
+ // on whether it's supported by NumberFormat, or manually translated.
45
+ const unitLabel = this.pluralRules.select(value) === 'one'
46
+ ? this.unitTranslation.singular
47
+ : this.unitTranslation.plural;
48
+ return `${formatted} ${unitLabel}`;
49
+ }
50
+ getTranslationToUse(unitTranslations, locale) {
51
+ const localeObject = new Intl.Locale(locale ?? 'en');
52
+ const language = localeObject.language;
53
+ const region = localeObject.region;
54
+ const regionSpecificMatchedTranslations = region
55
+ ? unitTranslations.get(`${language}-${region}`)
56
+ : undefined;
57
+ return (regionSpecificMatchedTranslations
58
+ ?? unitTranslations.get(language)
59
+ ?? unitTranslations.get('en'));
60
+ }
61
+ }
62
+ /* eslint-enable max-classes-per-file */
63
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/scaled-unit-format/manually-translated/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,eAAe;IACxB,YACoB,QAAgB,EAChB,MAAc,EACd,MAAc;QAFd,aAAQ,GAAR,QAAQ,CAAQ;QAChB,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAQ;IAC/B,CAAC;CACP;AAqBD;;GAEG;AACH,MAAM,OAAO,kCAAmC,SAAQ,gBAAgB;IAMpE,YACI,8BAA8D,EAC9D,EACI,gBAAgB,EAChB,gBAAgB,EACwB;QAE5C,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACtC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CACX,yDAAyD,CAC5D,CAAC;QACN,CAAC;QACD,IAAI,CAAC,WAAW,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACrD,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,CAAC,YAAY,CAClC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,uBAAuB,CAC/B,CAAC;QACF,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,mBAAmB,CAC3C,gBAAgB,EAChB,IAAI,CAAC,MAAM,CACd,CAAC;QACF,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC7C,CAAC;IAEM,MAAM,CAAC,aAAa,CACvB,yCAAoF;QAEpF,OAAO,CACH,8BAA8D,EAC5B,EAAE,CAAC,IAAI,kCAAkC,CAC3E,8BAA8B,EAC9B,yCAAyC,CAC5C,CAAC;IACN,CAAC;IAEM,MAAM,CAAC,KAAa;QACvB,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC/C,sEAAsE;QACtE,IAAI,IAAI,CAAC,gBAAgB,KAAK,EAAE,EAAE,CAAC;YAC/B,OAAO,GAAG,SAAS,IAAI,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC;QACjF,CAAC;QAED,wEAAwE;QACxE,6EAA6E;QAC7E,oHAAoH;QACpH,EAAE;QACF,sFAAsF;QACtF,iFAAiF;QACjF,wFAAwF;QACxF,kFAAkF;QAClF,mFAAmF;QACnF,qEAAqE;QACrE,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,KAAK;YACtD,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ;YAC/B,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC;QAClC,OAAO,GAAG,SAAS,IAAI,SAAS,EAAE,CAAC;IACvC,CAAC;IAEO,mBAAmB,CACvB,gBAAkC,EAClC,MAAc;QAEd,MAAM,YAAY,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,IAAI,CAAC,CAAC;QACrD,MAAM,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC;QACvC,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC;QACnC,MAAM,iCAAiC,GAAG,MAAM;YAC5C,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,QAAQ,IAAI,MAAM,EAAE,CAAC;YAC/C,CAAC,CAAC,SAAS,CAAC;QAChB,OAAO,CACH,iCAAiC;eAC9B,gBAAgB,CAAC,GAAG,CAAC,QAAQ,CAAC;eAC9B,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAE,CACjC,CAAC;IACN,CAAC;CACJ;AACD,wCAAwC","sourcesContent":["/* eslint-disable max-classes-per-file */\r\nimport type { ScaledUnitFormatFactoryOptions } from '../../scaled-unit/index.js';\r\nimport { ScaledUnitFormat } from '../index.js';\r\n\r\n/**\r\n * Representations of a unit in a particular language\r\n */\r\nexport class UnitTranslation {\r\n public constructor(\r\n public readonly singular: string,\r\n public readonly plural: string,\r\n public readonly symbol: string\r\n ) {}\r\n}\r\n/**\r\n * A map of locales of string format \"[lang]\" or \"[lang]-[region]\", for example \"en\" and / or \"en-us\", to UnitTranslation objects\r\n */\r\nexport type UnitTranslations = ReadonlyMap<string, UnitTranslation>;\r\nexport interface ScaledUnitFormatManuallyTranslatedOptions {\r\n /**\r\n * Translations for the unit by locale string.\r\n * The locale strings must be of the form [lang] or [lang]-[region], for example \"en\" and / or \"en-us\".\r\n * Other subtags besides lang and region are not supported.\r\n * Translations for \"en\" must be provided.\r\n */\r\n readonly unitTranslations: UnitTranslations;\r\n /**\r\n * String for prefix of this scaled unit, for example \"k\" (for kilo-).\r\n * Assumed the same across languages.\r\n * Base unit must use \"\", i.e. empty string, as the scaled prefix text.\r\n */\r\n readonly scaledPrefixText: string;\r\n}\r\n\r\n/**\r\n * A formatter for units that are not supported by Intl.NumberFormat\r\n */\r\nexport class ScaledUnitFormatManuallyTranslated extends ScaledUnitFormat {\r\n private readonly pluralRules: Intl.PluralRules;\r\n private readonly formatter: Intl.NumberFormat;\r\n private readonly unitTranslation: UnitTranslation;\r\n private readonly scaledPrefixText: string;\r\n\r\n protected constructor(\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions,\r\n {\r\n unitTranslations,\r\n scaledPrefixText\r\n }: ScaledUnitFormatManuallyTranslatedOptions\r\n ) {\r\n super(scaledUnitFormatFactoryOptions);\r\n if (!unitTranslations.get('en')) {\r\n throw new Error(\r\n 'English translations must exist with locale string \"en\"'\r\n );\r\n }\r\n this.pluralRules = new Intl.PluralRules(this.locale);\r\n this.formatter = new Intl.NumberFormat(\r\n this.locale,\r\n this.intlNumberFormatOptions\r\n );\r\n this.unitTranslation = this.getTranslationToUse(\r\n unitTranslations,\r\n this.locale\r\n );\r\n this.scaledPrefixText = scaledPrefixText;\r\n }\r\n\r\n public static createFactory(\r\n scaledUnitFormatManuallyTranslatedOptions: ScaledUnitFormatManuallyTranslatedOptions\r\n ) {\r\n return (\r\n scaledUnitFormatFactoryOptions: ScaledUnitFormatFactoryOptions\r\n ): ScaledUnitFormatManuallyTranslated => new ScaledUnitFormatManuallyTranslated(\r\n scaledUnitFormatFactoryOptions,\r\n scaledUnitFormatManuallyTranslatedOptions\r\n );\r\n }\r\n\r\n public format(value: number): string {\r\n const formatted = this.formatter.format(value);\r\n // For non-base units (which are a scaled prefix text of empty string)\r\n if (this.scaledPrefixText !== '') {\r\n return `${formatted} ${this.scaledPrefixText}${this.unitTranslation.symbol}`;\r\n }\r\n\r\n // Some languages have more than two forms (singular/plural) of cardinal\r\n // numbers, but we are treating anything other than the 'one' form as plural.\r\n // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules#description\r\n //\r\n // Because PluralRules.select() takes a number (not a string), it cannot differentiate\r\n // between 1 and 1.0. When NumberFormat is configured to format with a set number\r\n // of fractional digits, those fractional digits can have an effect on the pluralization\r\n // of the unit. E.g. in English, it formats \"1 byte\" vs \"1.0 bytes\". Thus there is\r\n // sometimes an inconsistency between unit pluralization for the same number, based\r\n // on whether it's supported by NumberFormat, or manually translated.\r\n const unitLabel = this.pluralRules.select(value) === 'one'\r\n ? this.unitTranslation.singular\r\n : this.unitTranslation.plural;\r\n return `${formatted} ${unitLabel}`;\r\n }\r\n\r\n private getTranslationToUse(\r\n unitTranslations: UnitTranslations,\r\n locale: string\r\n ): UnitTranslation {\r\n const localeObject = new Intl.Locale(locale ?? 'en');\r\n const language = localeObject.language;\r\n const region = localeObject.region;\r\n const regionSpecificMatchedTranslations = region\r\n ? unitTranslations.get(`${language}-${region}`)\r\n : undefined;\r\n return (\r\n regionSpecificMatchedTranslations\r\n ?? unitTranslations.get(language)\r\n ?? unitTranslations.get('en')!\r\n );\r\n }\r\n}\r\n/* eslint-enable max-classes-per-file */\r\n"]}
@@ -0,0 +1 @@
1
+ {"root":["../../src/index.ts","../../src/decimal/index.ts","../../src/default/index.ts","../../src/scaled-unit/index.ts","../../src/scaled-unit-format/index.ts","../../src/scaled-unit-format/intl-number-format/index.ts","../../src/scaled-unit-format/manually-translated/index.ts","../../src/scaled-unit-format/tests/scaled-unit-format-manually-translated.spec.ts","../../src/tests/scaled-unit-format-test.ts","../../src/tests/unit-format-decimal.spec.ts","../../src/tests/unit-format-default.spec.ts","../../src/unit-scale/index.ts","../../src/unit-scale/byte/index.ts","../../src/unit-scale/byte-1024/index.ts","../../src/unit-scale/celsius/index.ts","../../src/unit-scale/fahrenheit/index.ts","../../src/unit-scale/passthrough/index.ts","../../src/unit-scale/tests/unit-scale-byte-1024.spec.ts","../../src/unit-scale/tests/unit-scale-byte.spec.ts","../../src/unit-scale/tests/unit-scale-celsius.spec.ts","../../src/unit-scale/tests/unit-scale-fahrenheit.spec.ts","../../src/unit-scale/tests/unit-scale-volt.spec.ts","../../src/unit-scale/tests/unit-scale.spec.ts","../../src/unit-scale/utilities/metric-prefixes.ts","../../src/unit-scale/volt/index.ts"],"version":"5.8.3"}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Byte units (1000-based)
4
+ */
5
+ declare class UnitScaleByte extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScaleByte: UnitScaleByte;
9
+ export {};
@@ -0,0 +1,25 @@
1
+ import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';
2
+ import { ScaledUnit } from '../../scaled-unit/index.js';
3
+ import { UnitScale } from '../index.js';
4
+ const unitScaleByteConfig = [
5
+ [1000 ** 0, 'byte', 'long'],
6
+ [1000 ** 1, 'kilobyte', 'short'],
7
+ [1000 ** 2, 'megabyte', 'short'],
8
+ [1000 ** 3, 'gigabyte', 'short'],
9
+ [1000 ** 4, 'terabyte', 'short'],
10
+ [1000 ** 5, 'petabyte', 'short']
11
+ ];
12
+ /**
13
+ * Byte units (1000-based)
14
+ */
15
+ class UnitScaleByte extends UnitScale {
16
+ constructor() {
17
+ super(unitScaleByteConfig.map(([scaleFactor, unit, unitDisplay]) => new ScaledUnit(scaleFactor, ScaledUnitFormatIntlNumberFormat.createFactory({
18
+ style: 'unit',
19
+ unit,
20
+ unitDisplay
21
+ }))));
22
+ }
23
+ }
24
+ export const unitScaleByte = new UnitScaleByte();
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/byte/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,MAAM,sDAAsD,CAAC;AACxG,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,mBAAmB,GAAG;IACxB,CAAC,IAAI,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC;IAC3B,CAAC,IAAI,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC;IAChC,CAAC,IAAI,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC;IAChC,CAAC,IAAI,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC;IAChC,CAAC,IAAI,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC;IAChC,CAAC,IAAI,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEX;;GAEG;AACH,MAAM,aAAc,SAAQ,SAAS;IACjC;QACI,KAAK,CACD,mBAAmB,CAAC,GAAG,CACnB,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC,IAAI,UAAU,CAChD,WAAW,EACX,gCAAgC,CAAC,aAAa,CAAC;YAC3C,KAAK,EAAE,MAAM;YACb,IAAI;YACJ,WAAW;SACd,CAAC,CACL,CACJ,CACJ,CAAC;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC","sourcesContent":["import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';\r\nimport { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { UnitScale } from '../index.js';\r\n\r\nconst unitScaleByteConfig = [\r\n [1000 ** 0, 'byte', 'long'],\r\n [1000 ** 1, 'kilobyte', 'short'],\r\n [1000 ** 2, 'megabyte', 'short'],\r\n [1000 ** 3, 'gigabyte', 'short'],\r\n [1000 ** 4, 'terabyte', 'short'],\r\n [1000 ** 5, 'petabyte', 'short']\r\n] as const;\r\n\r\n/**\r\n * Byte units (1000-based)\r\n */\r\nclass UnitScaleByte extends UnitScale {\r\n public constructor() {\r\n super(\r\n unitScaleByteConfig.map(\r\n ([scaleFactor, unit, unitDisplay]) => new ScaledUnit(\r\n scaleFactor,\r\n ScaledUnitFormatIntlNumberFormat.createFactory({\r\n style: 'unit',\r\n unit,\r\n unitDisplay\r\n })\r\n )\r\n )\r\n );\r\n }\r\n}\r\n\r\nexport const unitScaleByte = new UnitScaleByte();\r\n"]}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Byte units (1024-based)
4
+ */
5
+ declare class UnitScaleByte1024 extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScaleByte1024: UnitScaleByte1024;
9
+ export {};
@@ -0,0 +1,31 @@
1
+ import { ScaledUnit } from '../../scaled-unit/index.js';
2
+ import { UnitScale } from '../index.js';
3
+ import { ScaledUnitFormatManuallyTranslated, UnitTranslation } from '../../scaled-unit-format/manually-translated/index.js';
4
+ const unitTranslations = new Map([
5
+ ['en', new UnitTranslation('byte', 'bytes', 'B')],
6
+ ['fr', new UnitTranslation('octet', 'octets', 'o')],
7
+ ['de', new UnitTranslation('Byte', 'Byte', 'B')],
8
+ ['ja', new UnitTranslation('バイト', 'バイト', 'B')],
9
+ ['zh', new UnitTranslation('字节', '字节', 'B')]
10
+ ]);
11
+ const byte1024Prefixes = [
12
+ [1024 ** 0, ''],
13
+ [1024 ** 1, 'Ki'],
14
+ [1024 ** 2, 'Mi'],
15
+ [1024 ** 3, 'Gi'],
16
+ [1024 ** 4, 'Ti'],
17
+ [1024 ** 5, 'Pi']
18
+ ];
19
+ /**
20
+ * Byte units (1024-based)
21
+ */
22
+ class UnitScaleByte1024 extends UnitScale {
23
+ constructor() {
24
+ super(byte1024Prefixes.map(([scaleFactor, scaledPrefixText]) => new ScaledUnit(scaleFactor, ScaledUnitFormatManuallyTranslated.createFactory({
25
+ unitTranslations,
26
+ scaledPrefixText
27
+ }))));
28
+ }
29
+ }
30
+ export const unitScaleByte1024 = new UnitScaleByte1024();
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/byte-1024/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EACH,kCAAkC,EAElC,eAAe,EAClB,MAAM,uDAAuD,CAAC;AAE/D,MAAM,gBAAgB,GAAqB,IAAI,GAAG,CAAC;IAC/C,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;IACnD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;IAChD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;CAC/C,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG;IACrB,CAAC,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;IACf,CAAC,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC;IACjB,CAAC,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC;IACjB,CAAC,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC;IACjB,CAAC,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC;IACjB,CAAC,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC;CACX,CAAC;AAEX;;GAEG;AACH,MAAM,iBAAkB,SAAQ,SAAS;IACrC;QACI,KAAK,CACD,gBAAgB,CAAC,GAAG,CAChB,CAAC,CAAC,WAAW,EAAE,gBAAgB,CAAC,EAAE,EAAE,CAAC,IAAI,UAAU,CAC/C,WAAW,EACX,kCAAkC,CAAC,aAAa,CAAC;YAC7C,gBAAgB;YAChB,gBAAgB;SACnB,CAAC,CACL,CACJ,CACJ,CAAC;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,EAAE,CAAC","sourcesContent":["import { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { UnitScale } from '../index.js';\r\nimport {\r\n ScaledUnitFormatManuallyTranslated,\r\n type UnitTranslations,\r\n UnitTranslation\r\n} from '../../scaled-unit-format/manually-translated/index.js';\r\n\r\nconst unitTranslations: UnitTranslations = new Map([\r\n ['en', new UnitTranslation('byte', 'bytes', 'B')],\r\n ['fr', new UnitTranslation('octet', 'octets', 'o')],\r\n ['de', new UnitTranslation('Byte', 'Byte', 'B')],\r\n ['ja', new UnitTranslation('バイト', 'バイト', 'B')],\r\n ['zh', new UnitTranslation('字节', '字节', 'B')]\r\n]);\r\n\r\nconst byte1024Prefixes = [\r\n [1024 ** 0, ''],\r\n [1024 ** 1, 'Ki'],\r\n [1024 ** 2, 'Mi'],\r\n [1024 ** 3, 'Gi'],\r\n [1024 ** 4, 'Ti'],\r\n [1024 ** 5, 'Pi']\r\n] as const;\r\n\r\n/**\r\n * Byte units (1024-based)\r\n */\r\nclass UnitScaleByte1024 extends UnitScale {\r\n public constructor() {\r\n super(\r\n byte1024Prefixes.map(\r\n ([scaleFactor, scaledPrefixText]) => new ScaledUnit(\r\n scaleFactor,\r\n ScaledUnitFormatManuallyTranslated.createFactory({\r\n unitTranslations,\r\n scaledPrefixText\r\n })\r\n )\r\n )\r\n );\r\n }\r\n}\r\n\r\nexport const unitScaleByte1024 = new UnitScaleByte1024();\r\n"]}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Degrees Celsius units
4
+ */
5
+ declare class UnitScaleCelsius extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScaleCelsius: UnitScaleCelsius;
9
+ export {};
@@ -0,0 +1,18 @@
1
+ import { ScaledUnit } from '../../scaled-unit/index.js';
2
+ import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';
3
+ import { UnitScale } from '../index.js';
4
+ const unitScaleCelsiusConfig = [[1, 'celsius', 'short']];
5
+ /**
6
+ * Degrees Celsius units
7
+ */
8
+ class UnitScaleCelsius extends UnitScale {
9
+ constructor() {
10
+ super(unitScaleCelsiusConfig.map(([scaleFactor, unit, unitDisplay]) => new ScaledUnit(scaleFactor, ScaledUnitFormatIntlNumberFormat.createFactory({
11
+ style: 'unit',
12
+ unit,
13
+ unitDisplay
14
+ }))));
15
+ }
16
+ }
17
+ export const unitScaleCelsius = new UnitScaleCelsius();
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/celsius/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,gCAAgC,EAAE,MAAM,sDAAsD,CAAC;AACxG,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,sBAAsB,GAAG,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CAAU,CAAC;AAElE;;GAEG;AACH,MAAM,gBAAiB,SAAQ,SAAS;IACpC;QACI,KAAK,CACD,sBAAsB,CAAC,GAAG,CACtB,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC,IAAI,UAAU,CAChD,WAAW,EACX,gCAAgC,CAAC,aAAa,CAAC;YAC3C,KAAK,EAAE,MAAM;YACb,IAAI;YACJ,WAAW;SACd,CAAC,CACL,CACJ,CACJ,CAAC;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC","sourcesContent":["import { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';\r\nimport { UnitScale } from '../index.js';\r\n\r\nconst unitScaleCelsiusConfig = [[1, 'celsius', 'short']] as const;\r\n\r\n/**\r\n * Degrees Celsius units\r\n */\r\nclass UnitScaleCelsius extends UnitScale {\r\n public constructor() {\r\n super(\r\n unitScaleCelsiusConfig.map(\r\n ([scaleFactor, unit, unitDisplay]) => new ScaledUnit(\r\n scaleFactor,\r\n ScaledUnitFormatIntlNumberFormat.createFactory({\r\n style: 'unit',\r\n unit,\r\n unitDisplay\r\n })\r\n )\r\n )\r\n );\r\n }\r\n}\r\n\r\nexport const unitScaleCelsius = new UnitScaleCelsius();\r\n"]}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Degrees Fahrenheit units
4
+ */
5
+ declare class UnitScaleFahrenheit extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScaleFahrenheit: UnitScaleFahrenheit;
9
+ export {};
@@ -0,0 +1,18 @@
1
+ import { ScaledUnit } from '../../scaled-unit/index.js';
2
+ import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';
3
+ import { UnitScale } from '../index.js';
4
+ const unitScaleFahrenheitConfig = [[1, 'fahrenheit', 'short']];
5
+ /**
6
+ * Degrees Fahrenheit units
7
+ */
8
+ class UnitScaleFahrenheit extends UnitScale {
9
+ constructor() {
10
+ super(unitScaleFahrenheitConfig.map(([scaleFactor, unit, unitDisplay]) => new ScaledUnit(scaleFactor, ScaledUnitFormatIntlNumberFormat.createFactory({
11
+ style: 'unit',
12
+ unit,
13
+ unitDisplay
14
+ }))));
15
+ }
16
+ }
17
+ export const unitScaleFahrenheit = new UnitScaleFahrenheit();
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/fahrenheit/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,gCAAgC,EAAE,MAAM,sDAAsD,CAAC;AACxG,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,yBAAyB,GAAG,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,CAAU,CAAC;AAExE;;GAEG;AACH,MAAM,mBAAoB,SAAQ,SAAS;IACvC;QACI,KAAK,CACD,yBAAyB,CAAC,GAAG,CACzB,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC,IAAI,UAAU,CAChD,WAAW,EACX,gCAAgC,CAAC,aAAa,CAAC;YAC3C,KAAK,EAAE,MAAM;YACb,IAAI;YACJ,WAAW;SACd,CAAC,CACL,CACJ,CACJ,CAAC;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,mBAAmB,EAAE,CAAC","sourcesContent":["import { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';\r\nimport { UnitScale } from '../index.js';\r\n\r\nconst unitScaleFahrenheitConfig = [[1, 'fahrenheit', 'short']] as const;\r\n\r\n/**\r\n * Degrees Fahrenheit units\r\n */\r\nclass UnitScaleFahrenheit extends UnitScale {\r\n public constructor() {\r\n super(\r\n unitScaleFahrenheitConfig.map(\r\n ([scaleFactor, unit, unitDisplay]) => new ScaledUnit(\r\n scaleFactor,\r\n ScaledUnitFormatIntlNumberFormat.createFactory({\r\n style: 'unit',\r\n unit,\r\n unitDisplay\r\n })\r\n )\r\n )\r\n );\r\n }\r\n}\r\n\r\nexport const unitScaleFahrenheit = new UnitScaleFahrenheit();\r\n"]}
@@ -0,0 +1,15 @@
1
+ import type { ScaledUnit } from '../scaled-unit/index.js';
2
+ interface ScaledNumber {
3
+ readonly scaledValue: number;
4
+ readonly scaledUnit: ScaledUnit;
5
+ }
6
+ /**
7
+ * A unit scale consisting of a set of scaled units.
8
+ */
9
+ export declare abstract class UnitScale {
10
+ readonly supportedScaledUnits: readonly ScaledUnit[];
11
+ readonly baseScaledUnit: ScaledUnit;
12
+ constructor(supportedScaledUnits: readonly ScaledUnit[]);
13
+ scaleNumber(number: number): ScaledNumber;
14
+ }
15
+ export {};
@@ -0,0 +1,49 @@
1
+ /**
2
+ * A unit scale consisting of a set of scaled units.
3
+ */
4
+ export class UnitScale {
5
+ constructor(supportedScaledUnits) {
6
+ this.supportedScaledUnits = supportedScaledUnits;
7
+ const unitsSorted = supportedScaledUnits.every((curr, i, arr) => i === 0 || arr[i - 1].scaleFactor < curr.scaleFactor);
8
+ if (!unitsSorted) {
9
+ throw new Error('Supported scaled units must have unique and ordered scale factors');
10
+ }
11
+ const baseScaledUnit = supportedScaledUnits.find(x => x.isBase());
12
+ if (!baseScaledUnit) {
13
+ throw new Error('Supported scaled units must include a base scaled unit (scale factor=1)');
14
+ }
15
+ this.supportedScaledUnits = supportedScaledUnits;
16
+ this.baseScaledUnit = baseScaledUnit;
17
+ }
18
+ // Note that for the sake of reducing complexity in the implementation,
19
+ // we do NOT consider the effects of rounding when picking the unit to
20
+ // use for a given value. If formatting results in rounding, a value
21
+ // may be shown with an unexpected unit. Examples:
22
+ // - 999 bytes with two significant digits => "1000 bytes" (instead of "1 kB")
23
+ // - 0.00000000000000001 volts (= 0.01 fV) with one fractional digit => "0 fV" (instead of "0 volts")
24
+ scaleNumber(number) {
25
+ const magnitude = Math.abs(number);
26
+ const onlyBaseScaledUnit = this.supportedScaledUnits.length === 1;
27
+ if (onlyBaseScaledUnit
28
+ || magnitude === 0
29
+ || magnitude === Infinity
30
+ || Number.isNaN(magnitude)) {
31
+ return { scaledValue: number, scaledUnit: this.baseScaledUnit };
32
+ }
33
+ for (let i = this.supportedScaledUnits.length - 1; i >= 0; i -= 1) {
34
+ const scaledUnit = this.supportedScaledUnits[i];
35
+ if (magnitude / scaledUnit.scaleFactor >= 1) {
36
+ return {
37
+ scaledValue: number / scaledUnit.scaleFactor,
38
+ scaledUnit
39
+ };
40
+ }
41
+ }
42
+ const smallestUnit = this.supportedScaledUnits[0];
43
+ return {
44
+ scaledValue: number / smallestUnit.scaleFactor,
45
+ scaledUnit: smallestUnit
46
+ };
47
+ }
48
+ }
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/unit-scale/index.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,OAAgB,SAAS;IAG3B,YACoB,oBAA2C;QAA3C,yBAAoB,GAApB,oBAAoB,CAAuB;QAE3D,MAAM,WAAW,GAAG,oBAAoB,CAAC,KAAK,CAC1C,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAE,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAC1E,CAAC;QACF,IAAI,CAAC,WAAW,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CACX,mEAAmE,CACtE,CAAC;QACN,CAAC;QACD,MAAM,cAAc,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;QAClE,IAAI,CAAC,cAAc,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CACX,yEAAyE,CAC5E,CAAC;QACN,CAAC;QACD,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACjD,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACzC,CAAC;IAED,uEAAuE;IACvE,sEAAsE;IACtE,oEAAoE;IACpE,kDAAkD;IAClD,8EAA8E;IAC9E,qGAAqG;IAC9F,WAAW,CAAC,MAAc;QAC7B,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,kBAAkB,GAAG,IAAI,CAAC,oBAAoB,CAAC,MAAM,KAAK,CAAC,CAAC;QAClE,IACI,kBAAkB;eACf,SAAS,KAAK,CAAC;eACf,SAAS,KAAK,QAAQ;eACtB,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,EAC5B,CAAC;YACC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC;QACpE,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,oBAAoB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YAChE,MAAM,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAE,CAAC;YACjD,IAAI,SAAS,GAAG,UAAU,CAAC,WAAW,IAAI,CAAC,EAAE,CAAC;gBAC1C,OAAO;oBACH,WAAW,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW;oBAC5C,UAAU;iBACb,CAAC;YACN,CAAC;QACL,CAAC;QACD,MAAM,YAAY,GAAG,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAE,CAAC;QACnD,OAAO;YACH,WAAW,EAAE,MAAM,GAAG,YAAY,CAAC,WAAW;YAC9C,UAAU,EAAE,YAAY;SAC3B,CAAC;IACN,CAAC;CACJ","sourcesContent":["import type { ScaledUnit } from '../scaled-unit/index.js';\r\n\r\ninterface ScaledNumber {\r\n readonly scaledValue: number;\r\n readonly scaledUnit: ScaledUnit;\r\n}\r\n\r\n/**\r\n * A unit scale consisting of a set of scaled units.\r\n */\r\nexport abstract class UnitScale {\r\n public readonly baseScaledUnit: ScaledUnit;\r\n\r\n public constructor(\r\n public readonly supportedScaledUnits: readonly ScaledUnit[]\r\n ) {\r\n const unitsSorted = supportedScaledUnits.every(\r\n (curr, i, arr) => i === 0 || arr[i - 1]!.scaleFactor < curr.scaleFactor\r\n );\r\n if (!unitsSorted) {\r\n throw new Error(\r\n 'Supported scaled units must have unique and ordered scale factors'\r\n );\r\n }\r\n const baseScaledUnit = supportedScaledUnits.find(x => x.isBase());\r\n if (!baseScaledUnit) {\r\n throw new Error(\r\n 'Supported scaled units must include a base scaled unit (scale factor=1)'\r\n );\r\n }\r\n this.supportedScaledUnits = supportedScaledUnits;\r\n this.baseScaledUnit = baseScaledUnit;\r\n }\r\n\r\n // Note that for the sake of reducing complexity in the implementation,\r\n // we do NOT consider the effects of rounding when picking the unit to\r\n // use for a given value. If formatting results in rounding, a value\r\n // may be shown with an unexpected unit. Examples:\r\n // - 999 bytes with two significant digits => \"1000 bytes\" (instead of \"1 kB\")\r\n // - 0.00000000000000001 volts (= 0.01 fV) with one fractional digit => \"0 fV\" (instead of \"0 volts\")\r\n public scaleNumber(number: number): ScaledNumber {\r\n const magnitude = Math.abs(number);\r\n const onlyBaseScaledUnit = this.supportedScaledUnits.length === 1;\r\n if (\r\n onlyBaseScaledUnit\r\n || magnitude === 0\r\n || magnitude === Infinity\r\n || Number.isNaN(magnitude)\r\n ) {\r\n return { scaledValue: number, scaledUnit: this.baseScaledUnit };\r\n }\r\n for (let i = this.supportedScaledUnits.length - 1; i >= 0; i -= 1) {\r\n const scaledUnit = this.supportedScaledUnits[i]!;\r\n if (magnitude / scaledUnit.scaleFactor >= 1) {\r\n return {\r\n scaledValue: number / scaledUnit.scaleFactor,\r\n scaledUnit\r\n };\r\n }\r\n }\r\n const smallestUnit = this.supportedScaledUnits[0]!;\r\n return {\r\n scaledValue: number / smallestUnit.scaleFactor,\r\n scaledUnit: smallestUnit\r\n };\r\n }\r\n}\r\n"]}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Unit scale that is used to passthrough a number without applying scaling or units
4
+ */
5
+ declare class UnitScalePassthrough extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScalePassthrough: UnitScalePassthrough;
9
+ export {};
@@ -0,0 +1,15 @@
1
+ import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';
2
+ import { ScaledUnit } from '../../scaled-unit/index.js';
3
+ import { UnitScale } from '../index.js';
4
+ /**
5
+ * Unit scale that is used to passthrough a number without applying scaling or units
6
+ */
7
+ class UnitScalePassthrough extends UnitScale {
8
+ constructor() {
9
+ super([
10
+ new ScaledUnit(10 ** 0, ScaledUnitFormatIntlNumberFormat.createFactory({}))
11
+ ]);
12
+ }
13
+ }
14
+ export const unitScalePassthrough = new UnitScalePassthrough();
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/passthrough/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,MAAM,sDAAsD,CAAC;AACxG,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC;;GAEG;AACH,MAAM,oBAAqB,SAAQ,SAAS;IACxC;QACI,KAAK,CAAC;YACF,IAAI,UAAU,CACV,EAAE,IAAI,CAAC,EACP,gCAAgC,CAAC,aAAa,CAAC,EAAE,CAAC,CACrD;SACJ,CAAC,CAAC;IACP,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,oBAAoB,EAAE,CAAC","sourcesContent":["import { ScaledUnitFormatIntlNumberFormat } from '../../scaled-unit-format/intl-number-format/index.js';\r\nimport { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { UnitScale } from '../index.js';\r\n\r\n/**\r\n * Unit scale that is used to passthrough a number without applying scaling or units\r\n */\r\nclass UnitScalePassthrough extends UnitScale {\r\n public constructor() {\r\n super([\r\n new ScaledUnit(\r\n 10 ** 0,\r\n ScaledUnitFormatIntlNumberFormat.createFactory({})\r\n )\r\n ]);\r\n }\r\n}\r\n\r\nexport const unitScalePassthrough = new UnitScalePassthrough();\r\n"]}
@@ -0,0 +1 @@
1
+ export declare const metricPrefixes: readonly [readonly [number, "f"], readonly [number, "p"], readonly [number, "n"], readonly [number, "μ"], readonly [number, "m"], readonly [number, "c"], readonly [number, "d"], readonly [number, ""], readonly [number, "k"], readonly [number, "M"], readonly [number, "G"], readonly [number, "T"], readonly [number, "P"], readonly [number, "E"]];
@@ -0,0 +1,17 @@
1
+ export const metricPrefixes = [
2
+ [10 ** -15, 'f'],
3
+ [10 ** -12, 'p'],
4
+ [10 ** -9, 'n'],
5
+ [10 ** -6, 'μ'],
6
+ [10 ** -3, 'm'],
7
+ [10 ** -2, 'c'],
8
+ [10 ** -1, 'd'],
9
+ [10 ** 0, ''],
10
+ [10 ** 3, 'k'],
11
+ [10 ** 6, 'M'],
12
+ [10 ** 9, 'G'],
13
+ [10 ** 12, 'T'],
14
+ [10 ** 15, 'P'],
15
+ [10 ** 18, 'E']
16
+ ];
17
+ //# sourceMappingURL=metric-prefixes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metric-prefixes.js","sourceRoot":"","sources":["../../../../src/unit-scale/utilities/metric-prefixes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,cAAc,GAAG;IAC1B,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC;IAChB,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC;IAChB,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC;IACb,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC;IACd,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC;IACd,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC;IACd,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,CAAC;IACf,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,CAAC;CACT,CAAC","sourcesContent":["export const metricPrefixes = [\r\n [10 ** -15, 'f'],\r\n [10 ** -12, 'p'],\r\n [10 ** -9, 'n'],\r\n [10 ** -6, 'μ'],\r\n [10 ** -3, 'm'],\r\n [10 ** -2, 'c'],\r\n [10 ** -1, 'd'],\r\n [10 ** 0, ''],\r\n [10 ** 3, 'k'],\r\n [10 ** 6, 'M'],\r\n [10 ** 9, 'G'],\r\n [10 ** 12, 'T'],\r\n [10 ** 15, 'P'],\r\n [10 ** 18, 'E']\r\n] as const;\r\n"]}
@@ -0,0 +1,9 @@
1
+ import { UnitScale } from '../index.js';
2
+ /**
3
+ * Voltage unit scale
4
+ */
5
+ declare class UnitScaleVolt extends UnitScale {
6
+ constructor();
7
+ }
8
+ export declare const unitScaleVolt: UnitScaleVolt;
9
+ export {};
@@ -0,0 +1,24 @@
1
+ import { ScaledUnit } from '../../scaled-unit/index.js';
2
+ import { UnitScale } from '../index.js';
3
+ import { metricPrefixes } from '../utilities/metric-prefixes.js';
4
+ import { ScaledUnitFormatManuallyTranslated, UnitTranslation } from '../../scaled-unit-format/manually-translated/index.js';
5
+ const unitTranslations = new Map([
6
+ ['en', new UnitTranslation('volt', 'volts', 'V')],
7
+ ['fr', new UnitTranslation('volt', 'volts', 'V')],
8
+ ['de', new UnitTranslation('Volt', 'Volt', 'V')],
9
+ ['ja', new UnitTranslation('ボルト', 'ボルト', 'V')],
10
+ ['zh', new UnitTranslation('伏特', '伏特', 'V')]
11
+ ]);
12
+ /**
13
+ * Voltage unit scale
14
+ */
15
+ class UnitScaleVolt extends UnitScale {
16
+ constructor() {
17
+ super(metricPrefixes.map(([scaleFactor, scaledPrefixText]) => new ScaledUnit(scaleFactor, ScaledUnitFormatManuallyTranslated.createFactory({
18
+ unitTranslations,
19
+ scaledPrefixText
20
+ }))));
21
+ }
22
+ }
23
+ export const unitScaleVolt = new UnitScaleVolt();
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/unit-scale/volt/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EACH,kCAAkC,EAElC,eAAe,EAClB,MAAM,uDAAuD,CAAC;AAE/D,MAAM,gBAAgB,GAAqB,IAAI,GAAG,CAAC;IAC/C,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;IAChD,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC,IAAI,EAAE,IAAI,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;CAC/C,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,aAAc,SAAQ,SAAS;IACjC;QACI,KAAK,CACD,cAAc,CAAC,GAAG,CACd,CAAC,CAAC,WAAW,EAAE,gBAAgB,CAAC,EAAE,EAAE,CAAC,IAAI,UAAU,CAC/C,WAAW,EACX,kCAAkC,CAAC,aAAa,CAAC;YAC7C,gBAAgB;YAChB,gBAAgB;SACnB,CAAC,CACL,CACJ,CACJ,CAAC;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC","sourcesContent":["import { ScaledUnit } from '../../scaled-unit/index.js';\r\nimport { UnitScale } from '../index.js';\r\nimport { metricPrefixes } from '../utilities/metric-prefixes.js';\r\nimport {\r\n ScaledUnitFormatManuallyTranslated,\r\n type UnitTranslations,\r\n UnitTranslation\r\n} from '../../scaled-unit-format/manually-translated/index.js';\r\n\r\nconst unitTranslations: UnitTranslations = new Map([\r\n ['en', new UnitTranslation('volt', 'volts', 'V')],\r\n ['fr', new UnitTranslation('volt', 'volts', 'V')],\r\n ['de', new UnitTranslation('Volt', 'Volt', 'V')],\r\n ['ja', new UnitTranslation('ボルト', 'ボルト', 'V')],\r\n ['zh', new UnitTranslation('伏特', '伏特', 'V')]\r\n]);\r\n\r\n/**\r\n * Voltage unit scale\r\n */\r\nclass UnitScaleVolt extends UnitScale {\r\n public constructor() {\r\n super(\r\n metricPrefixes.map(\r\n ([scaleFactor, scaledPrefixText]) => new ScaledUnit(\r\n scaleFactor,\r\n ScaledUnitFormatManuallyTranslated.createFactory({\r\n unitTranslations,\r\n scaledPrefixText\r\n })\r\n )\r\n )\r\n );\r\n }\r\n}\r\n\r\nexport const unitScaleVolt = new UnitScaleVolt();\r\n"]}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@ni/unit-format",
3
+ "version": "0.0.1",
4
+ "description": "NumberFormat aligned library to format numbers with scaled units.",
5
+ "keywords": [
6
+ "units", "formatting", "nimble", "engineering", "metric", "si", "volts", "degrees", "bytes"
7
+ ],
8
+ "scripts": {
9
+ "build": "tsc -b",
10
+ "lint": "eslint .",
11
+ "format": "eslint . --fix",
12
+ "pack": "npm pack",
13
+ "invoke-publish": "npm publish",
14
+ "tdd": "npm run build && npm run test",
15
+ "test": "npm run test:node && npm run test:browser",
16
+ "test:node": "jasmine",
17
+ "test:browser": "karma start karma.conf.cjs --browsers=ChromeHeadlessOpt --single-run",
18
+ "test:browser:debugger": "karma start karma.conf.cjs --browsers=ChromeDebugging"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/ni/nimble.git"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "type": "module",
28
+ "exports": {
29
+ "./package.json": "./package.json",
30
+ ".": {
31
+ "types": "./dist/esm/index.d.ts",
32
+ "import": "./dist/esm/index.js"
33
+ },
34
+ "./*": {
35
+ "types": "./dist/esm/*.d.ts",
36
+ "import": "./dist/esm/*/index.js"
37
+ }
38
+ },
39
+ "typesVersions": {
40
+ "*": {
41
+ "*": [
42
+ "dist/esm/*",
43
+ "dist/esm/*/index.d.ts"
44
+ ]
45
+ }
46
+ },
47
+ "author": "National Instruments",
48
+ "license": "MIT",
49
+ "devDependencies": {
50
+ "@ni/jasmine-parameterized": "^1.0.7",
51
+ "@ni-private/eslint-config-nimble": "*",
52
+ "@types/jasmine": "^5.1.4",
53
+ "jasmine": "^5.1.0",
54
+ "jasmine-core": "^5.1.2",
55
+ "karma": "^6.3.0",
56
+ "karma-chrome-launcher": "^3.1.0",
57
+ "karma-jasmine": "^5.1.0",
58
+ "karma-jasmine-html-reporter": "^2.0.0",
59
+ "karma-spec-reporter": "^0.0.36",
60
+ "karma-vite": "^1.0.5",
61
+ "playwright": "1.54.1",
62
+ "typescript": "~5.8.3"
63
+ }
64
+ }