basic-zero.js (1715B)
1 // |reftest| skip-if(!this.hasOwnProperty('Intl')||!Intl.hasOwnProperty("DurationFormat")) 2 3 // Test formatting zero amount for a single unit and compare "auto" against 4 // "always" for the display option. 5 6 const { 7 Integer, Literal, Unit 8 } = NumberFormatParts; 9 10 function ToDurationFormatPart(unit) { 11 unit = unit.slice(0, -1); 12 unit = unit[0].toUpperCase() + unit.slice(1); 13 return DurationFormatParts[unit]; 14 } 15 16 const tests = { 17 "en": Object.fromEntries(units.map(unit => [unit, unit])), 18 "de": { 19 "years": "Jahre", 20 "months": "Monate", 21 "weeks": "Wochen", 22 "days": "Tage", 23 "hours": "Stunden", 24 "minutes": "Minuten", 25 "seconds": "Sekunden", 26 "milliseconds": "Millisekunden", 27 "microseconds": "Mikrosekunden", 28 "nanoseconds": "Nanosekunden", 29 }, 30 }; 31 32 for (let [locale, data] of Object.entries(tests)) { 33 for (let unit of units) { 34 let auto = new Intl.DurationFormat(locale, { 35 style: "long", 36 [unit + "Display"]: "auto", 37 }); 38 let always = new Intl.DurationFormat(locale, { 39 style: "long", 40 [unit + "Display"]: "always", 41 }); 42 43 let duration = {[unit]: 0}; 44 let expected = `0 ${data[unit]}`; 45 46 // Empty string. 47 assertEq(auto.format(duration), "", `auto: ${unit}`); 48 49 // Empty array. 50 assertEq(auto.formatToParts(duration).length, 0, `auto: ${unit}`); 51 52 assertEq(always.format(duration), expected, `always: ${unit}`); 53 54 let parts = always.formatToParts(duration); 55 assertEq(PartsToString(parts), expected, `always: ${unit}`); 56 57 assertDeepEq(parts, [ 58 ...ToDurationFormatPart(unit)(Integer("0"), Literal(" "), Unit(data[unit])), 59 ]); 60 } 61 } 62 63 if (typeof reportCompare === "function") 64 reportCompare(true, true);