UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

77 lines (75 loc) • 5.73 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js"; import { plainDateFormatOptions } from "../../internal/plainFormatOptions.js"; import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js"; // src/plain/format/formatDateToParts.ts /** * Return the locale-formatted parts of a PlainDate. * * - Use the parts instead of a fixed field pattern. A pattern such as UTS #35's `dd/MM/yyyy` fixes * one field order for every locale; ECMA-402's `Intl.DateTimeFormat.prototype.formatToParts` * (§11.3.6; `FormatDateTimeToParts` §11.5.7) returns the fields in the order of the locale's * CLDR date pattern, so the caller controls presentation and the locale keeps control of order. * - Each part is `{ type, value }` where `type` is one of: * `"era"`, `"year"`, `"relatedYear"`, `"yearName"`, `"month"`, `"day"`, `"weekday"`, * `"literal"`. * - A lunisolar calendar adds two more: `zh-u-ca-chinese` and `ko-u-ca-dangi` emit * `"relatedYear"` (the Gregorian year the cycle falls in) and `"yearName"` (the sexagenary * cycle name) in place of, or beside, `"year"`. Code that looks up a `"year"` part must handle * their absence. This holds on a runtime that prints the observatory's Chinese or Dangi dates * (see the next item). * - A `chinese` or `dangi` date follows the observatory the standard names: Purple Mountain * Observatory for `chinese`, KASI for `dangi`. A runtime that prints a different date for one of * them returns `[]` for any format with a year, month, day or era in that calendar. A runtime * that prints the observatory's date keeps its own parts. A format with only a weekday * field always returns its parts. * - The caller should iterate the array as returned; reassembling in a fixed * order reintroduces exactly the bug `formatToParts` exists to avoid. * - A PlainDate has no time or zone, so no `hour`/`minute`/`second`/`dayPeriod`/`timeZoneName` * part is ever returned, following Temporal's ECMA-402 PlainDate format: `timeStyle` without * `dateStyle`, or only time fields, yields `[]` (as `formatDate` yields `""`); `timeZoneName` is * ignored. With both `dateStyle` and `timeStyle` the parts keep the date style and drop the * `timeStyle` (the `Intl.DateTimeFormat` entry point, required ~any~), whereas the text sibling * `formatDate` returns `""` (`PlainDate#toLocaleString`, required ~date~, is a TypeError). * - The `formatZonedToParts` example below writes the literal before the day period as * `"\u202f"`, U+202F NARROW NO-BREAK SPACE, which Node 22 and 24 return; Node 26 returns an * ordinary space there (see `formatZonedToParts`). * - Returns `[]` for invalid input. * - **Compatibility:** before 1.16.0 time options leaked a UTC midnight and a `"UTC"` zone name * into the parts. `formatZonedToParts` on the date at midnight UTC returns those parts. * - `options` null returns `[]`, as ECMA-402's CoerceOptionsToObject rejects it. Any * other non-object — a string, a number, a boolean — formats with the defaults, as * `Intl.DateTimeFormat` does: CoerceOptionsToObject calls ToObject on it and the wrapper * carries no recognised option. Only `null` and `undefined` are special-cased. * * @param value ISO PlainDate string * @param locale optional BCP 47 locale identifier (default: runtime default), or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns array of `{ type, value }` parts, or `[]` on invalid input * * @example formatDateToParts("2024-03-15", "zh-CN") // [{ type: "year", value: "2024" }, { type: "literal", value: "/" }, { type: "month", value: "3" }, { type: "literal", value: "/" }, { type: "day", value: "15" }] * @example formatDateToParts("2024-03-15", "de-DE") // [{ type: "day", value: "15" }, { type: "literal", value: "." }, { type: "month", value: "3" }, { type: "literal", value: "." }, { type: "year", value: "2024" }] * @example formatDateToParts("2024-02-29", "hi-IN", { dateStyle: "full", timeStyle: "full" }) // [{ type: "weekday", value: "गुरुवार" }, { type: "literal", value: ", " }, { type: "day", value: "29" }, { type: "literal", value: " " }, { type: "month", value: "फ़रवरी" }, { type: "literal", value: " " }, { type: "year", value: "2024" }] * @example formatDateToParts("2024-02-29", "en-US", { timeStyle: "long" }) // [] — a PlainDate has no time * @example formatZonedToParts("2024-02-29T00:00:00+00:00[UTC]", "en-US", { timeStyle: "long" }) // [{ type: "hour", value: "12" }, { type: "literal", value: ":" }, { type: "minute", value: "00" }, { type: "literal", value: ":" }, { type: "second", value: "00" }, { type: "literal", value: "\u202f" }, { type: "dayPeriod", value: "AM" }, { type: "literal", value: " " }, { type: "timeZoneName", value: "UTC" }] — the pre-1.16.0 parts * @example formatDateToParts("invalid") // [] * @example formatDateToParts("2024-02-03", ["fr-FR", "en-US"]) // [{ type: "day", value: "03" }, { type: "literal", value: "/" }, …] */ function formatDateToParts(value, locale, options) { const valueParsed = plainDateOf(value); if (valueParsed === null) return []; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); const resolved = plainDateFormatOptions(read); if (resolved === null) return []; const epochMilliseconds = valueParsed.toZonedDateTime("UTC").epochMilliseconds; return cachedFormatter("DateTimeFormat", locale, resolved).formatToParts(epochMilliseconds).map((p) => ({ type: p.type, value: p.value })); } catch { return []; } } export { formatDateToParts };