(
date: DateArg<Date> & {},
formatStr: string,
options?: FormatOptions,
)
| 344 | * //=> "3 o'clock" |
| 345 | */ |
| 346 | export function format( |
| 347 | date: DateArg<Date> & {}, |
| 348 | formatStr: string, |
| 349 | options?: FormatOptions, |
| 350 | ): string { |
| 351 | const defaultOptions = getDefaultOptions(); |
| 352 | const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale; |
| 353 | |
| 354 | const firstWeekContainsDate = |
| 355 | options?.firstWeekContainsDate ?? |
| 356 | options?.locale?.options?.firstWeekContainsDate ?? |
| 357 | defaultOptions.firstWeekContainsDate ?? |
| 358 | defaultOptions.locale?.options?.firstWeekContainsDate ?? |
| 359 | 1; |
| 360 | |
| 361 | const weekStartsOn = |
| 362 | options?.weekStartsOn ?? |
| 363 | options?.locale?.options?.weekStartsOn ?? |
| 364 | defaultOptions.weekStartsOn ?? |
| 365 | defaultOptions.locale?.options?.weekStartsOn ?? |
| 366 | 0; |
| 367 | |
| 368 | const originalDate = toDate(date, options?.in); |
| 369 | |
| 370 | if (!isValid(originalDate)) { |
| 371 | throw new RangeError("Invalid time value"); |
| 372 | } |
| 373 | |
| 374 | let parts: FormatPart[] = formatStr |
| 375 | .match(longFormattingTokensRegExp)! |
| 376 | .map((substring) => { |
| 377 | const firstCharacter = substring[0]; |
| 378 | if (firstCharacter === "p" || firstCharacter === "P") { |
| 379 | const longFormatter = longFormatters[firstCharacter]; |
| 380 | return longFormatter(substring, locale.formatLong); |
| 381 | } |
| 382 | return substring; |
| 383 | }) |
| 384 | .join("") |
| 385 | .match(formattingTokensRegExp)! |
| 386 | .map((substring) => { |
| 387 | // Replace two single quote characters with one single quote character |
| 388 | if (substring === "''") { |
| 389 | return { isToken: false, value: "'" }; |
| 390 | } |
| 391 | |
| 392 | const firstCharacter = substring[0]; |
| 393 | if (firstCharacter === "'") { |
| 394 | return { isToken: false, value: cleanEscapedString(substring) }; |
| 395 | } |
| 396 | |
| 397 | if (formatters[firstCharacter]) { |
| 398 | return { isToken: true, value: substring }; |
| 399 | } |
| 400 | |
| 401 | if (firstCharacter.match(unescapedLatinCharacterRegExp)) { |
| 402 | throw new RangeError( |
| 403 | "Format string contains an unescaped latin alphabet character `" + |
no test coverage detected