2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- import 'package:js/js.dart' ;
6
- import 'package:js/js_util.dart' ;
5
+ import 'dart:js_interop' ;
7
6
8
7
import '../locale/locale.dart' ;
9
8
import '../options.dart' ;
@@ -18,16 +17,14 @@ NumberFormatImpl? getNumberFormatterECMA(
18
17
_NumberFormatECMA .tryToBuild (locale, options, localeMatcher);
19
18
20
19
@JS ('Intl.NumberFormat' )
21
- class _NumberFormatJS {
22
- external factory _NumberFormatJS ([List <String > locale, Object options]);
23
- external String format (Object num );
24
- }
20
+ extension type NumberFormat ._(JSObject _) implements JSObject {
21
+ external factory NumberFormat ([JSArray <JSString > locale, JSAny options]);
22
+ external String format (JSAny num );
25
23
26
- @JS ('Intl.NumberFormat.supportedLocalesOf' )
27
- external List <String > _supportedLocalesOfJS (
28
- List <String > listOfLocales, [
29
- Object options,
30
- ]);
24
+ external static JSArray <JSString > supportedLocalesOf (
25
+ JSArray <JSString > locales,
26
+ [JSAny options]);
27
+ }
31
28
32
29
class _NumberFormatECMA extends NumberFormatImpl {
33
30
_NumberFormatECMA (super .locale, super .options);
@@ -47,73 +44,72 @@ class _NumberFormatECMA extends NumberFormatImpl {
47
44
LocaleMatcher localeMatcher,
48
45
Locale locale,
49
46
) {
50
- final o = newObject <Object >();
51
- setProperty (o, 'localeMatcher' , localeMatcher.jsName);
52
- return List <dynamic >.from (
53
- _supportedLocalesOfJS ([locale.toLanguageTag ()], o))
47
+ final o = {
48
+ 'localeMatcher' : localeMatcher.jsName,
49
+ }.jsify ()! ;
50
+ return NumberFormat .supportedLocalesOf (
51
+ [locale.toLanguageTag ().toJS].toJS, o)
52
+ .toDart
54
53
.whereType <String >()
55
54
.map (Locale .parse)
56
55
.toList ();
57
56
}
58
57
59
58
@override
60
59
String formatImpl (Object number) {
61
- final numberFormatJS = _NumberFormatJS (
62
- [locale.toLanguageTag ()] ,
60
+ final numberFormatJS = NumberFormat (
61
+ [locale.toLanguageTag ().toJS].toJS ,
63
62
options.toJsOptions (),
64
63
);
65
- return numberFormatJS.format (number);
64
+ return numberFormatJS.format (number. jsify () ! );
66
65
}
67
66
}
68
67
69
68
extension on NumberFormatOptions {
70
- Object toJsOptions () {
71
- final o = newObject <Object >();
72
- setProperty (o, 'sign' , signDisplay.name);
73
- if (notation is CompactNotation ) {
74
- setProperty (o, 'compactDisplay' ,
75
- (notation as CompactNotation ).compactDisplay.name);
76
- }
69
+ JSAny toJsOptions () {
70
+ Map <String , dynamic > styleOptions;
77
71
if (style is CurrencyStyle ) {
78
72
final currencyStyle = style as CurrencyStyle ;
79
- setProperty (o, 'currency' , currencyStyle.currency);
80
- setProperty (o, 'currencyDisplay' , currencyStyle.display.name);
81
- setProperty (o, 'currencySign' , currencyStyle.sign.name);
82
- }
83
- setProperty (o, 'localeMatcher' , localeMatcher.jsName);
84
- setProperty (o, 'notation' , notation.name);
85
- if (numberingSystem != null ) {
86
- setProperty (o, 'numberingSystem' , numberingSystem);
87
- }
88
- setProperty (o, 'signDisplay' , signDisplay.name);
89
- setProperty (o, 'style' , style.name);
90
- if (style is UnitStyle ) {
73
+ styleOptions = {
74
+ 'currency' : currencyStyle.currency,
75
+ 'currencyDisplay' : currencyStyle.display.name,
76
+ 'currencySign' : currencyStyle.sign.name,
77
+ };
78
+ } else if (style is UnitStyle ) {
91
79
final unitStyle = style as UnitStyle ;
92
- setProperty (o, 'unit' , unitStyle.unit.jsName);
93
- setProperty (o, 'unitDisplay' , unitStyle.unitDisplay.name);
94
- }
95
- setProperty (o, 'useGrouping' , useGrouping.jsName);
96
- setProperty (o, 'roundingMode' , roundingMode.name);
97
- if (digits? .roundingPriority != null ) {
98
- setProperty (o, 'roundingPriority' , digits? .roundingPriority! .name);
99
- }
100
- if (digits? .roundingIncrement != null ) {
101
- setProperty (o, 'roundingIncrement' , digits? .roundingIncrement! );
102
- }
103
- setProperty (o, 'minimumIntegerDigits' , minimumIntegerDigits);
104
- if (digits? .fractionDigits.$1 != null ) {
105
- setProperty (o, 'minimumFractionDigits' , digits? .fractionDigits.$1);
106
- }
107
- if (digits? .fractionDigits.$2 != null ) {
108
- setProperty (o, 'maximumFractionDigits' , digits? .fractionDigits.$2);
109
- }
110
- if (digits? .significantDigits.$1 != null ) {
111
- setProperty (o, 'minimumSignificantDigits' , digits? .significantDigits.$1);
112
- }
113
- if (digits? .significantDigits.$2 != null ) {
114
- setProperty (o, 'maximumSignificantDigits' , digits? .significantDigits.$2);
80
+ styleOptions = {
81
+ 'unit' : unitStyle.unit.jsName,
82
+ 'unitDisplay' : unitStyle.unitDisplay.name,
83
+ };
84
+ } else {
85
+ styleOptions = {};
115
86
}
116
- setProperty (o, 'trailingZeroDisplay' , trailingZeroDisplay.name);
117
- return o;
87
+ return {
88
+ ...styleOptions,
89
+ 'sign' : signDisplay.name,
90
+ if (notation is CompactNotation )
91
+ 'compactDisplay' : (notation as CompactNotation ).compactDisplay.name,
92
+ 'localeMatcher' : localeMatcher.jsName,
93
+ 'notation' : notation.name,
94
+ if (numberingSystem != null ) 'numberingSystem' : numberingSystem,
95
+ 'signDisplay' : signDisplay.name,
96
+ 'style' : style.name,
97
+ 'useGrouping' : useGrouping.jsName,
98
+ 'roundingMode' : roundingMode.name,
99
+ if (digits? .roundingPriority != null )
100
+ 'roundingPriority' : digits? .roundingPriority! .name,
101
+ if (digits? .roundingIncrement != null )
102
+ 'roundingIncrement' : digits? .roundingIncrement! ,
103
+ 'minimumIntegerDigits' : minimumIntegerDigits,
104
+ if (digits? .fractionDigits.$1 != null )
105
+ 'minimumFractionDigits' : digits? .fractionDigits.$1,
106
+ if (digits? .fractionDigits.$2 != null )
107
+ 'maximumFractionDigits' : digits? .fractionDigits.$2,
108
+ if (digits? .significantDigits.$1 != null )
109
+ 'minimumSignificantDigits' : digits? .significantDigits.$1,
110
+ if (digits? .significantDigits.$2 != null )
111
+ 'maximumSignificantDigits' : digits? .significantDigits.$2,
112
+ 'trailingZeroDisplay' : trailingZeroDisplay.name,
113
+ }.jsify ()! ;
118
114
}
119
115
}
0 commit comments