@@ -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 }
0 commit comments