NumberFormat.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function testNumberFormatOptions(opts, number, expected, altExpected) {
  6. try{
  7. var options = { minimumFractionDigits: 0, maximumFractionDigits: 0, minimumIntegerDigits: 1, style: "decimal", useGrouping: false };
  8. for (option in opts) {
  9. options[option] = opts[option];
  10. }
  11. var numberFormatter = new Intl.NumberFormat("en-US", options);
  12. var actual = numberFormatter.format(number);
  13. if (actual !== expected && actual != altExpected) {
  14. WScript.Echo("ERROR: Formatting '" + number + "' with options '" + JSON.stringify(options) + "' resulted in '" + actual + "'; expected '" + expected + "'!");
  15. }
  16. }
  17. catch (ex) {
  18. WScript.Echo(ex.message);
  19. }
  20. }
  21. function testNumberFormatOptionsRegex(opts, number, expectedRegex) {
  22. try{
  23. var options = { minimumFractionDigits: 0, maximumFractionDigits: 0, minimumIntegerDigits: 1, style: "decimal", useGrouping: false };
  24. for (option in opts) {
  25. options[option] = opts[option];
  26. }
  27. var numberFormatter = new Intl.NumberFormat("en-US", options);
  28. var actual = numberFormatter.format(number);
  29. if (!expectedRegex.test(actual)) {
  30. WScript.Echo("ERROR: Formatting '" + number + "' with options '" + JSON.stringify(options) + "' resulted in '" + actual + "'; expectedRegex '" + expectedRegex + "'!");
  31. }
  32. }
  33. catch (ex) {
  34. WScript.Echo(ex.message);
  35. }
  36. }
  37. function arrayEqual(arr1, arr2) {
  38. if (arr1.length !== arr2.length) return false;
  39. for (var i = 0; i < arr1.length; i++) {
  40. if (arr1[i] !== arr2[i]) return false;
  41. }
  42. return true;
  43. }
  44. function testSupportedLocales(locales, expectedResult) {
  45. try {
  46. var actual = Intl.NumberFormat.supportedLocalesOf(locales, { localeMatcher: "best fit" });
  47. if (!arrayEqual(actual, expectedResult)) {
  48. throw new Error("Calling SupportedLocalesOf on '[" + locales.join(",") + "]' doesn't match expected result '[" + expectedResult.join(",") + "]' when using best fit. Actual:[" + actual.join(",") + "]");
  49. }
  50. actual = Intl.NumberFormat.supportedLocalesOf(locales, { localeMatcher: "best fit" });
  51. if (!arrayEqual(actual, expectedResult)) {
  52. throw new Error("Calling SupportedLocalesOf on '[" + locales.join(",") + "]' doesn't match expected result '[" + expectedResult.join(",") + "]' when using lookup. Actual: [" + actual.join(",") + "]");
  53. }
  54. }
  55. catch (ex) {
  56. passed = false;
  57. WScript.Echo("Error testSupportedLocales: " + ex.message);
  58. }
  59. }
  60. testNumberFormatOptions({}, 5, "5");
  61. testNumberFormatOptions({ minimumFractionDigits: 2, maximumFractionDigits: 2 }, 5, "5.00");
  62. testNumberFormatOptions({ minimumFractionDigits: 1, maximumFractionDigits: 2 }, 5, "5.0");
  63. testNumberFormatOptions({ minimumFractionDigits: 1, maximumFractionDigits: 2 }, 5.444, "5.44");
  64. testNumberFormatOptions({ minimumIntegerDigits: 2 }, 5, "05");
  65. testNumberFormatOptions({ minimumSignificantDigits: 2 }, 5, "5.0");
  66. testNumberFormatOptions({ minimumSignificantDigits: 2 }, 125, "125");
  67. testNumberFormatOptions({ maximumSignificantDigits: 5 }, 125.123, "125.12");
  68. testNumberFormatOptions({ maximumSignificantDigits: 5 }, 125.125, "125.13");
  69. testNumberFormatOptions({ maximumSignificantDigits: 5 }, -125.125, "-125.13");
  70. testNumberFormatOptions({ useGrouping: true }, 12512, "12,512");
  71. testNumberFormatOptions({ useGrouping: false }, 12512, "12512");
  72. testNumberFormatOptionsRegex({ style: "percent" }, 1.5, new RegExp("150[\x20\u00a0]?%"));
  73. testNumberFormatOptions({ currency: "USD", style: "currency", maximumFractionDigits: 2, minimumFractionDigits: 2 }, 1.5, "$1.50");
  74. testNumberFormatOptions({ minimumIntegerDigits: NaN }, undefined, undefined);
  75. testNumberFormatOptions({ minimumIntegerDigits: "Error" }, undefined, undefined);
  76. testNumberFormatOptions({ minimumIntegerDigits: { foo: "bar" } }, undefined, undefined);
  77. testNumberFormatOptions({ minimumIntegerDigits: null }, undefined, undefined);
  78. testNumberFormatOptions({ minimumFractionDigits: { foo: "bar" } }, undefined, undefined);
  79. testNumberFormatOptions({ minimumFractionDigits: null }, "5", "5");
  80. testNumberFormatOptions({ minimumFractionDigits: NaN }, undefined, undefined);
  81. testNumberFormatOptions({ minimumFractionDigits: "Error" }, undefined, undefined);
  82. testNumberFormatOptions({ maximumFractionDigits: { foo: "bar" } }, undefined, undefined);
  83. testNumberFormatOptions({ maximumFractionDigits: null }, "5", "5");
  84. testNumberFormatOptions({ maximumFractionDigits: NaN }, undefined, undefined);
  85. testNumberFormatOptions({ maximumFractionDigits: "Error" }, undefined, undefined);
  86. testNumberFormatOptions({ minimumSignificantDigits: { foo: "bar" } }, undefined, undefined);
  87. testNumberFormatOptions({ minimumSignificantDigits: null }, undefined, undefined);
  88. testNumberFormatOptions({ minimumSignificantDigits: NaN }, undefined, undefined);
  89. testNumberFormatOptions({ minimumSignificantDigits: "Error" }, undefined, undefined);
  90. testNumberFormatOptions({ maximumSignificantDigits: { foo: "bar" } }, undefined, undefined);
  91. testNumberFormatOptions({ maximumSignificantDigits: null }, undefined, undefined);
  92. testNumberFormatOptions({ maximumSignificantDigits: NaN }, undefined, undefined);
  93. testNumberFormatOptions({ maximumSignificantDigits: "Error" }, undefined, undefined);
  94. testSupportedLocales(undefined, []);
  95. testSupportedLocales(["en-US"], ["en-US"]);
  96. testSupportedLocales([], []);
  97. testSupportedLocales(["xxx"], []);