| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/** |
| 6 |
* DurationFormat internal properties. |
| 7 |
*/ |
| 8 |
function durationFormatLocaleData() { |
| 9 |
return { |
| 10 |
nu: getNumberingSystems, |
| 11 |
default: { |
| 12 |
nu: intl_numberingSystem, |
| 13 |
}, |
| 14 |
}; |
| 15 |
} |
| 16 |
var durationFormatInternalProperties = { |
| 17 |
localeData: durationFormatLocaleData, |
| 18 |
relevantExtensionKeys: ["nu"], |
| 19 |
}; |
| 20 |
|
| 21 |
/** |
| 22 |
* Intl.DurationFormat ( [ locales [ , options ] ] ) |
| 23 |
* |
| 24 |
* Compute an internal properties object from |lazyDurationFormatData|. |
| 25 |
*/ |
| 26 |
function resolveDurationFormatInternals(lazyDurationFormatData) { |
| 27 |
assert(IsObject(lazyDurationFormatData), "lazy data not an object?"); |
| 28 |
|
| 29 |
var internalProps = std_Object_create(null); |
| 30 |
|
| 31 |
var DurationFormat = durationFormatInternalProperties; |
| 32 |
|
| 33 |
// Compute effective locale. |
| 34 |
|
| 35 |
// Step 9. |
| 36 |
var r = ResolveLocale( |
| 37 |
"DurationFormat", |
| 38 |
lazyDurationFormatData.requestedLocales, |
| 39 |
lazyDurationFormatData.opt, |
| 40 |
DurationFormat.relevantExtensionKeys, |
| 41 |
DurationFormat.localeData |
| 42 |
); |
| 43 |
|
| 44 |
// Steps 10-11. |
| 45 |
internalProps.locale = r.locale; |
| 46 |
|
| 47 |
// Steps 12-21. (Not applicable in our implementation.) |
| 48 |
|
| 49 |
// Step 22. |
| 50 |
internalProps.numberingSystem = r.nu; |
| 51 |
|
| 52 |
// Step 24. |
| 53 |
internalProps.style = lazyDurationFormatData.style; |
| 54 |
|
| 55 |
// Step 26. |
| 56 |
internalProps.yearsStyle = lazyDurationFormatData.yearsStyle; |
| 57 |
internalProps.yearsDisplay = lazyDurationFormatData.yearsDisplay; |
| 58 |
|
| 59 |
internalProps.weeksStyle = lazyDurationFormatData.weeksStyle; |
| 60 |
internalProps.weeksDisplay = lazyDurationFormatData.weeksDisplay; |
| 61 |
|
| 62 |
internalProps.monthsStyle = lazyDurationFormatData.monthsStyle; |
| 63 |
internalProps.monthsDisplay = lazyDurationFormatData.monthsDisplay; |
| 64 |
|
| 65 |
internalProps.daysStyle = lazyDurationFormatData.daysStyle; |
| 66 |
internalProps.daysDisplay = lazyDurationFormatData.daysDisplay; |
| 67 |
|
| 68 |
internalProps.hoursStyle = lazyDurationFormatData.hoursStyle; |
| 69 |
internalProps.hoursDisplay = lazyDurationFormatData.hoursDisplay; |
| 70 |
|
| 71 |
internalProps.minutesStyle = lazyDurationFormatData.minutesStyle; |
| 72 |
internalProps.minutesDisplay = lazyDurationFormatData.minutesDisplay; |
| 73 |
|
| 74 |
internalProps.secondsStyle = lazyDurationFormatData.secondsStyle; |
| 75 |
internalProps.secondsDisplay = lazyDurationFormatData.secondsDisplay; |
| 76 |
|
| 77 |
internalProps.millisecondsStyle = lazyDurationFormatData.millisecondsStyle; |
| 78 |
internalProps.millisecondsDisplay = |
| 79 |
lazyDurationFormatData.millisecondsDisplay; |
| 80 |
|
| 81 |
internalProps.microsecondsStyle = lazyDurationFormatData.microsecondsStyle; |
| 82 |
internalProps.microsecondsDisplay = |
| 83 |
lazyDurationFormatData.microsecondsDisplay; |
| 84 |
|
| 85 |
internalProps.nanosecondsStyle = lazyDurationFormatData.nanosecondsStyle; |
| 86 |
internalProps.nanosecondsDisplay = lazyDurationFormatData.nanosecondsDisplay; |
| 87 |
|
| 88 |
// Step 27. |
| 89 |
internalProps.fractionalDigits = lazyDurationFormatData.fractionalDigits; |
| 90 |
|
| 91 |
// The caller is responsible for associating |internalProps| with the right |
| 92 |
// object using |setInternalProperties|. |
| 93 |
return internalProps; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns an object containing the DurationFormat internal properties of |obj|. |
| 98 |
*/ |
| 99 |
function getDurationFormatInternals(obj) { |
| 100 |
assert(IsObject(obj), "getDurationFormatInternals called with non-object"); |
| 101 |
assert( |
| 102 |
intl_GuardToDurationFormat(obj) !== null, |
| 103 |
"getDurationFormatInternals called with non-DurationFormat" |
| 104 |
); |
| 105 |
|
| 106 |
var internals = getIntlObjectInternals(obj); |
| 107 |
assert( |
| 108 |
internals.type === "DurationFormat", |
| 109 |
"bad type escaped getIntlObjectInternals" |
| 110 |
); |
| 111 |
|
| 112 |
// If internal properties have already been computed, use them. |
| 113 |
var internalProps = maybeInternalProperties(internals); |
| 114 |
if (internalProps) { |
| 115 |
return internalProps; |
| 116 |
} |
| 117 |
|
| 118 |
// Otherwise it's time to fully create them. |
| 119 |
internalProps = resolveDurationFormatInternals(internals.lazyData); |
| 120 |
setInternalProperties(internals, internalProps); |
| 121 |
return internalProps; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Intl.DurationFormat ( [ locales [ , options ] ] ) |
| 126 |
* |
| 127 |
* Initializes an object as a DurationFormat. |
| 128 |
* |
| 129 |
* This method is complicated a moderate bit by its implementing initialization |
| 130 |
* as a *lazy* concept. Everything that must happen now, does -- but we defer |
| 131 |
* all the work we can until the object is actually used as a DurationFormat. |
| 132 |
* This later work occurs in |resolveDurationFormatInternals|; steps not noted |
| 133 |
* here occur there. |
| 134 |
*/ |
| 135 |
function InitializeDurationFormat(durationFormat, locales, options) { |
| 136 |
assert( |
| 137 |
IsObject(durationFormat), |
| 138 |
"InitializeDurationFormat called with non-object" |
| 139 |
); |
| 140 |
assert( |
| 141 |
intl_GuardToDurationFormat(durationFormat) !== null, |
| 142 |
"InitializeDurationFormat called with non-DurationFormat" |
| 143 |
); |
| 144 |
|
| 145 |
// Lazy DurationFormat data has the following structure: |
| 146 |
// |
| 147 |
// { |
| 148 |
// requestedLocales: List of locales, |
| 149 |
// style: "long" / "short" / "narrow" / "digital", |
| 150 |
// |
| 151 |
// yearsStyle: "long" / "short" / "narrow", |
| 152 |
// yearsDisplay: "auto" / "always", |
| 153 |
// |
| 154 |
// monthsStyle: "long" / "short" / "narrow", |
| 155 |
// monthsDisplay: "auto" / "always", |
| 156 |
// |
| 157 |
// weeksStyle: "long" / "short" / "narrow", |
| 158 |
// weeksDisplay: "auto" / "always", |
| 159 |
// |
| 160 |
// daysStyle: "long" / "short" / "narrow", |
| 161 |
// daysDisplay: "auto" / "always", |
| 162 |
// |
| 163 |
// hoursStyle: "long" / "short" / "narrow" / "numeric" / "2-digit", |
| 164 |
// hoursDisplay: "auto" / "always", |
| 165 |
// |
| 166 |
// minutesStyle: "long" / "short" / "narrow" / "numeric" / "2-digit", |
| 167 |
// minutesDisplay: "auto" / "always", |
| 168 |
// |
| 169 |
// secondsStyle: "long" / "short" / "narrow" / "numeric" / "2-digit", |
| 170 |
// secondsDisplay: "auto" / "always", |
| 171 |
// |
| 172 |
// millisecondsStyle: "long" / "short" / "narrow" / "numeric", |
| 173 |
// millisecondsDisplay: "auto" / "always", |
| 174 |
// |
| 175 |
// microsecondsStyle: "long" / "short" / "narrow" / "numeric", |
| 176 |
// microsecondsDisplay: "auto" / "always", |
| 177 |
// |
| 178 |
// nanosecondsStyle: "long" / "short" / "narrow" / "numeric", |
| 179 |
// nanosecondsDisplay: "auto" / "always", |
| 180 |
// |
| 181 |
// fractionalDigits: integer ∈ [0, 9] / undefined, |
| 182 |
// |
| 183 |
// opt: // opt object computed in InitializeDurationFormat |
| 184 |
// { |
| 185 |
// localeMatcher: "lookup" / "best fit", |
| 186 |
// |
| 187 |
// nu: string matching a Unicode extension type, // optional |
| 188 |
// } |
| 189 |
// } |
| 190 |
// |
| 191 |
// Note that lazy data is only installed as a final step of initialization, |
| 192 |
// so every DurationFormat lazy data object has *all* these properties, |
| 193 |
// never a subset of them. |
| 194 |
var lazyDurationFormatData = std_Object_create(null); |
| 195 |
|
| 196 |
// Step 3. |
| 197 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 198 |
lazyDurationFormatData.requestedLocales = requestedLocales; |
| 199 |
|
| 200 |
// Step 4. |
| 201 |
if (options === undefined) { |
| 202 |
options = std_Object_create(null); |
| 203 |
} else if (!IsObject(options)) { |
| 204 |
ThrowTypeError( |
| 205 |
JSMSG_OBJECT_REQUIRED, |
| 206 |
options === null ? "null" : typeof options |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
// Step 5. |
| 211 |
var matcher = GetOption( |
| 212 |
options, |
| 213 |
"localeMatcher", |
| 214 |
"string", |
| 215 |
["lookup", "best fit"], |
| 216 |
"best fit" |
| 217 |
); |
| 218 |
|
| 219 |
// Step 6. |
| 220 |
var numberingSystem = GetOption( |
| 221 |
options, |
| 222 |
"numberingSystem", |
| 223 |
"string", |
| 224 |
undefined, |
| 225 |
undefined |
| 226 |
); |
| 227 |
|
| 228 |
// Step 7. |
| 229 |
if (numberingSystem !== undefined) { |
| 230 |
numberingSystem = intl_ValidateAndCanonicalizeUnicodeExtensionType( |
| 231 |
numberingSystem, |
| 232 |
"numberingSystem", |
| 233 |
"nu" |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
// Step 8. |
| 238 |
var opt = new_Record(); |
| 239 |
opt.localeMatcher = matcher; |
| 240 |
opt.nu = numberingSystem; |
| 241 |
|
| 242 |
lazyDurationFormatData.opt = opt; |
| 243 |
|
| 244 |
// Compute formatting options. |
| 245 |
|
| 246 |
// Steps 23-24. |
| 247 |
var style = GetOption( |
| 248 |
options, |
| 249 |
"style", |
| 250 |
"string", |
| 251 |
["long", "short", "narrow", "digital"], |
| 252 |
"short" |
| 253 |
); |
| 254 |
lazyDurationFormatData.style = style; |
| 255 |
|
| 256 |
// Step 25. (Not applicable in our implementation) |
| 257 |
|
| 258 |
// Step 26, unit = "years". |
| 259 |
var yearsOptions = GetDurationUnitOptions( |
| 260 |
"years", |
| 261 |
options, |
| 262 |
style, |
| 263 |
["long", "short", "narrow"], |
| 264 |
"short", |
| 265 |
/* prevStyle= */ "" |
| 266 |
); |
| 267 |
lazyDurationFormatData.yearsStyle = yearsOptions.style; |
| 268 |
lazyDurationFormatData.yearsDisplay = yearsOptions.display; |
| 269 |
|
| 270 |
// Step 26, unit = "months". |
| 271 |
var monthsOptions = GetDurationUnitOptions( |
| 272 |
"months", |
| 273 |
options, |
| 274 |
style, |
| 275 |
["long", "short", "narrow"], |
| 276 |
"short", |
| 277 |
/* prevStyle= */ "" |
| 278 |
); |
| 279 |
lazyDurationFormatData.monthsStyle = monthsOptions.style; |
| 280 |
lazyDurationFormatData.monthsDisplay = monthsOptions.display; |
| 281 |
|
| 282 |
// Step 26, unit = "weeks". |
| 283 |
var weeksOptions = GetDurationUnitOptions( |
| 284 |
"weeks", |
| 285 |
options, |
| 286 |
style, |
| 287 |
["long", "short", "narrow"], |
| 288 |
"short", |
| 289 |
/* prevStyle= */ "" |
| 290 |
); |
| 291 |
lazyDurationFormatData.weeksStyle = weeksOptions.style; |
| 292 |
lazyDurationFormatData.weeksDisplay = weeksOptions.display; |
| 293 |
|
| 294 |
// Step 26, unit = "days". |
| 295 |
var daysOptions = GetDurationUnitOptions( |
| 296 |
"days", |
| 297 |
options, |
| 298 |
style, |
| 299 |
["long", "short", "narrow"], |
| 300 |
"short", |
| 301 |
/* prevStyle= */ "" |
| 302 |
); |
| 303 |
lazyDurationFormatData.daysStyle = daysOptions.style; |
| 304 |
lazyDurationFormatData.daysDisplay = daysOptions.display; |
| 305 |
|
| 306 |
// Step 26, unit = "hours". |
| 307 |
var hoursOptions = GetDurationUnitOptions( |
| 308 |
"hours", |
| 309 |
options, |
| 310 |
style, |
| 311 |
["long", "short", "narrow", "numeric", "2-digit"], |
| 312 |
"numeric", |
| 313 |
/* prevStyle= */ "" |
| 314 |
); |
| 315 |
lazyDurationFormatData.hoursStyle = hoursOptions.style; |
| 316 |
lazyDurationFormatData.hoursDisplay = hoursOptions.display; |
| 317 |
|
| 318 |
// Step 26, unit = "minutes". |
| 319 |
var minutesOptions = GetDurationUnitOptions( |
| 320 |
"minutes", |
| 321 |
options, |
| 322 |
style, |
| 323 |
["long", "short", "narrow", "numeric", "2-digit"], |
| 324 |
"numeric", |
| 325 |
hoursOptions.style |
| 326 |
); |
| 327 |
lazyDurationFormatData.minutesStyle = minutesOptions.style; |
| 328 |
lazyDurationFormatData.minutesDisplay = minutesOptions.display; |
| 329 |
|
| 330 |
// Step 26, unit = "seconds". |
| 331 |
var secondsOptions = GetDurationUnitOptions( |
| 332 |
"seconds", |
| 333 |
options, |
| 334 |
style, |
| 335 |
["long", "short", "narrow", "numeric", "2-digit"], |
| 336 |
"numeric", |
| 337 |
minutesOptions.style |
| 338 |
); |
| 339 |
lazyDurationFormatData.secondsStyle = secondsOptions.style; |
| 340 |
lazyDurationFormatData.secondsDisplay = secondsOptions.display; |
| 341 |
|
| 342 |
// Step 26, unit = "milliseconds". |
| 343 |
var millisecondsOptions = GetDurationUnitOptions( |
| 344 |
"milliseconds", |
| 345 |
options, |
| 346 |
style, |
| 347 |
["long", "short", "narrow", "numeric"], |
| 348 |
"numeric", |
| 349 |
secondsOptions.style |
| 350 |
); |
| 351 |
lazyDurationFormatData.millisecondsStyle = millisecondsOptions.style; |
| 352 |
lazyDurationFormatData.millisecondsDisplay = millisecondsOptions.display; |
| 353 |
|
| 354 |
// Step 26, unit = "microseconds". |
| 355 |
var microsecondsOptions = GetDurationUnitOptions( |
| 356 |
"microseconds", |
| 357 |
options, |
| 358 |
style, |
| 359 |
["long", "short", "narrow", "numeric"], |
| 360 |
"numeric", |
| 361 |
millisecondsOptions.style |
| 362 |
); |
| 363 |
lazyDurationFormatData.microsecondsStyle = microsecondsOptions.style; |
| 364 |
lazyDurationFormatData.microsecondsDisplay = microsecondsOptions.display; |
| 365 |
|
| 366 |
// Step 26, unit = "milliseconds". |
| 367 |
var nanosecondsOptions = GetDurationUnitOptions( |
| 368 |
"nanoseconds", |
| 369 |
options, |
| 370 |
style, |
| 371 |
["long", "short", "narrow", "numeric"], |
| 372 |
"numeric", |
| 373 |
microsecondsOptions.style |
| 374 |
); |
| 375 |
lazyDurationFormatData.nanosecondsStyle = nanosecondsOptions.style; |
| 376 |
lazyDurationFormatData.nanosecondsDisplay = nanosecondsOptions.display; |
| 377 |
|
| 378 |
// Step 27. |
| 379 |
lazyDurationFormatData.fractionalDigits = GetNumberOption( |
| 380 |
options, |
| 381 |
"fractionalDigits", |
| 382 |
0, |
| 383 |
9, |
| 384 |
undefined |
| 385 |
); |
| 386 |
|
| 387 |
// We've done everything that must be done now: mark the lazy data as fully |
| 388 |
// computed and install it. |
| 389 |
initializeIntlObject( |
| 390 |
durationFormat, |
| 391 |
"DurationFormat", |
| 392 |
lazyDurationFormatData |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle, twoDigitHours ) |
| 398 |
*/ |
| 399 |
function GetDurationUnitOptions( |
| 400 |
unit, |
| 401 |
options, |
| 402 |
baseStyle, |
| 403 |
stylesList, |
| 404 |
digitalBase, |
| 405 |
prevStyle |
| 406 |
) { |
| 407 |
assert(typeof unit === "string", "unit is a string"); |
| 408 |
assert(IsObject(options), "options is an object"); |
| 409 |
assert(typeof baseStyle === "string", "baseStyle is a string"); |
| 410 |
assert(IsArray(stylesList), "stylesList is an array"); |
| 411 |
assert(typeof digitalBase === "string", "digitalBase is a string"); |
| 412 |
assert(typeof prevStyle === "string", "prevStyle is a string"); |
| 413 |
|
| 414 |
// Step 1. |
| 415 |
var styleOption = GetOption(options, unit, "string", stylesList, undefined); |
| 416 |
|
| 417 |
var style = styleOption; |
| 418 |
|
| 419 |
// Step 2. |
| 420 |
var displayDefault = "always"; |
| 421 |
|
| 422 |
// Step 3. |
| 423 |
if (style === undefined) { |
| 424 |
// Steps 3.a-b. |
| 425 |
if (baseStyle === "digital") { |
| 426 |
// Step 3.a.i. |
| 427 |
if (unit !== "hours" && unit !== "minutes" && unit !== "seconds") { |
| 428 |
displayDefault = "auto"; |
| 429 |
} |
| 430 |
|
| 431 |
// Step 3.a.ii. |
| 432 |
style = digitalBase; |
| 433 |
} else { |
| 434 |
// Steps 3.b.i-ii. ("fractional" handled implicitly) |
| 435 |
if (prevStyle === "numeric" || prevStyle === "2-digit") { |
| 436 |
// Step 3.b.i.1. |
| 437 |
if (unit !== "minutes" && unit !== "seconds") { |
| 438 |
// Step 3.b.i.1.a. |
| 439 |
displayDefault = "auto"; |
| 440 |
} |
| 441 |
|
| 442 |
// Step 3.b.i.2. |
| 443 |
style = "numeric"; |
| 444 |
} else { |
| 445 |
// Step 3.b.ii.1. |
| 446 |
displayDefault = "auto"; |
| 447 |
|
| 448 |
// Step 3.b.ii.2. |
| 449 |
style = baseStyle; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
// Step 4. |
| 455 |
var isFractional = |
| 456 |
style === "numeric" && |
| 457 |
(unit === "milliseconds" || |
| 458 |
unit === "microseconds" || |
| 459 |
unit === "nanoseconds"); |
| 460 |
if (isFractional) { |
| 461 |
// Step 4.a.i. (Not applicable in our implementation) |
| 462 |
|
| 463 |
// Step 4.a.ii. |
| 464 |
displayDefault = "auto"; |
| 465 |
} |
| 466 |
|
| 467 |
// Step 5. |
| 468 |
var displayField = unit + "Display"; |
| 469 |
|
| 470 |
// Step 6. |
| 471 |
var displayOption = GetOption( |
| 472 |
options, |
| 473 |
displayField, |
| 474 |
"string", |
| 475 |
["auto", "always"], |
| 476 |
undefined |
| 477 |
); |
| 478 |
|
| 479 |
var display = displayOption ?? displayDefault; |
| 480 |
|
| 481 |
// Step 7. |
| 482 |
if (display === "always" && isFractional) { |
| 483 |
assert( |
| 484 |
styleOption !== undefined || displayOption !== undefined, |
| 485 |
"no error is thrown when both 'style' and 'display' are absent" |
| 486 |
); |
| 487 |
|
| 488 |
ThrowRangeError( |
| 489 |
// eslint-disable-next-line no-nested-ternary |
| 490 |
styleOption !== undefined && displayOption !== undefined |
| 491 |
? JSMSG_INTL_DURATION_INVALID_DISPLAY_OPTION |
| 492 |
: displayOption !== undefined |
| 493 |
? JSMSG_INTL_DURATION_INVALID_DISPLAY_OPTION_DEFAULT_STYLE |
| 494 |
: JSMSG_INTL_DURATION_INVALID_DISPLAY_OPTION_DEFAULT_DISPLAY, |
| 495 |
unit |
| 496 |
); |
| 497 |
} |
| 498 |
|
| 499 |
// Steps 8-9. |
| 500 |
if (prevStyle === "numeric" || prevStyle === "2-digit") { |
| 501 |
// Step 8.a. and 9.a. |
| 502 |
if (style !== "numeric" && style !== "2-digit") { |
| 503 |
ThrowRangeError( |
| 504 |
JSMSG_INTL_DURATION_INVALID_NON_NUMERIC_OPTION, |
| 505 |
unit, |
| 506 |
`"${style}"` |
| 507 |
); |
| 508 |
} |
| 509 |
|
| 510 |
// Step 9.b. |
| 511 |
else if (unit === "minutes" || unit === "seconds") { |
| 512 |
style = "2-digit"; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// Step 10. (Our implementation doesn't use |twoDigitHours|.) |
| 517 |
|
| 518 |
// Step 11. |
| 519 |
return { style, display }; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Returns the subset of the given locale list for which this locale list has a |
| 524 |
* matching (possibly fallback) locale. Locales appear in the same order in the |
| 525 |
* returned list as in the input list. |
| 526 |
*/ |
| 527 |
function Intl_DurationFormat_supportedLocalesOf(locales /*, options*/) { |
| 528 |
var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined; |
| 529 |
|
| 530 |
// Step 1. |
| 531 |
var availableLocales = "DurationFormat"; |
| 532 |
|
| 533 |
// Step 2. |
| 534 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 535 |
|
| 536 |
// Step 3. |
| 537 |
return SupportedLocales(availableLocales, requestedLocales, options); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* ToIntegerIfIntegral ( argument ) |
| 542 |
*/ |
| 543 |
function ToIntegerIfIntegral(argument) { |
| 544 |
// Step 1. |
| 545 |
var number = ToNumber(argument); |
| 546 |
|
| 547 |
// Step 2. |
| 548 |
if (!Number_isInteger(number)) { |
| 549 |
ThrowRangeError(JSMSG_INTL_DURATION_NOT_INTEGER, number); |
| 550 |
} |
| 551 |
|
| 552 |
// Step 3. |
| 553 |
// |
| 554 |
// Add +0 to normalize -0 to +0. |
| 555 |
return number + 0; |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* ToDurationRecord ( input ) |
| 560 |
*/ |
| 561 |
function ToDurationRecord(input) { |
| 562 |
// Step 1. |
| 563 |
if (!IsObject(input)) { |
| 564 |
// Step 1.a. |
| 565 |
if (typeof input === "string") { |
| 566 |
ThrowRangeError(JSMSG_INTL_DURATION_UNEXPECTED_STRING); |
| 567 |
} |
| 568 |
|
| 569 |
// Step 1.b. |
| 570 |
ThrowTypeError( |
| 571 |
JSMSG_OBJECT_REQUIRED, |
| 572 |
input === null ? "null" : typeof input |
| 573 |
); |
| 574 |
} |
| 575 |
|
| 576 |
// Step 2. |
| 577 |
var result = { |
| 578 |
years: 0, |
| 579 |
months: 0, |
| 580 |
weeks: 0, |
| 581 |
days: 0, |
| 582 |
hours: 0, |
| 583 |
minutes: 0, |
| 584 |
seconds: 0, |
| 585 |
milliseconds: 0, |
| 586 |
microseconds: 0, |
| 587 |
nanoseconds: 0, |
| 588 |
}; |
| 589 |
|
| 590 |
// Step 3. |
| 591 |
var days = input.days; |
| 592 |
|
| 593 |
// Step 4. |
| 594 |
if (days !== undefined) { |
| 595 |
result.days = ToIntegerIfIntegral(days); |
| 596 |
} |
| 597 |
|
| 598 |
// Step 5. |
| 599 |
var hours = input.hours; |
| 600 |
|
| 601 |
// Step 6. |
| 602 |
if (hours !== undefined) { |
| 603 |
result.hours = ToIntegerIfIntegral(hours); |
| 604 |
} |
| 605 |
|
| 606 |
// Step 7. |
| 607 |
var microseconds = input.microseconds; |
| 608 |
|
| 609 |
// Step 8. |
| 610 |
if (microseconds !== undefined) { |
| 611 |
result.microseconds = ToIntegerIfIntegral(microseconds); |
| 612 |
} |
| 613 |
|
| 614 |
// Step 9. |
| 615 |
var milliseconds = input.milliseconds; |
| 616 |
|
| 617 |
// Step 10. |
| 618 |
if (milliseconds !== undefined) { |
| 619 |
result.milliseconds = ToIntegerIfIntegral(milliseconds); |
| 620 |
} |
| 621 |
|
| 622 |
// Step 11. |
| 623 |
var minutes = input.minutes; |
| 624 |
|
| 625 |
// Step 12. |
| 626 |
if (minutes !== undefined) { |
| 627 |
result.minutes = ToIntegerIfIntegral(minutes); |
| 628 |
} |
| 629 |
|
| 630 |
// Step 13. |
| 631 |
var months = input.months; |
| 632 |
|
| 633 |
// Step 14. |
| 634 |
if (months !== undefined) { |
| 635 |
result.months = ToIntegerIfIntegral(months); |
| 636 |
} |
| 637 |
|
| 638 |
// Step 15. |
| 639 |
var nanoseconds = input.nanoseconds; |
| 640 |
|
| 641 |
// Step 16. |
| 642 |
if (nanoseconds !== undefined) { |
| 643 |
result.nanoseconds = ToIntegerIfIntegral(nanoseconds); |
| 644 |
} |
| 645 |
|
| 646 |
// Step 17. |
| 647 |
var seconds = input.seconds; |
| 648 |
|
| 649 |
// Step 18. |
| 650 |
if (seconds !== undefined) { |
| 651 |
result.seconds = ToIntegerIfIntegral(seconds); |
| 652 |
} |
| 653 |
|
| 654 |
// Step 19. |
| 655 |
var weeks = input.weeks; |
| 656 |
|
| 657 |
// Step 20. |
| 658 |
if (weeks !== undefined) { |
| 659 |
result.weeks = ToIntegerIfIntegral(weeks); |
| 660 |
} |
| 661 |
|
| 662 |
// Step 21. |
| 663 |
var years = input.years; |
| 664 |
|
| 665 |
// Step 22. |
| 666 |
if (years !== undefined) { |
| 667 |
result.years = ToIntegerIfIntegral(years); |
| 668 |
} |
| 669 |
|
| 670 |
// Step 23. |
| 671 |
if ( |
| 672 |
years === undefined && |
| 673 |
months === undefined && |
| 674 |
weeks === undefined && |
| 675 |
days === undefined && |
| 676 |
hours === undefined && |
| 677 |
minutes === undefined && |
| 678 |
seconds === undefined && |
| 679 |
milliseconds === undefined && |
| 680 |
microseconds === undefined && |
| 681 |
nanoseconds === undefined |
| 682 |
) { |
| 683 |
ThrowTypeError(JSMSG_INTL_DURATION_MISSING_UNIT); |
| 684 |
} |
| 685 |
|
| 686 |
// Step 24. |
| 687 |
if (!IsValidDurationSign(result)) { |
| 688 |
ThrowRangeError(JSMSG_INTL_DURATION_INVALID_SIGN); |
| 689 |
} |
| 690 |
if (!IsValidDurationLimit(result)) { |
| 691 |
ThrowRangeError(JSMSG_INTL_DURATION_INVALID_LIMIT); |
| 692 |
} |
| 693 |
|
| 694 |
// Step 25. |
| 695 |
return result; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ) |
| 700 |
*/ |
| 701 |
function DurationSign(record) { |
| 702 |
assert(IsObject(record), "record is an object"); |
| 703 |
assert(Number_isInteger(record.years), "record.years is an integer"); |
| 704 |
assert(Number_isInteger(record.months), "record.months is an integer"); |
| 705 |
assert(Number_isInteger(record.weeks), "record.weeks is an integer"); |
| 706 |
assert(Number_isInteger(record.days), "record.days is an integer"); |
| 707 |
assert(Number_isInteger(record.hours), "record.hours is an integer"); |
| 708 |
assert(Number_isInteger(record.minutes), "record.minutes is an integer"); |
| 709 |
assert(Number_isInteger(record.seconds), "record.seconds is an integer"); |
| 710 |
assert( |
| 711 |
Number_isInteger(record.milliseconds), |
| 712 |
"record.milliseconds is an integer" |
| 713 |
); |
| 714 |
assert( |
| 715 |
Number_isInteger(record.microseconds), |
| 716 |
"record.microseconds is an integer" |
| 717 |
); |
| 718 |
assert( |
| 719 |
Number_isInteger(record.nanoseconds), |
| 720 |
"record.nanoseconds is an integer" |
| 721 |
); |
| 722 |
|
| 723 |
// Steps 1-2. (Loop unrolled) |
| 724 |
return ( |
| 725 |
std_Math_sign(record.years) || |
| 726 |
std_Math_sign(record.months) || |
| 727 |
std_Math_sign(record.weeks) || |
| 728 |
std_Math_sign(record.days) || |
| 729 |
std_Math_sign(record.hours) || |
| 730 |
std_Math_sign(record.minutes) || |
| 731 |
std_Math_sign(record.seconds) || |
| 732 |
std_Math_sign(record.milliseconds) || |
| 733 |
std_Math_sign(record.microseconds) || |
| 734 |
std_Math_sign(record.nanoseconds) |
| 735 |
); |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ) |
| 740 |
*/ |
| 741 |
function IsValidDurationSign(record) { |
| 742 |
// Step 1. |
| 743 |
var sign = DurationSign(record); |
| 744 |
|
| 745 |
// Fast-path for an all-zero duration. |
| 746 |
if (sign === 0) { |
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
// Step 2. (Loop unrolled) |
| 751 |
if ( |
| 752 |
std_Math_sign(record.years) === -sign || |
| 753 |
std_Math_sign(record.months) === -sign || |
| 754 |
std_Math_sign(record.weeks) === -sign || |
| 755 |
std_Math_sign(record.days) === -sign || |
| 756 |
std_Math_sign(record.hours) === -sign || |
| 757 |
std_Math_sign(record.minutes) === -sign || |
| 758 |
std_Math_sign(record.seconds) === -sign || |
| 759 |
std_Math_sign(record.milliseconds) === -sign || |
| 760 |
std_Math_sign(record.microseconds) === -sign || |
| 761 |
std_Math_sign(record.nanoseconds) === -sign |
| 762 |
) { |
| 763 |
return false; |
| 764 |
} |
| 765 |
|
| 766 |
// Steps 3-8. (Performed in caller) |
| 767 |
|
| 768 |
// Step 9. |
| 769 |
return true; |
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ) |
| 774 |
*/ |
| 775 |
function IsValidDurationLimit(record) { |
| 776 |
// Steps 1-2. (Performed in caller) |
| 777 |
assert(IsValidDurationSign(record), "duration has the correct sign"); |
| 778 |
|
| 779 |
// Steps 3-5. |
| 780 |
if ( |
| 781 |
std_Math_abs(record.years) >= 2**32 || |
| 782 |
std_Math_abs(record.months) >= 2**32 || |
| 783 |
std_Math_abs(record.weeks) >= 2**32 |
| 784 |
) { |
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
// Steps 6-7. |
| 789 |
var normalizedSeconds = ( |
| 790 |
record.days * 86_400 + |
| 791 |
record.hours * 3600 + |
| 792 |
record.minutes * 60 + |
| 793 |
record.seconds |
| 794 |
); |
| 795 |
|
| 796 |
var milliseconds = record.milliseconds; |
| 797 |
var microseconds = record.microseconds; |
| 798 |
var nanoseconds = record.nanoseconds; |
| 799 |
if ( |
| 800 |
std_Math_abs(milliseconds) < 2 ** 53 && |
| 801 |
std_Math_abs(microseconds) < 2 ** 53 && |
| 802 |
std_Math_abs(nanoseconds) < 2 ** 53 |
| 803 |
) { |
| 804 |
// Fast case for safe integers. |
| 805 |
normalizedSeconds += ( |
| 806 |
std_Math_trunc(milliseconds / 1e3) + |
| 807 |
std_Math_trunc(microseconds / 1e6) + |
| 808 |
std_Math_trunc(nanoseconds / 1e9) |
| 809 |
); |
| 810 |
} else { |
| 811 |
// Slow case for too large numbers. |
| 812 |
normalizedSeconds += BigIntToNumber( |
| 813 |
NumberToBigInt(milliseconds) / 1_000n + |
| 814 |
NumberToBigInt(microseconds) / 1_000_000n + |
| 815 |
NumberToBigInt(nanoseconds) / 1_000_000_000n |
| 816 |
); |
| 817 |
} |
| 818 |
normalizedSeconds += std_Math_trunc(( |
| 819 |
(milliseconds % 1e3) * 1e6 + |
| 820 |
(microseconds % 1e6) * 1e3 + |
| 821 |
(nanoseconds % 1e9) |
| 822 |
) / 1e9); |
| 823 |
|
| 824 |
// Step 8. |
| 825 |
if (std_Math_abs(normalizedSeconds) >= 2**53) { |
| 826 |
return false; |
| 827 |
} |
| 828 |
|
| 829 |
// Step 9. |
| 830 |
return true; |
| 831 |
} |
| 832 |
|
| 833 |
#ifdef DEBUG |
| 834 |
/** |
| 835 |
* IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ) |
| 836 |
*/ |
| 837 |
function IsValidDuration(record) { |
| 838 |
return IsValidDurationSign(record) && IsValidDurationLimit(record); |
| 839 |
} |
| 840 |
#endif |
| 841 |
|
| 842 |
/** |
| 843 |
* ComputeFractionalDigits ( durationFormat, duration ) |
| 844 |
* |
| 845 |
* Return the fractional seconds from |duration| as an exact value. This is |
| 846 |
* either an integer Number value when the fractional part is zero, or a |
| 847 |
* decimal string when the fractional part is non-zero. |
| 848 |
*/ |
| 849 |
function ComputeFractionalDigits(duration, unit) { |
| 850 |
assert( |
| 851 |
IsValidDuration(duration), |
| 852 |
"DurationToFractional called with non-valid duration" |
| 853 |
); |
| 854 |
|
| 855 |
var exponent; |
| 856 |
if (unit === "seconds") { |
| 857 |
exponent = 9; |
| 858 |
} else if (unit === "milliseconds") { |
| 859 |
exponent = 6; |
| 860 |
} else { |
| 861 |
assert(unit === "microseconds", "unexpected unit"); |
| 862 |
exponent = 3; |
| 863 |
} |
| 864 |
|
| 865 |
// Directly return the duration amount when no sub-seconds are present. |
| 866 |
switch (exponent) { |
| 867 |
case 9: { |
| 868 |
if ( |
| 869 |
duration.milliseconds === 0 && |
| 870 |
duration.microseconds === 0 && |
| 871 |
duration.nanoseconds === 0 |
| 872 |
) { |
| 873 |
return duration.seconds; |
| 874 |
} |
| 875 |
break; |
| 876 |
} |
| 877 |
|
| 878 |
case 6: { |
| 879 |
if (duration.microseconds === 0 && duration.nanoseconds === 0) { |
| 880 |
return duration.milliseconds; |
| 881 |
} |
| 882 |
break; |
| 883 |
} |
| 884 |
|
| 885 |
case 3: { |
| 886 |
if (duration.nanoseconds === 0) { |
| 887 |
return duration.microseconds; |
| 888 |
} |
| 889 |
break; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
// Otherwise compute the overall amount of nanoseconds using BigInt to avoid |
| 894 |
// loss of precision. |
| 895 |
var ns = NumberToBigInt(duration.nanoseconds); |
| 896 |
switch (exponent) { |
| 897 |
case 9: |
| 898 |
ns += NumberToBigInt(duration.seconds) * 1_000_000_000n; |
| 899 |
// fallthrough |
| 900 |
case 6: |
| 901 |
ns += NumberToBigInt(duration.milliseconds) * 1_000_000n; |
| 902 |
// fallthrough |
| 903 |
case 3: |
| 904 |
ns += NumberToBigInt(duration.microseconds) * 1_000n; |
| 905 |
} |
| 906 |
|
| 907 |
var e = NumberToBigInt(10 ** exponent); |
| 908 |
|
| 909 |
// Split the nanoseconds amount into an integer and its fractional part. |
| 910 |
var q = ns / e; |
| 911 |
var r = ns % e; |
| 912 |
|
| 913 |
// Pad fractional part, without any leading negative sign, to |exponent| digits. |
| 914 |
if (r < 0) { |
| 915 |
r = -r; |
| 916 |
} |
| 917 |
r = callFunction(String_pad_start, ToString(r), exponent, "0"); |
| 918 |
|
| 919 |
// Return the result as a decimal string. |
| 920 |
return `${q}.${r}`; |
| 921 |
} |
| 922 |
|
| 923 |
/** |
| 924 |
* FormatNumericHours ( durationFormat, hoursValue, signDisplayed ) |
| 925 |
* FormatNumericMinutes ( durationFormat, minutesValue, hoursDisplayed, signDisplayed ) |
| 926 |
* FormatNumericSeconds ( durationFormat, secondsValue, minutesDisplayed, signDisplayed ) |
| 927 |
*/ |
| 928 |
function FormatNumericHoursOrMinutesOrSeconds( |
| 929 |
internals, |
| 930 |
value, |
| 931 |
style, |
| 932 |
unit, |
| 933 |
signDisplayed, |
| 934 |
formatToParts |
| 935 |
) { |
| 936 |
assert( |
| 937 |
unit === "hour" || unit === "minute" || unit === "second", |
| 938 |
"unexpected unit: " + unit |
| 939 |
); |
| 940 |
|
| 941 |
// FormatNumericHours, step 1. (Not applicable in our implementation.) |
| 942 |
// FormatNumericMinutes, steps 1-3. (Not applicable in our implementation.) |
| 943 |
// FormatNumericSeconds, step 1. (Not applicable in our implementation.) |
| 944 |
|
| 945 |
// FormatNumericHours, step 2. |
| 946 |
// FormatNumericMinutes, step 4. |
| 947 |
// FormatNumericSeconds, step 2. |
| 948 |
assert(style === "numeric" || style === "2-digit", "invalid style: " + style); |
| 949 |
|
| 950 |
// FormatNumericHours, step 3. (Not applicable in our implementation.) |
| 951 |
// FormatNumericSeconds, steps 3-4. (Not applicable in our implementation.) |
| 952 |
|
| 953 |
// FormatNumericHours, step 4. |
| 954 |
// FormatNumericMinutes, step 5. |
| 955 |
// FormatNumericSeconds, step 5. |
| 956 |
var nfOpts = std_Object_create(null); |
| 957 |
|
| 958 |
// FormatNumericHours, step 5-6. |
| 959 |
// FormatNumericMinutes, steps 6-7. |
| 960 |
// FormatNumericSeconds, steps 6-7. |
| 961 |
nfOpts.numberingSystem = internals.numberingSystem; |
| 962 |
|
| 963 |
// FormatNumericHours, step 7. |
| 964 |
// FormatNumericMinutes, step 8. |
| 965 |
// FormatNumericSeconds, step 8. |
| 966 |
if (style === "2-digit") { |
| 967 |
nfOpts.minimumIntegerDigits = 2; |
| 968 |
} |
| 969 |
|
| 970 |
// FormatNumericHours, step 8. |
| 971 |
// FormatNumericMinutes, step 9. |
| 972 |
// FormatNumericSeconds, step 9. |
| 973 |
if (!signDisplayed) { |
| 974 |
nfOpts.signDisplay = "never"; |
| 975 |
} |
| 976 |
|
| 977 |
// FormatNumericHours, step 9. |
| 978 |
// FormatNumericMinutes, step 10. |
| 979 |
// FormatNumericSeconds, step 10. |
| 980 |
nfOpts.useGrouping = false; |
| 981 |
|
| 982 |
if (unit === "second") { |
| 983 |
// FormatNumericSeconds, steps 11-14. |
| 984 |
var fractionalDigits = internals.fractionalDigits; |
| 985 |
nfOpts.maximumFractionDigits = fractionalDigits ?? 9; |
| 986 |
nfOpts.minimumFractionDigits = fractionalDigits ?? 0; |
| 987 |
|
| 988 |
// FormatNumericSeconds, step 15. |
| 989 |
nfOpts.roundingMode = "trunc"; |
| 990 |
} |
| 991 |
|
| 992 |
// FormatNumericHours, step 10. |
| 993 |
// FormatNumericMinutes, step 11. |
| 994 |
// FormatNumericSeconds, step 16. |
| 995 |
var nf = intl_NumberFormat(internals.locale, nfOpts); |
| 996 |
|
| 997 |
// FormatNumericHours, step 11. |
| 998 |
// FormatNumericMinutes, step 12. |
| 999 |
// FormatNumericSeconds, step 17. |
| 1000 |
var parts = intl_FormatNumber(nf, value, formatToParts); |
| 1001 |
|
| 1002 |
// FormatNumericHours, step 12. |
| 1003 |
// FormatNumericMinutes, step 13. |
| 1004 |
// FormatNumericSeconds, step 18. |
| 1005 |
if (formatToParts) { |
| 1006 |
assert(IsArray(parts), "parts is an array"); |
| 1007 |
|
| 1008 |
// Modify the parts in-place instead of creating new objects. |
| 1009 |
for (var i = 0; i < parts.length; i++) { |
| 1010 |
DefineDataProperty(parts[i], "unit", unit); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
// FormatNumericHours, step 13. |
| 1015 |
// FormatNumericMinutes, step 14. |
| 1016 |
// FormatNumericSeconds, step 19. |
| 1017 |
return parts; |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* eslint-disable complexity */ |
| 1021 |
/** |
| 1022 |
* FormatNumericUnits ( durationFormat, duration, firstNumericUnit, signDisplayed ) |
| 1023 |
*/ |
| 1024 |
function FormatNumericUnits( |
| 1025 |
internals, |
| 1026 |
duration, |
| 1027 |
firstNumericUnit, |
| 1028 |
signDisplayed, |
| 1029 |
formatToParts |
| 1030 |
) { |
| 1031 |
// Step 1. |
| 1032 |
assert( |
| 1033 |
firstNumericUnit === "hours" || |
| 1034 |
firstNumericUnit === "minutes" || |
| 1035 |
firstNumericUnit === "seconds", |
| 1036 |
"invalid numeric unit: " + firstNumericUnit |
| 1037 |
); |
| 1038 |
|
| 1039 |
// Step 2. |
| 1040 |
var numericPartsList; |
| 1041 |
if (formatToParts) { |
| 1042 |
numericPartsList = []; |
| 1043 |
} else { |
| 1044 |
numericPartsList = ""; |
| 1045 |
} |
| 1046 |
|
| 1047 |
// Step 3. |
| 1048 |
var hoursValue = duration.hours; |
| 1049 |
|
| 1050 |
// Step 4. |
| 1051 |
var hoursDisplay = internals.hoursDisplay; |
| 1052 |
|
| 1053 |
// Step 5. |
| 1054 |
var minutesValue = duration.minutes; |
| 1055 |
|
| 1056 |
// Step 6. |
| 1057 |
var minutesDisplay = internals.minutesDisplay; |
| 1058 |
|
| 1059 |
// Steps 7-8. |
| 1060 |
var secondsValue = ComputeFractionalDigits(duration, "seconds"); |
| 1061 |
|
| 1062 |
// Step 9. |
| 1063 |
var secondsDisplay = internals.secondsDisplay; |
| 1064 |
|
| 1065 |
// Step 10. |
| 1066 |
var hoursFormatted = false; |
| 1067 |
|
| 1068 |
// Step 11. |
| 1069 |
if (firstNumericUnit === "hours") { |
| 1070 |
// Step 11.a. |
| 1071 |
hoursFormatted = hoursValue !== 0 || hoursDisplay === "always"; |
| 1072 |
} |
| 1073 |
|
| 1074 |
// Steps 12-13. |
| 1075 |
var secondsFormatted = secondsValue !== 0 || secondsDisplay === "always"; |
| 1076 |
|
| 1077 |
// Step 14. |
| 1078 |
var minutesFormatted = false; |
| 1079 |
|
| 1080 |
// Step 15. |
| 1081 |
if (firstNumericUnit === "hours" || firstNumericUnit === "minutes") { |
| 1082 |
// Steps 15.a-b. |
| 1083 |
minutesFormatted = ( |
| 1084 |
(hoursFormatted && secondsFormatted) || |
| 1085 |
minutesValue !== 0 || |
| 1086 |
minutesDisplay === "always" |
| 1087 |
); |
| 1088 |
} |
| 1089 |
|
| 1090 |
// Return early when no units are displayed. |
| 1091 |
if (!hoursFormatted && !minutesFormatted && !secondsFormatted) { |
| 1092 |
return undefined; |
| 1093 |
} |
| 1094 |
|
| 1095 |
var timeSeparator; |
| 1096 |
if ( |
| 1097 |
(minutesFormatted && hoursFormatted) || |
| 1098 |
(secondsFormatted && minutesFormatted) |
| 1099 |
) { |
| 1100 |
timeSeparator = intl_GetTimeSeparator( |
| 1101 |
internals.locale, |
| 1102 |
internals.numberingSystem |
| 1103 |
); |
| 1104 |
} |
| 1105 |
|
| 1106 |
// Step 16. |
| 1107 |
if (hoursFormatted) { |
| 1108 |
// Step 16.a. |
| 1109 |
if (signDisplayed && hoursValue === 0 && DurationSign(duration) < 0) { |
| 1110 |
hoursValue = -0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
// Step 16.b. |
| 1114 |
var hoursParts = FormatNumericHoursOrMinutesOrSeconds( |
| 1115 |
internals, |
| 1116 |
hoursValue, |
| 1117 |
internals.hoursStyle, |
| 1118 |
"hour", |
| 1119 |
signDisplayed, |
| 1120 |
formatToParts |
| 1121 |
); |
| 1122 |
if (formatToParts) { |
| 1123 |
for (var i = 0; i < hoursParts.length; i++) { |
| 1124 |
DefineDataProperty(numericPartsList, numericPartsList.length, hoursParts[i]); |
| 1125 |
} |
| 1126 |
} else { |
| 1127 |
numericPartsList += hoursParts; |
| 1128 |
} |
| 1129 |
|
| 1130 |
// Step 16.c. |
| 1131 |
signDisplayed = false; |
| 1132 |
} |
| 1133 |
|
| 1134 |
// Step 17. |
| 1135 |
if (minutesFormatted) { |
| 1136 |
// Step 17.a. |
| 1137 |
if (signDisplayed && minutesValue === 0 && DurationSign(duration) < 0) { |
| 1138 |
minutesValue = -0; |
| 1139 |
} |
| 1140 |
|
| 1141 |
// Step 17.b. |
| 1142 |
if (hoursFormatted) { |
| 1143 |
if (formatToParts) { |
| 1144 |
DefineDataProperty(numericPartsList, numericPartsList.length, { |
| 1145 |
type: "literal", |
| 1146 |
value: timeSeparator, |
| 1147 |
}); |
| 1148 |
} else { |
| 1149 |
numericPartsList += timeSeparator; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
var minutesParts = FormatNumericHoursOrMinutesOrSeconds( |
| 1154 |
internals, |
| 1155 |
minutesValue, |
| 1156 |
internals.minutesStyle, |
| 1157 |
"minute", |
| 1158 |
signDisplayed, |
| 1159 |
formatToParts |
| 1160 |
); |
| 1161 |
if (formatToParts) { |
| 1162 |
for (var i = 0; i < minutesParts.length; i++) { |
| 1163 |
DefineDataProperty(numericPartsList, numericPartsList.length, minutesParts[i]); |
| 1164 |
} |
| 1165 |
} else { |
| 1166 |
numericPartsList += minutesParts; |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Step 17.c. |
| 1170 |
signDisplayed = false; |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Step 18. |
| 1174 |
if (secondsFormatted) { |
| 1175 |
// Step 18.a. |
| 1176 |
if (minutesFormatted) { |
| 1177 |
if (formatToParts) { |
| 1178 |
DefineDataProperty(numericPartsList, numericPartsList.length, { |
| 1179 |
type: "literal", |
| 1180 |
value: timeSeparator, |
| 1181 |
}); |
| 1182 |
} else { |
| 1183 |
numericPartsList += timeSeparator; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
var secondsParts = FormatNumericHoursOrMinutesOrSeconds( |
| 1188 |
internals, |
| 1189 |
secondsValue, |
| 1190 |
internals.secondsStyle, |
| 1191 |
"second", |
| 1192 |
signDisplayed, |
| 1193 |
formatToParts |
| 1194 |
); |
| 1195 |
if (formatToParts) { |
| 1196 |
for (var i = 0; i < secondsParts.length; i++) { |
| 1197 |
DefineDataProperty(numericPartsList, numericPartsList.length, secondsParts[i]); |
| 1198 |
} |
| 1199 |
} else { |
| 1200 |
numericPartsList += secondsParts; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
// Step 19. |
| 1205 |
return numericPartsList; |
| 1206 |
} |
| 1207 |
/* eslint-enable complexity */ |
| 1208 |
|
| 1209 |
/** |
| 1210 |
* ListFormatParts ( durationFormat, partitionedPartsList ) |
| 1211 |
*/ |
| 1212 |
function ListFormatParts(internals, partitionedPartsList, formatToParts) { |
| 1213 |
assert(IsArray(partitionedPartsList), "partitionedPartsList is an array"); |
| 1214 |
|
| 1215 |
// Step 1. |
| 1216 |
var lfOpts = std_Object_create(null); |
| 1217 |
|
| 1218 |
// Step 2. |
| 1219 |
lfOpts.type = "unit"; |
| 1220 |
|
| 1221 |
// Step 3. |
| 1222 |
var listStyle = internals.style; |
| 1223 |
|
| 1224 |
// Step 4. |
| 1225 |
if (listStyle === "digital") { |
| 1226 |
listStyle = "short"; |
| 1227 |
} |
| 1228 |
|
| 1229 |
// Step 5. |
| 1230 |
lfOpts.style = listStyle; |
| 1231 |
|
| 1232 |
// Step 6. |
| 1233 |
var ListFormat = GetBuiltinConstructor("ListFormat"); |
| 1234 |
var lf = new ListFormat(internals.locale, lfOpts); |
| 1235 |
|
| 1236 |
// Steps 7-14. |
| 1237 |
if (!formatToParts) { |
| 1238 |
return intl_FormatList(lf, partitionedPartsList, /* formatToParts = */ false); |
| 1239 |
} |
| 1240 |
|
| 1241 |
// <https://unicode.org/reports/tr35/tr35-general.html#ListPatterns> requires |
| 1242 |
// that the list patterns are sorted, for example "{1} and {0}" isn't a valid |
| 1243 |
// pattern, because "{1}" appears before "{0}". This requirement also means |
| 1244 |
// all entries appear in order in the formatted result. |
| 1245 |
|
| 1246 |
// Step 7. |
| 1247 |
var strings = []; |
| 1248 |
|
| 1249 |
// Step 8. |
| 1250 |
for (var i = 0; i < partitionedPartsList.length; i++) { |
| 1251 |
var parts = partitionedPartsList[i]; |
| 1252 |
assert(IsArray(parts), "parts is an array"); |
| 1253 |
|
| 1254 |
// Step 8.a. |
| 1255 |
var string = ""; |
| 1256 |
|
| 1257 |
// Step 8.b. |
| 1258 |
// |
| 1259 |
// Combine the individual number-formatted parts into a single string. |
| 1260 |
for (var j = 0; j < parts.length; j++) { |
| 1261 |
var part = parts[j]; |
| 1262 |
assert( |
| 1263 |
hasOwn("type", part) && |
| 1264 |
hasOwn("value", part) && |
| 1265 |
typeof part.value === "string", |
| 1266 |
"part is a number-formatted element" |
| 1267 |
); |
| 1268 |
|
| 1269 |
string += part.value; |
| 1270 |
} |
| 1271 |
|
| 1272 |
// Step 8.c. |
| 1273 |
DefineDataProperty(strings, strings.length, string); |
| 1274 |
} |
| 1275 |
|
| 1276 |
// Step 9. |
| 1277 |
var formattedPartsList = intl_FormatList(lf, strings, /* formatToParts = */ true); |
| 1278 |
|
| 1279 |
// Step 10. |
| 1280 |
var partitionedPartsIndex = 0; |
| 1281 |
|
| 1282 |
// Step 11. (Not applicable in our implementation.) |
| 1283 |
|
| 1284 |
// Step 12. |
| 1285 |
var flattenedPartsList = []; |
| 1286 |
|
| 1287 |
// Step 13. |
| 1288 |
for (var i = 0; i < formattedPartsList.length; i++) { |
| 1289 |
var listPart = formattedPartsList[i]; |
| 1290 |
assert( |
| 1291 |
hasOwn("type", listPart) && |
| 1292 |
hasOwn("value", listPart) && |
| 1293 |
typeof listPart.type === "string", |
| 1294 |
"part is a list-formatted element" |
| 1295 |
); |
| 1296 |
|
| 1297 |
// Steps 13.a-b. |
| 1298 |
if (listPart.type === "element") { |
| 1299 |
// Step 13.a.i. |
| 1300 |
assert( |
| 1301 |
partitionedPartsIndex < partitionedPartsList.length, |
| 1302 |
"resultIndex is an index into result" |
| 1303 |
); |
| 1304 |
|
| 1305 |
// Step 13.a.ii. |
| 1306 |
var parts = partitionedPartsList[partitionedPartsIndex]; |
| 1307 |
assert(IsArray(parts), "parts is an array"); |
| 1308 |
|
| 1309 |
// Step 13.a.iii. |
| 1310 |
// |
| 1311 |
// Replace "element" parts with the number-formatted result. |
| 1312 |
for (var j = 0; j < parts.length; j++) { |
| 1313 |
DefineDataProperty(flattenedPartsList, flattenedPartsList.length, parts[j]); |
| 1314 |
} |
| 1315 |
|
| 1316 |
// Step 13.a.iv. |
| 1317 |
partitionedPartsIndex += 1; |
| 1318 |
} else { |
| 1319 |
// Step 13.b.i. |
| 1320 |
assert(listPart.type === "literal", "literal part"); |
| 1321 |
|
| 1322 |
// Step 13.b.ii. |
| 1323 |
// |
| 1324 |
// Append "literal" parts as-is. |
| 1325 |
DefineDataProperty(flattenedPartsList, flattenedPartsList.length, listPart); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
assert( |
| 1329 |
partitionedPartsIndex === partitionedPartsList.length, |
| 1330 |
"all number-formatted parts handled" |
| 1331 |
); |
| 1332 |
|
| 1333 |
// Step 14. |
| 1334 |
return flattenedPartsList; |
| 1335 |
} |
| 1336 |
|
| 1337 |
/** |
| 1338 |
* PartitionDurationFormatPattern ( durationFormat, duration ) |
| 1339 |
*/ |
| 1340 |
function PartitionDurationFormatPattern( |
| 1341 |
durationFormat, |
| 1342 |
duration, |
| 1343 |
formatToParts |
| 1344 |
) { |
| 1345 |
assert( |
| 1346 |
IsObject(durationFormat), |
| 1347 |
"PartitionDurationFormatPattern called with non-object" |
| 1348 |
); |
| 1349 |
assert( |
| 1350 |
intl_GuardToDurationFormat(durationFormat) !== null, |
| 1351 |
"PartitionDurationFormatPattern called with non-DurationFormat" |
| 1352 |
); |
| 1353 |
assert( |
| 1354 |
IsValidDuration(duration), |
| 1355 |
"PartitionDurationFormatPattern called with non-valid duration" |
| 1356 |
); |
| 1357 |
assert( |
| 1358 |
typeof formatToParts === "boolean", |
| 1359 |
"PartitionDurationFormatPattern called with non-boolean formatToParts" |
| 1360 |
); |
| 1361 |
|
| 1362 |
var units = [ |
| 1363 |
"years", |
| 1364 |
"months", |
| 1365 |
"weeks", |
| 1366 |
"days", |
| 1367 |
"hours", |
| 1368 |
"minutes", |
| 1369 |
"seconds", |
| 1370 |
"milliseconds", |
| 1371 |
"microseconds", |
| 1372 |
"nanoseconds", |
| 1373 |
]; |
| 1374 |
|
| 1375 |
var internals = getDurationFormatInternals(durationFormat); |
| 1376 |
|
| 1377 |
// Step 1. |
| 1378 |
var result = []; |
| 1379 |
|
| 1380 |
// Step 2. |
| 1381 |
var signDisplayed = true; |
| 1382 |
|
| 1383 |
// Step 3. |
| 1384 |
var numericUnitFound = false; |
| 1385 |
|
| 1386 |
// Step 4. |
| 1387 |
for (var i = 0; !numericUnitFound && i < units.length; i++) { |
| 1388 |
// Step 4.d. (Reordered) |
| 1389 |
var unit = units[i]; |
| 1390 |
|
| 1391 |
// Step 4.a. |
| 1392 |
var value = duration[unit]; |
| 1393 |
|
| 1394 |
// Step 4.b. |
| 1395 |
var style = internals[unit + "Style"]; |
| 1396 |
|
| 1397 |
// Step 4.c. |
| 1398 |
var display = internals[unit + "Display"]; |
| 1399 |
|
| 1400 |
// Step 4.d. (Moved above) |
| 1401 |
|
| 1402 |
// Steps 4.e-f. |
| 1403 |
if (style === "numeric" || style === "2-digit") { |
| 1404 |
// Step 4.e.1. |
| 1405 |
var numericPartsList = FormatNumericUnits( |
| 1406 |
internals, |
| 1407 |
duration, |
| 1408 |
unit, |
| 1409 |
signDisplayed, |
| 1410 |
formatToParts |
| 1411 |
); |
| 1412 |
if (numericPartsList !== undefined) { |
| 1413 |
DefineDataProperty(result, result.length, numericPartsList); |
| 1414 |
} |
| 1415 |
|
| 1416 |
// Step 4.e.2. |
| 1417 |
numericUnitFound = true; |
| 1418 |
} else { |
| 1419 |
// Step 4.f.i. |
| 1420 |
var nfOpts = std_Object_create(null); |
| 1421 |
|
| 1422 |
// Step 4.f.ii. |
| 1423 |
if ( |
| 1424 |
unit === "seconds" || |
| 1425 |
unit === "milliseconds" || |
| 1426 |
unit === "microseconds" |
| 1427 |
) { |
| 1428 |
// Step 4.f.ii.1. |
| 1429 |
if (internals[units[i + 1] + "Style"] === "numeric") { |
| 1430 |
// Step 4.f.ii.1.a. |
| 1431 |
value = ComputeFractionalDigits(duration, unit); |
| 1432 |
|
| 1433 |
// Steps 4.f.ii.1.b-e. |
| 1434 |
var fractionalDigits = internals.fractionalDigits; |
| 1435 |
nfOpts.maximumFractionDigits = fractionalDigits ?? 9; |
| 1436 |
nfOpts.minimumFractionDigits = fractionalDigits ?? 0; |
| 1437 |
|
| 1438 |
// Step 4.f.ii.1.f. |
| 1439 |
nfOpts.roundingMode = "trunc"; |
| 1440 |
|
| 1441 |
// Step 4.f.ii.1.g. |
| 1442 |
numericUnitFound = true; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
// Step 4.f.iii. (Condition inverted to reduce indentation.) |
| 1447 |
if (value === 0 && display === "auto") { |
| 1448 |
continue; |
| 1449 |
} |
| 1450 |
|
| 1451 |
// Steps 4.f.iii.1-2. |
| 1452 |
nfOpts.numberingSystem = internals.numberingSystem; |
| 1453 |
|
| 1454 |
// Steps 4.f.iii.3-4. |
| 1455 |
if (signDisplayed) { |
| 1456 |
// Step 4.f.iii.3.a. |
| 1457 |
signDisplayed = false; |
| 1458 |
|
| 1459 |
// Step 4.f.iii.3.b. |
| 1460 |
if (value === 0 && DurationSign(duration) < 0) { |
| 1461 |
value = -0; |
| 1462 |
} |
| 1463 |
} else { |
| 1464 |
// Step 4.f.iii.4.b. |
| 1465 |
nfOpts.signDisplay = "never"; |
| 1466 |
} |
| 1467 |
|
| 1468 |
// Step 4.f.iii.5. |
| 1469 |
// |
| 1470 |
// Remove the trailing 's' from the unit name. |
| 1471 |
var numberFormatUnit = Substring(unit, 0, unit.length - 1); |
| 1472 |
|
| 1473 |
// Step 4.f.iii.6. |
| 1474 |
nfOpts.style = "unit"; |
| 1475 |
|
| 1476 |
// Step 4.f.iii.7. |
| 1477 |
nfOpts.unit = numberFormatUnit; |
| 1478 |
|
| 1479 |
// Step 4.f.iii.8. |
| 1480 |
nfOpts.unitDisplay = style; |
| 1481 |
|
| 1482 |
// Step 4.f.iii.9. |
| 1483 |
var nf = intl_NumberFormat(internals.locale, nfOpts); |
| 1484 |
|
| 1485 |
// Step 4.f.iii.10. |
| 1486 |
var parts = intl_FormatNumber(nf, value, formatToParts); |
| 1487 |
|
| 1488 |
// Steps 4.f.iii.11-12. |
| 1489 |
if (formatToParts) { |
| 1490 |
assert(IsArray(parts), "parts is an array"); |
| 1491 |
|
| 1492 |
// Modify the parts in-place instead of creating new objects. |
| 1493 |
for (var j = 0; j < parts.length; j++) { |
| 1494 |
DefineDataProperty(parts[j], "unit", numberFormatUnit); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
// Step 4.f.iii.13. |
| 1499 |
DefineDataProperty(result, result.length, parts); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
// Step 5. |
| 1504 |
return ListFormatParts(internals, result, formatToParts); |
| 1505 |
} |
| 1506 |
|
| 1507 |
/** |
| 1508 |
* Intl.DurationFormat.prototype.format ( durationLike ) |
| 1509 |
*/ |
| 1510 |
function Intl_DurationFormat_format(durationLike) { |
| 1511 |
// Step 1. |
| 1512 |
var df = this; |
| 1513 |
|
| 1514 |
// Step 2. |
| 1515 |
if (!IsObject(df) || (df = intl_GuardToDurationFormat(df)) === null) { |
| 1516 |
return callFunction( |
| 1517 |
intl_CallDurationFormatMethodIfWrapped, |
| 1518 |
this, |
| 1519 |
durationLike, |
| 1520 |
"Intl_DurationFormat_format" |
| 1521 |
); |
| 1522 |
} |
| 1523 |
|
| 1524 |
// Step 3. |
| 1525 |
var duration = ToTemporalDuration(durationLike); |
| 1526 |
|
| 1527 |
// Fallback if Temporal is disabled. |
| 1528 |
if (duration === null) { |
| 1529 |
duration = ToDurationRecord(durationLike); |
| 1530 |
} |
| 1531 |
|
| 1532 |
// Steps 4-7. |
| 1533 |
return PartitionDurationFormatPattern( |
| 1534 |
df, |
| 1535 |
duration, |
| 1536 |
/* formatToParts = */ false |
| 1537 |
); |
| 1538 |
} |
| 1539 |
|
| 1540 |
/** |
| 1541 |
* Intl.DurationFormat.prototype.formatToParts ( durationLike ) |
| 1542 |
*/ |
| 1543 |
function Intl_DurationFormat_formatToParts(durationLike) { |
| 1544 |
// Step 1. |
| 1545 |
var df = this; |
| 1546 |
|
| 1547 |
// Step 2. |
| 1548 |
if (!IsObject(df) || (df = intl_GuardToDurationFormat(df)) === null) { |
| 1549 |
return callFunction( |
| 1550 |
intl_CallDurationFormatMethodIfWrapped, |
| 1551 |
this, |
| 1552 |
durationLike, |
| 1553 |
"Intl_DurationFormat_formatToParts" |
| 1554 |
); |
| 1555 |
} |
| 1556 |
|
| 1557 |
// Step 3. |
| 1558 |
var duration = ToTemporalDuration(durationLike); |
| 1559 |
|
| 1560 |
// Fallback if Temporal is disabled. |
| 1561 |
if (duration === null) { |
| 1562 |
duration = ToDurationRecord(durationLike); |
| 1563 |
} |
| 1564 |
|
| 1565 |
// Steps 4-8. |
| 1566 |
return PartitionDurationFormatPattern(df, duration, /* formatToParts = */ true); |
| 1567 |
} |
| 1568 |
|
| 1569 |
/** |
| 1570 |
* Returns the resolved options for a DurationFormat object. |
| 1571 |
*/ |
| 1572 |
function Intl_DurationFormat_resolvedOptions() { |
| 1573 |
// Step 1. |
| 1574 |
var durationFormat = this; |
| 1575 |
|
| 1576 |
// Step 2. |
| 1577 |
if ( |
| 1578 |
!IsObject(durationFormat) || |
| 1579 |
(durationFormat = intl_GuardToDurationFormat(durationFormat)) === null |
| 1580 |
) { |
| 1581 |
return callFunction( |
| 1582 |
intl_CallDurationFormatMethodIfWrapped, |
| 1583 |
this, |
| 1584 |
"Intl_DurationFormat_resolvedOptions" |
| 1585 |
); |
| 1586 |
} |
| 1587 |
|
| 1588 |
var internals = getDurationFormatInternals(durationFormat); |
| 1589 |
|
| 1590 |
// Steps 3-4. |
| 1591 |
var result = { |
| 1592 |
locale: internals.locale, |
| 1593 |
numberingSystem: internals.numberingSystem, |
| 1594 |
style: internals.style, |
| 1595 |
years: internals.yearsStyle, |
| 1596 |
yearsDisplay: internals.yearsDisplay, |
| 1597 |
months: internals.monthsStyle, |
| 1598 |
monthsDisplay: internals.monthsDisplay, |
| 1599 |
weeks: internals.weeksStyle, |
| 1600 |
weeksDisplay: internals.weeksDisplay, |
| 1601 |
days: internals.daysStyle, |
| 1602 |
daysDisplay: internals.daysDisplay, |
| 1603 |
hours: internals.hoursStyle, |
| 1604 |
hoursDisplay: internals.hoursDisplay, |
| 1605 |
minutes: internals.minutesStyle, |
| 1606 |
minutesDisplay: internals.minutesDisplay, |
| 1607 |
seconds: internals.secondsStyle, |
| 1608 |
secondsDisplay: internals.secondsDisplay, |
| 1609 |
milliseconds: internals.millisecondsStyle, |
| 1610 |
millisecondsDisplay: internals.millisecondsDisplay, |
| 1611 |
microseconds: internals.microsecondsStyle, |
| 1612 |
microsecondsDisplay: internals.microsecondsDisplay, |
| 1613 |
nanoseconds: internals.nanosecondsStyle, |
| 1614 |
nanosecondsDisplay: internals.nanosecondsDisplay, |
| 1615 |
}; |
| 1616 |
|
| 1617 |
if (internals.fractionalDigits !== undefined) { |
| 1618 |
DefineDataProperty(result, "fractionalDigits", internals.fractionalDigits); |
| 1619 |
} |
| 1620 |
|
| 1621 |
// Step 5. |
| 1622 |
return result; |
| 1623 |
} |