Collator.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. var passed = true;
  6. function testCollatorOptions(option, value, str1, str2, expected) {
  7. try {
  8. var options = {};
  9. options[option] = value;
  10. var collator = new Intl.Collator("en-US", options);
  11. var actual = collator.compare(str1, str2);
  12. if (actual !== expected) {
  13. passed = false;
  14. WScript.Echo("ERROR: Comparing '" + str1 + "' and '" + str2 + "' with option '" + option + ": " + value + "' resulted in '" + actual + "'; expected '" + expected + "'!");
  15. }
  16. } catch (ex) {
  17. passed = false;
  18. WScript.Echo("Error testCollatorOptions: " + ex.message);
  19. }
  20. }
  21. function arrayEqual(arr1, arr2) {
  22. if (arr1.length !== arr2.length) return false;
  23. for (var i = 0; i < arr1.length; i++) {
  24. if (arr1[i] !== arr2[i]) return false;
  25. }
  26. return true;
  27. }
  28. function testSupportedLocales(locales, expectedResult) {
  29. try {
  30. var actual = Intl.Collator.supportedLocalesOf(locales, { localeMatcher: "best fit" });
  31. if (!arrayEqual(actual, expectedResult)) {
  32. throw new Error("Calling SupportedLocalesOf on '[" + locales.join(",") + "]' doesn't match expected result '[" + expectedResult.join(",") + "]' when using best fit. Actual:[" + actual.join(",") + "]");
  33. }
  34. actual = Intl.Collator.supportedLocalesOf(locales, { localeMatcher: "best fit" });
  35. if (!arrayEqual(actual, expectedResult)) {
  36. throw new Error("Calling SupportedLocalesOf on '[" + locales.join(",") + "]' doesn't match expected result '[" + expectedResult.join(",") + "]' when using lookup. Actual: [" + actual.join(",") + "]");
  37. }
  38. }
  39. catch (ex) {
  40. passed = false;
  41. WScript.Echo("Error testSupportedLocales: " + ex.message);
  42. }
  43. }
  44. testCollatorOptions("sensitivity", "base", "A", "a", 0);
  45. testCollatorOptions("sensitivity", "base", "A", "B", -1);
  46. testCollatorOptions("sensitivity", "base", "a", "\u00E2", 0);
  47. testCollatorOptions("sensitivity", "accent", "A", "a", 0);
  48. testCollatorOptions("sensitivity", "accent", "A", "B", -1);
  49. testCollatorOptions("sensitivity", "accent", "a", "\u00E2", -1);
  50. testCollatorOptions("sensitivity", "case", "A", "a", 1);
  51. testCollatorOptions("sensitivity", "case", "A", "B", -1);
  52. testCollatorOptions("sensitivity", "case", "a", "\u00E2", 0);
  53. testCollatorOptions("sensitivity", "variant", "A", "a", 1);
  54. testCollatorOptions("sensitivity", "variant", "A", "B", -1);
  55. testCollatorOptions("sensitivity", "variant", "a", "\u00E2", -1);
  56. testCollatorOptions("ignorePunctuation", true, ".a", "a", 0);
  57. testCollatorOptions("ignorePunctuation", false, ".a", "a", -1);
  58. testCollatorOptions("numeric", true, "10", "9", 1);
  59. testCollatorOptions("numeric", false, "10", "9", -1);
  60. testSupportedLocales(undefined, []);
  61. testSupportedLocales(["en-US"], ["en-US"]);
  62. testSupportedLocales([], []);
  63. testSupportedLocales(["xxx"], []);
  64. if (new Intl.Collator("es", { collation: "trad" }).resolvedOptions().collation !== "default") {
  65. WScript.Echo("Error: Collation option passed through option affects the value.");
  66. passed = false;
  67. }
  68. if (passed === true) {
  69. WScript.Echo("Pass");
  70. }