Skip to content

Commit 470533e

Browse files
committed
Fix ecma
1 parent 38f81b6 commit 470533e

File tree

7 files changed

+115
-223
lines changed

7 files changed

+115
-223
lines changed

pkgs/intl4x/lib/src/locale/locale.dart

+5-31
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import '../../collation.dart';
6-
import '../datetime_format/datetime_format_options.dart';
75
import 'locale_native.dart' if (dart.library.js) 'locale_ecma.dart';
86

97
/// Representing a Unicode locale identifier. It is composed of the primary
@@ -22,25 +20,12 @@ class Locale {
2220
/// for Latin America.
2321
final String? region;
2422

25-
final Calendar? calendar;
26-
final CaseFirst? caseFirst;
27-
final String? collation;
28-
final HourCycle? hourCycle;
29-
final String? numberingSystem;
30-
final bool? numeric;
31-
3223
//TODO(mosum): Add RecordSymbol here, as soon as it is supported on
3324
//const constructors
3425
const Locale({
3526
required this.language,
3627
this.region,
3728
this.script,
38-
this.calendar,
39-
this.caseFirst,
40-
this.collation,
41-
this.hourCycle,
42-
this.numberingSystem,
43-
this.numeric,
4429
});
4530

4631
/// Generate a language tag by joining the subtags with the [separator].
@@ -63,27 +48,16 @@ class Locale {
6348
return other is Locale &&
6449
other.language == language &&
6550
other.script == script &&
66-
other.region == region &&
67-
other.calendar == calendar &&
68-
other.caseFirst == caseFirst &&
69-
other.collation == collation &&
70-
other.hourCycle == hourCycle &&
71-
other.numberingSystem == numberingSystem &&
72-
other.numeric == numeric;
51+
other.region == region;
7352
}
7453

7554
@override
7655
int get hashCode {
77-
return language.hashCode ^
78-
script.hashCode ^
79-
region.hashCode ^
80-
calendar.hashCode ^
81-
caseFirst.hashCode ^
82-
collation.hashCode ^
83-
hourCycle.hashCode ^
84-
numberingSystem.hashCode ^
85-
numeric.hashCode;
56+
return language.hashCode ^ script.hashCode ^ region.hashCode;
8657
}
58+
59+
@override
60+
String toString() => toLanguageTag();
8761
}
8862

8963
// TODO: add all locales which are supported by ICU4X / Browsers

pkgs/intl4x/lib/src/locale/locale_ecma.dart

+6-49
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import 'dart:js_interop';
66

7-
import '../../collation.dart';
8-
import '../datetime_format/datetime_format_options.dart';
97
import 'locale.dart';
108

119
@JS('Intl.Locale')
@@ -17,12 +15,6 @@ extension type LocaleJS._(JSObject _) implements JSObject {
1715
external String get language;
1816
external String? get script;
1917
external String? get region;
20-
external String? get calendar;
21-
external String? get caseFirst;
22-
external String? get collation;
23-
external String? get hourCycle;
24-
external String? get numberingSystem;
25-
external String? get numeric;
2618
}
2719

2820
Locale parseLocale(String s) => toLocale(LocaleJS(s));
@@ -31,52 +23,17 @@ Locale toLocale(LocaleJS parsed) => Locale(
3123
language: parsed.language,
3224
region: parsed.region,
3325
script: parsed.script,
34-
calendar: Calendar.values
35-
.where((element) => element.jsName == parsed.calendar)
36-
.firstOrNull,
37-
caseFirst: CaseFirst.values
38-
.where((element) => element.jsName == parsed.caseFirst)
39-
.firstOrNull,
40-
collation: parsed.collation,
41-
hourCycle: HourCycle.values
42-
.where((element) => element.name == parsed.hourCycle)
43-
.firstOrNull,
44-
numberingSystem: parsed.numberingSystem,
45-
numeric: bool.tryParse(parsed.numeric ?? ''),
4626
);
4727

48-
String toLanguageTagImpl(Locale l, [String separator = '-']) {
49-
// return fromLocale(l).toString(); Uncomment as soon as https://github.com/dart-lang/sdk/issues/53106 is resolved
50-
51-
final subtags = <String>[
52-
if (l.calendar != null) ...['ca', l.calendar!.jsName],
53-
if (l.caseFirst != null) l.caseFirst!.jsName,
54-
if (l.collation != null) l.collation!,
55-
if (l.hourCycle != null) ...['hc', l.hourCycle!.name],
56-
if (l.numberingSystem != null) l.numberingSystem!,
57-
if (l.numeric != null) l.numeric!.toString(),
58-
];
59-
return <String>[
60-
l.language,
61-
if (l.script != null) l.script!,
62-
if (l.region != null) l.region!,
63-
if (subtags.isNotEmpty) 'u',
64-
...subtags,
65-
].join(separator);
66-
}
28+
String toLanguageTagImpl(Locale l, [String separator = '-']) =>
29+
fromLocale(l).toString();
6730

6831
LocaleJS fromLocale(Locale l) {
6932
final options = {
70-
if (l.region != null) 'region': l.region!.toJS,
71-
if (l.script != null) 'script': l.script!.toJS,
72-
if (l.calendar != null) 'calendar': l.calendar!.jsName.toJS,
73-
if (l.caseFirst != null) 'caseFirst': l.caseFirst!.jsName.toJS,
74-
if (l.collation != null) 'collation': l.collation!.toJS,
75-
if (l.hourCycle != null) 'hourCycle': l.hourCycle!.name.toJS,
76-
if (l.numberingSystem != null) 'numberingSystem': l.numberingSystem!.toJS,
77-
if (l.numeric != null) 'numeric': l.numeric!.toJS,
78-
}.jsify()!;
79-
return LocaleJS.constructor(l.language, options);
33+
if (l.region != null) 'region': l.region,
34+
if (l.script != null) 'script': l.script,
35+
};
36+
return LocaleJS.constructor(l.language, options.jsify()!);
8037
}
8138

8239
Locale minimizeImpl(Locale l) => toLocale(fromLocale(l).minimize());

pkgs/intl4x/lib/src/locale/locale_native.dart

-10
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,10 @@ Locale parseLocale(String s, [String separator = '-']) {
4747
}
4848

4949
String toLanguageTagImpl(Locale l, [String separator = '-']) {
50-
final subtags = <String>[
51-
if (l.calendar != null) ...['ca', l.calendar!.jsName],
52-
if (l.caseFirst != null) l.caseFirst!.jsName,
53-
if (l.collation != null) l.collation!,
54-
if (l.hourCycle != null) ...['hc', l.hourCycle!.name],
55-
if (l.numberingSystem != null) l.numberingSystem!,
56-
if (l.numeric != null) l.numeric!.toString(),
57-
];
5850
return <String>[
5951
l.language,
6052
if (l.script != null) l.script!,
6153
if (l.region != null) l.region!,
62-
if (subtags.isNotEmpty) 'u',
63-
...subtags,
6454
].join(separator);
6555
}
6656

pkgs/intl4x/lib/src/number_format/number_format_ecma.dart

+58-62
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:js/js.dart';
6-
import 'package:js/js_util.dart';
5+
import 'dart:js_interop';
76

87
import '../locale/locale.dart';
98
import '../options.dart';
@@ -18,16 +17,14 @@ NumberFormatImpl? getNumberFormatterECMA(
1817
_NumberFormatECMA.tryToBuild(locale, options, localeMatcher);
1918

2019
@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);
2523

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+
}
3128

3229
class _NumberFormatECMA extends NumberFormatImpl {
3330
_NumberFormatECMA(super.locale, super.options);
@@ -47,73 +44,72 @@ class _NumberFormatECMA extends NumberFormatImpl {
4744
LocaleMatcher localeMatcher,
4845
Locale locale,
4946
) {
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
5453
.whereType<String>()
5554
.map(Locale.parse)
5655
.toList();
5756
}
5857

5958
@override
6059
String formatImpl(Object number) {
61-
final numberFormatJS = _NumberFormatJS(
62-
[locale.toLanguageTag()],
60+
final numberFormatJS = NumberFormat(
61+
[locale.toLanguageTag().toJS].toJS,
6362
options.toJsOptions(),
6463
);
65-
return numberFormatJS.format(number);
64+
return numberFormatJS.format(number.jsify()!);
6665
}
6766
}
6867

6968
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;
7771
if (style is CurrencyStyle) {
7872
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) {
9179
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 = {};
11586
}
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()!;
118114
}
119115
}

0 commit comments

Comments
 (0)