Skip to content

Commit 4533950

Browse files
committed
Add "unit" option to "number-format"
1 parent 8342bc0 commit 4533950

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/expression/definitions/number_format.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ export class NumberFormat implements Expression {
1010
number: Expression;
1111
locale: Expression | null; // BCP 47 language tag
1212
currency: Expression | null; // ISO 4217 currency code, required if style=currency
13+
unit: Expression | null; // CLDR or ECMA-402 unit specifier, required if style=unit
1314
minFractionDigits: Expression | null; // Default 0
1415
maxFractionDigits: Expression | null; // Default 3
1516

1617
constructor(number: Expression,
1718
locale: Expression | null,
1819
currency: Expression | null,
20+
unit: Expression | null,
1921
minFractionDigits: Expression | null,
2022
maxFractionDigits: Expression | null) {
2123
this.type = StringType;
2224
this.number = number;
2325
this.locale = locale;
2426
this.currency = currency;
27+
this.unit = unit;
2528
this.minFractionDigits = minFractionDigits;
2629
this.maxFractionDigits = maxFractionDigits;
2730
}
@@ -49,6 +52,16 @@ export class NumberFormat implements Expression {
4952
if (!currency) return null;
5053
}
5154

55+
let unit = null;
56+
if (options['unit']) {
57+
unit = context.parse(options['unit'], 1, StringType);
58+
if (!unit) return null;
59+
}
60+
61+
if (currency && unit) {
62+
return context.error('NumberFormat options `currency` and `unit` are mutually exclusive') as null;
63+
}
64+
5265
let minFractionDigits = null;
5366
if (options['min-fraction-digits']) {
5467
minFractionDigits = context.parse(options['min-fraction-digits'], 1, NumberType);
@@ -61,14 +74,15 @@ export class NumberFormat implements Expression {
6174
if (!maxFractionDigits) return null;
6275
}
6376

64-
return new NumberFormat(number, locale, currency, minFractionDigits, maxFractionDigits);
77+
return new NumberFormat(number, locale, currency, unit, minFractionDigits, maxFractionDigits);
6578
}
6679

6780
evaluate(ctx: EvaluationContext) {
6881
return new Intl.NumberFormat(this.locale ? this.locale.evaluate(ctx) : [],
6982
{
70-
style: this.currency ? 'currency' : 'decimal',
83+
style: this.currency ? 'currency' : this.unit ? 'unit' : 'decimal',
7184
currency: this.currency ? this.currency.evaluate(ctx) : undefined,
85+
unit: this.unit ? this.unit.evaluate(ctx) : undefined,
7286
minimumFractionDigits: this.minFractionDigits ? this.minFractionDigits.evaluate(ctx) : undefined,
7387
maximumFractionDigits: this.maxFractionDigits ? this.maxFractionDigits.evaluate(ctx) : undefined,
7488
}).format(this.number.evaluate(ctx));
@@ -82,6 +96,9 @@ export class NumberFormat implements Expression {
8296
if (this.currency) {
8397
fn(this.currency);
8498
}
99+
if (this.unit) {
100+
fn(this.unit);
101+
}
85102
if (this.minFractionDigits) {
86103
fn(this.minFractionDigits);
87104
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"expression": [
3+
"number-format",
4+
1234.567,
5+
{
6+
"locale": [
7+
"get",
8+
"locale"
9+
],
10+
"unit": [
11+
"get",
12+
"unit"
13+
]
14+
}
15+
],
16+
"inputs": [
17+
[
18+
{},
19+
{
20+
"properties": {
21+
"locale": "en-US",
22+
"unit": "meter"
23+
}
24+
}
25+
],
26+
[
27+
{},
28+
{
29+
"properties": {
30+
"locale": "en-US",
31+
"unit": "celsius"
32+
}
33+
}
34+
],
35+
[
36+
{},
37+
{
38+
"properties": {
39+
"locale": "en-US",
40+
"unit": "kilobyte"
41+
}
42+
}
43+
]
44+
],
45+
"expected": {
46+
"compiled": {
47+
"result": "success",
48+
"isFeatureConstant": false,
49+
"isZoomConstant": true,
50+
"type": "string"
51+
},
52+
"outputs": [
53+
"1,234.567 m",
54+
"1,234.567°C",
55+
"1,234.567 kB"
56+
]
57+
}
58+
}

0 commit comments

Comments
 (0)