basic.js (18658B)
1 // |reftest| skip-if(!this.hasOwnProperty('Intl')||!Intl.hasOwnProperty("DurationFormat")) 2 3 const { 4 Integer, Group, Decimal, Fraction, Literal, Unit 5 } = NumberFormatParts; 6 7 const { 8 Year, Month, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond 9 } = DurationFormatParts; 10 11 const tests = { 12 "en": [ 13 { 14 options: {}, 15 data: [ 16 { 17 duration: {years: 1}, 18 expected: [ 19 ...Year(Integer("1"), Literal(" "), Unit("yr")), 20 ], 21 }, 22 { 23 duration: {years: 2}, 24 expected: [ 25 ...Year(Integer("2"), Literal(" "), Unit("yrs")), 26 ], 27 }, 28 ], 29 }, 30 ], 31 "de": [ 32 { 33 options: {style: "long"}, 34 data: [ 35 { 36 duration: {years: 1, months: 2, weeks: 3, days: 4}, 37 expected: [ 38 ...Year(Integer("1"), Literal(" "), Unit("Jahr")), 39 Literal(", "), 40 ...Month(Integer("2"), Literal(" "), Unit("Monate")), 41 Literal(", "), 42 ...Week(Integer("3"), Literal(" "), Unit("Wochen")), 43 Literal(" und "), 44 ...Day(Integer("4"), Literal(" "), Unit("Tage")), 45 ], 46 }, 47 { 48 duration: {hours: 1, minutes: 2}, 49 expected: [ 50 ...Hour(Integer("1"), Literal(" "), Unit("Stunde")), 51 Literal(", "), 52 ...Minute(Integer("2"), Literal(" "), Unit("Minuten")), 53 ], 54 }, 55 { 56 duration: {minutes: 3, seconds: 4}, 57 expected: [ 58 ...Minute(Integer("3"), Literal(" "), Unit("Minuten")), 59 Literal(", "), 60 ...Second(Integer("4"), Literal(" "), Unit("Sekunden")), 61 ], 62 }, 63 { 64 duration: {seconds: 5, milliseconds: 6}, 65 expected: [ 66 ...Second(Integer("5"), Literal(" "), Unit("Sekunden")), 67 Literal(", "), 68 ...Millisecond(Integer("6"), Literal(" "), Unit("Millisekunden")), 69 ], 70 }, 71 { 72 duration: {milliseconds: 7, microseconds: 8}, 73 expected: [ 74 ...Millisecond(Integer("7"), Literal(" "), Unit("Millisekunden")), 75 Literal(", "), 76 ...Microsecond(Integer("8"), Literal(" "), Unit("Mikrosekunden")), 77 ], 78 }, 79 { 80 duration: {microseconds: 9, nanoseconds: 10}, 81 expected: [ 82 ...Microsecond(Integer("9"), Literal(" "), Unit("Mikrosekunden")), 83 Literal(", "), 84 ...Nanosecond(Integer("10"), Literal(" "), Unit("Nanosekunden")), 85 ], 86 }, 87 88 // Gaps between time units. 89 { 90 duration: {hours: 1, seconds: 2}, 91 expected: [ 92 ...Hour(Integer("1"), Literal(" "), Unit("Stunde")), 93 Literal(", "), 94 ...Second(Integer("2"), Literal(" "), Unit("Sekunden")), 95 ], 96 }, 97 { 98 duration: {hours: 3, milliseconds: 4}, 99 expected: [ 100 ...Hour(Integer("3"), Literal(" "), Unit("Stunden")), 101 Literal(", "), 102 ...Millisecond(Integer("4"), Literal(" "), Unit("Millisekunden")), 103 ], 104 }, 105 { 106 duration: {hours: 5, microseconds: 6}, 107 expected: [ 108 ...Hour(Integer("5"), Literal(" "), Unit("Stunden")), 109 Literal(", "), 110 ...Microsecond(Integer("6"), Literal(" "), Unit("Mikrosekunden")), 111 ], 112 }, 113 { 114 duration: {hours: 7, nanoseconds: 9}, 115 expected: [ 116 ...Hour(Integer("7"), Literal(" "), Unit("Stunden")), 117 Literal(", "), 118 ...Nanosecond(Integer("9"), Literal(" "), Unit("Nanosekunden")), 119 ], 120 }, 121 ], 122 }, 123 { 124 options: {style: "short"}, 125 data: [ 126 { 127 duration: {years: 1, months: 2, weeks: 3, days: 4}, 128 expected: [ 129 ...Year(Integer("1"), Literal(" "), Unit("J")), 130 Literal(", "), 131 ...Month(Integer("2"), Literal(" "), Unit("Mon.")), 132 Literal(", "), 133 ...Week(Integer("3"), Literal(" "), Unit("Wo.")), 134 Literal(" und "), 135 ...Day(Integer("4"), Literal(" "), Unit("Tg.")), 136 ], 137 }, 138 { 139 duration: {hours: 1, minutes: 2}, 140 expected: [ 141 ...Hour(Integer("1"), Literal(" "), Unit("Std.")), 142 Literal(", "), 143 ...Minute(Integer("2"), Literal(" "), Unit("Min.")), 144 ], 145 }, 146 { 147 duration: {minutes: 3, seconds: 4}, 148 expected: [ 149 ...Minute(Integer("3"), Literal(" "), Unit("Min.")), 150 Literal(", "), 151 ...Second(Integer("4"), Literal(" "), Unit("Sek.")), 152 ], 153 }, 154 { 155 duration: {seconds: 5, milliseconds: 6}, 156 expected: [ 157 ...Second(Integer("5"), Literal(" "), Unit("Sek.")), 158 Literal(", "), 159 ...Millisecond(Integer("6"), Literal(" "), Unit("ms")), 160 ], 161 }, 162 { 163 duration: {milliseconds: 7, microseconds: 8}, 164 expected: [ 165 ...Millisecond(Integer("7"), Literal(" "), Unit("ms")), 166 Literal(", "), 167 ...Microsecond(Integer("8"), Literal(" "), Unit("μs")), 168 ], 169 }, 170 { 171 duration: {microseconds: 9, nanoseconds: 10}, 172 expected: [ 173 ...Microsecond(Integer("9"), Literal(" "), Unit("μs")), 174 Literal(", "), 175 ...Nanosecond(Integer("10"), Literal(" "), Unit("ns")), 176 ], 177 }, 178 ], 179 }, 180 { 181 options: {style: "narrow"}, 182 data: [ 183 { 184 duration: {years: 1, months: 2, weeks: 3, days: 4}, 185 expected: [ 186 ...Year(Integer("1"), Literal(" "), Unit("J")), 187 Literal(", "), 188 ...Month(Integer("2"), Literal(" "), Unit("M")), 189 Literal(", "), 190 ...Week(Integer("3"), Literal(" "), Unit("W")), 191 Literal(" und "), 192 ...Day(Integer("4"), Literal(" "), Unit("T")), 193 ], 194 }, 195 { 196 duration: {hours: 1, minutes: 2}, 197 expected: [ 198 ...Hour(Integer("1"), Unit("h")), 199 Literal(", "), 200 ...Minute(Integer("2"), Literal(" "), Unit("Min.")), 201 ], 202 }, 203 { 204 duration: {minutes: 3, seconds: 4}, 205 expected: [ 206 ...Minute(Integer("3"), Literal(" "), Unit("Min.")), 207 Literal(", "), 208 ...Second(Integer("4"), Literal(" "), Unit("Sek.")), 209 ], 210 }, 211 { 212 duration: {seconds: 5, milliseconds: 6}, 213 expected: [ 214 ...Second(Integer("5"), Literal(" "), Unit("Sek.")), 215 Literal(", "), 216 ...Millisecond(Integer("6"), Literal(" "), Unit("ms")), 217 ], 218 }, 219 { 220 duration: {milliseconds: 7, microseconds: 8}, 221 expected: [ 222 ...Millisecond(Integer("7"), Literal(" "), Unit("ms")), 223 Literal(", "), 224 ...Microsecond(Integer("8"), Literal(" "), Unit("μs")), 225 ], 226 }, 227 { 228 duration: {microseconds: 9, nanoseconds: 10}, 229 expected: [ 230 ...Microsecond(Integer("9"), Literal(" "), Unit("μs")), 231 Literal(", "), 232 ...Nanosecond(Integer("10"), Literal(" "), Unit("ns")), 233 ], 234 }, 235 ], 236 }, 237 ], 238 "fr": [ 239 { 240 options: {style: "digital"}, 241 data: [ 242 // "digital" style defaults to "short" for non-numeric parts. 243 { 244 duration: {years: 111, months: 222, weeks: 333, days: 444}, 245 expected: [ 246 ...Year(Integer("111"), Literal(" "), Unit("ans")), 247 Literal(", "), 248 ...Month(Integer("222"), Literal(" "), Unit("m.")), 249 Literal(", "), 250 ...Week(Integer("333"), Literal(" "), Unit("sem.")), 251 Literal(", "), 252 ...Day(Integer("444"), Literal(" "), Unit("j")), 253 Literal(" et "), 254 ...Hour(Integer("0")), 255 Literal(":"), 256 ...Minute(Integer("00")), 257 Literal(":"), 258 ...Second(Integer("00")), 259 ], 260 }, 261 262 { 263 duration: {hours: 1, minutes: 0}, 264 expected: [ 265 ...Hour(Integer("1")), 266 Literal(":"), 267 ...Minute(Integer("00")), 268 Literal(":"), 269 ...Second(Integer("00")), 270 ], 271 }, 272 { 273 duration: {hours: 1, minutes: 2}, 274 expected: [ 275 ...Hour(Integer("1")), 276 Literal(":"), 277 ...Minute(Integer("02")), 278 Literal(":"), 279 ...Second(Integer("00")), 280 ], 281 }, 282 283 // Fractional digits default to min=0 and max=9. 284 { 285 duration: {hours: 1, minutes: 2, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 286 expected: [ 287 ...Hour(Integer("1")), 288 Literal(":"), 289 ...Minute(Integer("02")), 290 Literal(":"), 291 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 292 ], 293 }, 294 295 // Zero unit digital parts aren't omitted. 296 { 297 duration: {hours: 1, minutes: 0, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 298 expected: [ 299 ...Hour(Integer("1")), 300 Literal(":"), 301 ...Minute(Integer("00")), 302 Literal(":"), 303 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 304 ], 305 }, 306 ], 307 }, 308 { 309 options: {style: "digital", fractionalDigits: 9}, 310 data: [ 311 { 312 duration: {hours: 1, minutes: 2, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 313 expected: [ 314 ...Hour(Integer("1")), 315 Literal(":"), 316 ...Minute(Integer("02")), 317 Literal(":"), 318 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 319 ], 320 }, 321 { 322 duration: {hours: 1, minutes: 0, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 323 expected: [ 324 ...Hour(Integer("1")), 325 Literal(":"), 326 ...Minute(Integer("00")), 327 Literal(":"), 328 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 329 ], 330 }, 331 ], 332 }, 333 { 334 options: { 335 style: "digital", 336 hoursDisplay: "always", 337 minutesDisplay: "always", 338 secondsDisplay: "always", 339 fractionalDigits: 9, 340 }, 341 data: [ 342 { 343 duration: {hours: 1}, 344 expected: [ 345 ...Hour(Integer("1")), 346 Literal(":"), 347 ...Minute(Integer("00")), 348 Literal(":"), 349 ...Second(Integer("00"), Decimal(","), Fraction("000000000")), 350 ], 351 }, 352 { 353 duration: {hours: 1, minutes: 2}, 354 expected: [ 355 ...Hour(Integer("1")), 356 Literal(":"), 357 ...Minute(Integer("02")), 358 Literal(":"), 359 ...Second(Integer("00"), Decimal(","), Fraction("000000000")), 360 ], 361 }, 362 { 363 duration: {hours: 1, minutes: 2, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 364 expected: [ 365 ...Hour(Integer("1")), 366 Literal(":"), 367 ...Minute(Integer("02")), 368 Literal(":"), 369 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 370 ], 371 }, 372 { 373 duration: {hours: 1, minutes: 0, seconds: 3, milliseconds: 4, microseconds: 5, nanoseconds: 6}, 374 expected: [ 375 ...Hour(Integer("1")), 376 Literal(":"), 377 ...Minute(Integer("00")), 378 Literal(":"), 379 ...Second(Integer("03"), Decimal(","), Fraction("004005006")), 380 ], 381 }, 382 ], 383 }, 384 ], 385 "es": [ 386 { 387 options: { 388 seconds: "2-digit", 389 fractionalDigits: 3, 390 }, 391 data: [ 392 { 393 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 3}, 394 expected: [...Second(Integer("00"), Decimal(","), Fraction("000"))], 395 }, 396 { 397 duration: {seconds: 0, milliseconds: 1, microseconds: 2, nanoseconds: 3}, 398 expected: [...Second(Integer("00"), Decimal(","), Fraction("001"))], 399 }, 400 { 401 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 1002003}, 402 expected: [...Second(Integer("00"), Decimal(","), Fraction("001"))], 403 }, 404 { 405 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 123001002003}, 406 expected: [...Second(Integer("123"), Decimal(","), Fraction("001"))], 407 }, 408 ], 409 }, 410 { 411 options: { 412 seconds: "2-digit", 413 fractionalDigits: 6, 414 }, 415 data: [ 416 { 417 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 3}, 418 expected: [...Second(Integer("00"), Decimal(","), Fraction("000000"))], 419 }, 420 { 421 duration: {seconds: 0, milliseconds: 1, microseconds: 2, nanoseconds: 3}, 422 expected: [...Second(Integer("00"), Decimal(","), Fraction("001002"))], 423 }, 424 { 425 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 1002003}, 426 expected: [...Second(Integer("00"), Decimal(","), Fraction("001002"))], 427 }, 428 { 429 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 123001002003}, 430 expected: [...Second(Integer("123"), Decimal(","), Fraction("001002"))], 431 }, 432 ], 433 }, 434 { 435 options: { 436 seconds: "2-digit", 437 fractionalDigits: 9, 438 }, 439 data: [ 440 { 441 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 3}, 442 expected: [...Second(Integer("00"), Decimal(","), Fraction("000000003"))], 443 }, 444 { 445 duration: {seconds: 0, milliseconds: 1, microseconds: 2, nanoseconds: 3}, 446 expected: [...Second(Integer("00"), Decimal(","), Fraction("001002003"))], 447 }, 448 { 449 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 1002003}, 450 expected: [...Second(Integer("00"), Decimal(","), Fraction("001002003"))], 451 }, 452 { 453 duration: {seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 123001002003}, 454 expected: [...Second(Integer("123"), Decimal(","), Fraction("001002003"))], 455 }, 456 ], 457 }, 458 ], 459 "ar-EG": [ 460 { 461 options: { 462 years: "long", 463 months: "short", 464 weeks: "short", 465 days: "narrow", 466 yearsDisplay: "always", 467 monthsDisplay: "always", 468 weeksDisplay: "always", 469 daysDisplay: "always", 470 }, 471 data: [ 472 { 473 duration: {years: 100, months: 200, weeks: 0, days: 1000}, 474 expected: [ 475 ...Year(Integer("١٠٠"), Literal(" "), Unit("سنة")), 476 Literal("، و"), 477 ...Month(Integer("٢٠٠"), Literal(" "), Unit("شهر")), 478 Literal("، و"), 479 ...Week(Integer("٠"), Literal(" "), Unit("أسبوع")), 480 Literal("، و"), 481 ...Day(Integer("١"), Group("٬"), Integer("٠٠٠"), Literal(" "), Unit("ي")), 482 ], 483 }, 484 ], 485 } 486 ], 487 "zh": [ 488 { 489 options: { 490 years: "narrow", 491 months: "narrow", 492 weeks: "short", 493 days: "short", 494 yearsDisplay: "always", 495 monthsDisplay: "always", 496 weeksDisplay: "always", 497 daysDisplay: "always", 498 hoursDisplay: "always", 499 minutesDisplay: "always", 500 secondsDisplay: "always", 501 millisecondsDisplay: "always", 502 microsecondsDisplay: "always", 503 nanosecondsDisplay: "always", 504 }, 505 data: [ 506 { 507 duration: {years: 100, months: 200, weeks: 0, days: 1000}, 508 expected: [ 509 ...Year(Integer("100"), Unit("年")), 510 ...Month(Integer("200"), Unit("个月")), 511 ...Week(Integer("0"), Unit("周")), 512 ...Day(Integer("1"), Group(","), Integer("000"), Unit("天")), 513 ...Hour(Integer("0"), Unit("小时")), 514 ...Minute(Integer("0"), Unit("分钟")), 515 ...Second(Integer("0"), Unit("秒")), 516 ...Millisecond(Integer("0"), Unit("毫秒")), 517 ...Microsecond(Integer("0"), Unit("微秒")), 518 ...Nanosecond(Integer("0"), Unit("纳秒")), 519 ], 520 }, 521 { 522 duration: {years: 0}, 523 expected: [ 524 ...Year(Integer("0"), Unit("年")), 525 ...Month(Integer("0"), Unit("个月")), 526 ...Week(Integer("0"), Unit("周")), 527 ...Day(Integer("0"), Unit("天")), 528 ...Hour(Integer("0"), Unit("小时")), 529 ...Minute(Integer("0"), Unit("分钟")), 530 ...Second(Integer("0"), Unit("秒")), 531 ...Millisecond(Integer("0"), Unit("毫秒")), 532 ...Microsecond(Integer("0"), Unit("微秒")), 533 ...Nanosecond(Integer("0"), Unit("纳秒")), 534 ], 535 }, 536 { 537 duration: {nanoseconds: 0}, 538 expected: [ 539 ...Year(Integer("0"), Unit("年")), 540 ...Month(Integer("0"), Unit("个月")), 541 ...Week(Integer("0"), Unit("周")), 542 ...Day(Integer("0"), Unit("天")), 543 ...Hour(Integer("0"), Unit("小时")), 544 ...Minute(Integer("0"), Unit("分钟")), 545 ...Second(Integer("0"), Unit("秒")), 546 ...Millisecond(Integer("0"), Unit("毫秒")), 547 ...Microsecond(Integer("0"), Unit("微秒")), 548 ...Nanosecond(Integer("0"), Unit("纳秒")), 549 ], 550 }, 551 ], 552 } 553 ], 554 }; 555 556 for (let [locale, list] of Object.entries(tests)) { 557 for (let {options, data} of list) { 558 let df = new Intl.DurationFormat(locale, options); 559 for (let {duration, expected} of data) { 560 let str = PartsToString(expected); 561 562 assertEq(df.format(duration), str, 563 `${locale} [${JSON.stringify(options)}]: ${JSON.stringify(duration)}`); 564 565 let parts = df.formatToParts(duration); 566 assertEq(PartsToString(parts), str, 567 `${locale} [${JSON.stringify(options)}]: ${JSON.stringify(duration)}`); 568 569 assertEq(parts.length, expected.length, 570 `${locale} [${JSON.stringify(options)}]: ${JSON.stringify(duration)}`); 571 572 assertDeepEq(parts, expected); 573 } 574 } 575 } 576 577 if (typeof reportCompare === "function") 578 reportCompare(true, true);