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.

51 lines (49 loc) • 2.7 kB
import { isJapaneseNumberingSystemCompatNeeded } from "./temporalCompat/capabilities.js"; import { digitRuns, digitSetOf } from "./weekYearText.js"; import { showsEraOrYear } from "./yearPrinting.js"; // src/internal/japaneseNumerals.ts /** * The formatter itself, or a wrapper that writes the era year of a `ja` japanese formatter in the * formatter's numbering system (D27, ICU's `jpanyear` ignores `nu`; ECMA-402 FormatNumeric). * * `jpanyear` prints the era year in Latin digits while every other number honours `nu`, so each run * of Latin digits in the text is the era year: no parts are read from the text, and the week-year * guard above this wrapper sees the same digits in every field. `year: "2-digit"` becomes * `minimumIntegerDigits: 2`. 元 is a name, not a number, and stays. * * @param formatter the formatter to wrap (the engine's, before the week-year guard) * @param callerOptions the options it was built with * @returns the formatter, or its wrapper * * @example repairEraYearNumerals(new Intl.DateTimeFormat("en"), {}) // the same formatter * @example repairEraYearNumerals(new Intl.DateTimeFormat("ja-u-ca-japanese-nu-arab", { year: "numeric", timeZone: "UTC" }), { year: "numeric", timeZone: "UTC" }).format(Date.UTC(2024, 6, 10)) // "令和٦年" where the engine prints 令和6年 */ function repairEraYearNumerals(formatter, callerOptions) { let resolved; try { resolved = formatter.resolvedOptions(); } catch { return formatter; } const { calendar, numberingSystem } = resolved; if (calendar !== "japanese" || numberingSystem === "latn" || !showsEraOrYear(callerOptions) || new Intl.Locale(resolved.locale).language !== "ja" || !isJapaneseNumberingSystemCompatNeeded()) return formatter; const latin = digitSetOf("latn"); const yearDigits = new Intl.NumberFormat(resolved.locale, { numberingSystem, useGrouping: false, minimumIntegerDigits: callerOptions.year === "2-digit" ? 2 : 1 }); const written = (text) => digitRuns(text, latin).filter((run) => [...run.text].every((digit) => latin.has(digit))).toReversed().reduce((out, run) => out.slice(0, run.start) + yearDigits.format(Number(run.text)) + out.slice(run.start + run.text.length), text); const writtenParts = (parts) => parts.map((part) => part.type === "year" ? { ...part, value: written(part.value) } : part); return { format: (date) => written(formatter.format(date)), formatToParts: (date) => writtenParts(formatter.formatToParts(date)), formatRange: (start, end) => written(formatter.formatRange(start, end)), formatRangeToParts: (start, end) => writtenParts(formatter.formatRangeToParts(start, end)), resolvedOptions: () => resolved }; } export { repairEraYearNumerals };