tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

use-grouping.js (2204B)


      1 // |reftest| skip-if(!this.hasOwnProperty("Intl"))
      2 
      3 const tests = {
      4  // minimumGroupingDigits is one for "en" (English).
      5  "en": {
      6    values: [
      7      {
      8        value: 1000,
      9        useGroupings: {
     10          auto: "1,000",
     11          always: "1,000",
     12          min2: "1000",
     13          "": "1000",
     14        },
     15      },
     16      {
     17        value: 10000,
     18        useGroupings: {
     19          auto: "10,000",
     20          always: "10,000",
     21          min2: "10,000",
     22          "": "10000",
     23        },
     24      },
     25    ],
     26  },
     27 
     28  // minimumGroupingDigits is two for "pl" (Polish).
     29  "pl": {
     30    values: [
     31      {
     32        value: 1000,
     33        useGroupings: {
     34          auto: "1000",
     35          always: "1 000",
     36          min2: "1000",
     37          "": "1000",
     38        },
     39      },
     40      {
     41        value: 10000,
     42        useGroupings: {
     43          auto: "10 000",
     44          always: "10 000",
     45          min2: "10 000",
     46          "": "10000",
     47        },
     48      },
     49    ],
     50  },
     51 };
     52 
     53 for (let [locale, {options = {}, values}] of Object.entries(tests)) {
     54  for (let {value, useGroupings} of values) {
     55    for (let [useGrouping, expected] of Object.entries(useGroupings)) {
     56      let nf = new Intl.NumberFormat(locale, {...options, useGrouping});
     57      assertEq(nf.format(value), expected, `locale=${locale}, value=${value}, useGrouping=${useGrouping}`);
     58    }
     59  }
     60 }
     61 
     62 // Resolved options.
     63 for (let [useGrouping, expected] of [
     64  [false, false],
     65  ["", false],
     66  [0, false],
     67  [null, false],
     68 
     69  ["auto", "auto"],
     70  [undefined, "auto"],
     71  ["true", "auto"],
     72  ["false", "auto"],
     73 
     74  ["always", "always"],
     75  [true, "always"],
     76 
     77  ["min2", "min2"],
     78 ]) {
     79  let nf = new Intl.NumberFormat("en", {useGrouping});
     80  assertEq(nf.resolvedOptions().useGrouping , expected);
     81 }
     82 
     83 // Throws a RangeError for unsupported values.
     84 for (let useGrouping of [
     85  "none",
     86  "yes",
     87  "no",
     88  {},
     89  123,
     90  123n,
     91 ]) {
     92  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {useGrouping}), RangeError);
     93 }
     94 
     95 // Throws a TypeError if ToString fails.
     96 for (let useGrouping of [Object.create(null), Symbol()]) {
     97  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {useGrouping}), TypeError);
     98 }
     99 
    100 if (typeof reportCompare === "function")
    101  reportCompare(true, true);