-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
327 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ | ||
out/collatorDart.js | ||
out/collatorDart.js.deps | ||
out/collatorDart.js.map | ||
out/*Dart.js | ||
out/*Dart.js.deps | ||
out/*Dart.js.map | ||
pubspec.lock |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:intl4x/intl4x.dart'; | ||
import 'package:intl4x/number_format.dart'; | ||
|
||
final patternsToOptions = <String, NumberFormatOptions>{ | ||
"0.0": | ||
NumberFormatOptions.custom(digits: Digits.withFractionDigits(minimum: 1)), | ||
"00": NumberFormatOptions.custom( | ||
digits: Digits.withSignificantDigits(minimum: 1)), | ||
"@@@": NumberFormatOptions.custom( | ||
digits: Digits.withSignificantDigits(minimum: 3)), | ||
"@@###": NumberFormatOptions.custom( | ||
digits: Digits.withSignificantDigits(minimum: 2, maximum: 5)), | ||
"0.0000E0": NumberFormatOptions.custom( | ||
notation: ScientificNotation(), | ||
digits: Digits.withFractionDigits(minimum: 4)), | ||
}; | ||
|
||
// The nodejs version that first supported advance rounding options | ||
const first_v3_version = 'v20.1.0'; | ||
|
||
enum NodeVersion { | ||
v3, | ||
preV3, | ||
} | ||
|
||
const unsupported_skeleton_terms = [ | ||
"scientific/+ee/sign-always", | ||
"decimal-always", | ||
]; | ||
|
||
const unsupported_rounding_modes = ["unnecessary"]; | ||
|
||
// Use this | ||
const supported_options_by_version = { | ||
NodeVersion.v3: [ | ||
"compactDisplay", | ||
"currency", | ||
"currencyDisplay", | ||
"currencySign", | ||
"localeMatcher", | ||
"notation", | ||
"numberingSystem", | ||
"signDisplay", | ||
"style", | ||
"unit", | ||
"unitDisplay", | ||
"useGrouping", | ||
"roundingMode", | ||
"roundingPriority", | ||
"roundingIncrement", | ||
"trailingZeroDisplay", | ||
"minimumIntegerDigits", | ||
"minimumFractionDigits", | ||
"maximumFractionDigits", | ||
"minimumSignificantDigits", | ||
"maximumSignificantDigits" | ||
], | ||
NodeVersion.preV3: [ | ||
"compactDisplay", | ||
"currency", | ||
"currencyDisplay", | ||
"currencySign", | ||
"localeMatcher", | ||
"notation", | ||
"numberingSystem", | ||
"signDisplay", | ||
"style", | ||
"unit", | ||
"unitDisplay", | ||
"useGrouping", | ||
"roundingMode", | ||
"minimumIntegerDigits", | ||
"minimumFractionDigits", | ||
"maximumFractionDigits", | ||
"minimumSignificantDigits", | ||
"maximumSignificantDigits" | ||
] | ||
// TODO: Add older version support. | ||
}; | ||
|
||
String testDecimalFormat( | ||
String encoded, | ||
bool doLogInput, | ||
String node_version, | ||
) { | ||
final json = jsonDecode(encoded); | ||
final label = json['label'] as String?; | ||
final skeleton = json['skeleton'] as String?; | ||
final pattern = json['pattern'] as String?; | ||
final rounding = json['rounding'] as String?; | ||
var input = | ||
double.parse(json['input'] as String); // May be changed with some options | ||
|
||
var unsupported_options = []; | ||
|
||
// If options are in the JSON, use them... | ||
NumberFormatOptions options; | ||
var jsonOptions = json['options'] as Map<String, dynamic>; | ||
if (json.containsKey('options')) { | ||
options = fromJson(jsonOptions); | ||
} else { | ||
try { | ||
options = decimalPatternToOptions(pattern, rounding); | ||
} catch (error) { | ||
// Some error - to return this message | ||
return jsonEncode({ | ||
'error': "Can't convert pattern", | ||
'label': label, | ||
}); | ||
} | ||
} | ||
// Default maximumFractionDigits and rounding modes are set in test generation | ||
final roundingMode = options.roundingMode; | ||
|
||
// Check each option for implementation. | ||
// Handle percent - input value is the basis of the actual percent | ||
// expected, e.g., input='0.25' should be interpreted '0.25%' | ||
if (options.style is PercentStyle) { | ||
input = input / 100.0; | ||
} | ||
|
||
// Handle scale in the skeleton | ||
var skeleton_terms; | ||
if (skeleton != null) { | ||
skeleton_terms = skeleton.split(" "); // all the components | ||
if (doLogInput) { | ||
print("# SKEL: " + skeleton_terms); | ||
} | ||
final scale_regex = RegExp(r'/scale\/(\d+\.\d*)/'); | ||
final match_scale = scale_regex.firstMatch(skeleton); | ||
if (match_scale != null) { | ||
// Get the value and use it | ||
final scale_value = double.parse(match_scale.group(1)!); | ||
input = input * scale_value; | ||
} | ||
} | ||
|
||
// Supported options depends on the nodejs version | ||
if (doLogInput) { | ||
print("#NNNN " + node_version); | ||
} | ||
List<String> version_supported_options; | ||
if (node_version.compareTo(first_v3_version) >= 0) { | ||
if (doLogInput) { | ||
print("#V3 !!!! " + node_version); | ||
} | ||
version_supported_options = supported_options_by_version[NodeVersion.v3]!; | ||
} else { | ||
if (doLogInput) { | ||
print("#pre_v3 !!!! " + node_version); | ||
} | ||
version_supported_options = | ||
supported_options_by_version[NodeVersion.preV3]!; | ||
} | ||
if (doLogInput) { | ||
print("#NNNN $version_supported_options"); | ||
} | ||
// Check for option items that are not supported | ||
for (var key in jsonOptions.keys) { | ||
if (!version_supported_options.contains(key)) { | ||
unsupported_options.add((key + ":" + jsonOptions[key])); | ||
} | ||
} | ||
|
||
// Check for skelection terms that are not supported | ||
for (var skel_index in skeleton_terms) { | ||
final skel_term = skeleton_terms[skel_index]; | ||
if (doLogInput) { | ||
print("# SKEL_TERM: " + skel_term); | ||
} | ||
if (unsupported_skeleton_terms.contains(skel_term)) { | ||
unsupported_options.add(skel_term); | ||
if (doLogInput) { | ||
print("# UNSUPPORTED SKEL_TERM: " + skel_term); | ||
} | ||
} | ||
} | ||
|
||
if (unsupported_rounding_modes.contains(roundingMode.name)) { | ||
unsupported_options.add(roundingMode.name); | ||
} | ||
if (unsupported_options.length > 0) { | ||
return jsonEncode({ | ||
'label': label, | ||
"unsupported": "unsupported_options", | ||
"error_detail": {'unsupported_options': unsupported_options} | ||
}); | ||
} | ||
|
||
var testLocale = json['locale']; | ||
|
||
NumberFormat nf; | ||
Map<String, dynamic> outputLine; | ||
try { | ||
if (testLocale) { | ||
nf = Intl(locale: testLocale).numberFormat(options); | ||
} else { | ||
nf = Intl(locale: Locale(language: 'und')).numberFormat(options); | ||
} | ||
|
||
var result = 'NOT IMPLEMENTED'; | ||
result = nf.format(input); | ||
|
||
// TODO: Catch unsupported units, e.g., furlongs. | ||
// Formatting as JSON | ||
var resultString = result; | ||
|
||
outputLine = { | ||
"label": json['label'], | ||
"result": resultString, | ||
"actual_options": options | ||
}; | ||
} catch (error) { | ||
if (error.toString().contains('furlong')) { | ||
// This is a special kind of unsupported. | ||
return jsonEncode({ | ||
'label': label, | ||
"unsupported": "unsupported_options", | ||
"error_detail": {'unsupported_options': error.toString()} | ||
}); | ||
} | ||
// Handle type of the error | ||
outputLine = { | ||
"label": json['label'], | ||
"error": "formatting error", | ||
}; | ||
if (error is RangeError) { | ||
outputLine['error_detail'] = error.message; | ||
outputLine['actual_options'] = options; | ||
} | ||
} | ||
return jsonEncode(outputLine); | ||
} | ||
|
||
NumberFormatOptions decimalPatternToOptions(String? pattern, String? rounding) { | ||
final numberFormatOptions = | ||
patternsToOptions[pattern] ?? NumberFormatOptions.custom(); | ||
if (rounding != null) { | ||
var roundingMode = | ||
RoundingMode.values.firstWhere((mode) => mode.name == rounding); | ||
return numberFormatOptions.copyWith(roundingMode: roundingMode); | ||
} else { | ||
return numberFormatOptions; | ||
} | ||
} | ||
|
||
NumberFormatOptions fromJson(Map<String, dynamic> options) { | ||
return NumberFormatOptions.custom(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'numberformat.dart'; | ||
|
||
void main(List<String> args) { | ||
//just some call to not treeshake the function | ||
testDecimalFormat(args.first, bool.parse(args[2]), args[3]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var tools = require('./numberformatDart'); | ||
// The Collator used for the actual testing. | ||
|
||
// !!! TODO: Collation: determine the sensitivity that corresponds | ||
// to the strength. | ||
module.exports = { | ||
testDecimalFormat: function (json) { | ||
return JSON.parse(tools.testDecimalFormat(JSON.stringify(json))); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
const dartVersion = "0.4.0"; | ||
const dartVersion = "0.5.0"; | ||
module.exports = { dartVersion }; |
Oops, something went wrong.