IntlBuiltIns.js 1.8 KB

1234567891011121314151617181920212223242526272829
  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 testBuiltInFunction(options, builtInConstructor, builtInName, builtInFunc, intlConstructor, intlFunc, args) {
  6. try {
  7. var builtInValue = args.length === 1 ?
  8. new builtInConstructor(args[0])[builtInFunc]("en-US", options) :
  9. new builtInConstructor(args[0])[builtInFunc](args[1], "en-US", options);
  10. var intlValue = new Intl[intlConstructor]("en-US", options)[intlFunc](args[0], args[1]);
  11. if (builtInValue !== intlValue) {
  12. console.log("ERROR: Result from built in function 'new " + builtInName + "()." + builtInFunc + "' doesn't match Intl." + intlConstructor + "'s function '" + intlFunc + "'!");
  13. }
  14. }
  15. catch (ex) {
  16. console.log(ex.message);
  17. }
  18. }
  19. testBuiltInFunction({ minimumFractionDigits: 3 }, Number, "Number", "toLocaleString", "NumberFormat", "format", [5]);
  20. testBuiltInFunction({ sensitivity: "base" }, String, "String", "localeCompare", "Collator", "compare", ["A", "a"]);
  21. testBuiltInFunction({ hour: "numeric", timeZone: "UTC" }, Date, "Date", "toLocaleString", "DateTimeFormat", "format", [new Date(2000, 1, 1)]);
  22. testBuiltInFunction({ hour: "numeric", timeZone: "UTC" }, Date, "Date", "toLocaleTimeString", "DateTimeFormat", "format", [new Date(2000, 1, 1)]);
  23. testBuiltInFunction({ month: "numeric", timeZone: "UTC" }, Date, "Date", "toLocaleDateString", "DateTimeFormat", "format", [new Date(2000, 1, 1)]);
  24. console.log("Pass");