@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.
102 lines (100 loc) • 4.04 kB
JavaScript
import { stringOptionOrDefault } from "./typedOptions.js";
import { cachedFormatter } from "./formatterCache.js";
import { normalizeDateTime } from "./normalizeDateTime.js";
import { resolveRelativeRounding } from "./resolveRelativeRounding.js";
import { resolveRelativeUnit } from "./resolveRelativeUnit.js";
// src/internal/formatRelativeDuration.ts
const RELATIVE_UNITS = [
"year",
"month",
"week",
"day",
"hour",
"minute",
"second"
];
const SECONDS_PER_DAY = 86400;
const AUTO_UNITS = [
{
unit: "second",
maxSeconds: 60
},
{
unit: "minute",
maxSeconds: 3600
},
{
unit: "hour",
maxSeconds: SECONDS_PER_DAY
},
{
unit: "day",
maxSeconds: 7 * SECONDS_PER_DAY
},
{
unit: "week",
maxSeconds: 28 * SECONDS_PER_DAY
},
{
unit: "month",
maxSeconds: 365 * SECONDS_PER_DAY
},
{
unit: "year",
maxSeconds: Infinity
}
];
/**
* Render a signed distance with `Intl.RelativeTimeFormat`, picking the unit from the distance when
* `largestUnit` is omitted. The shared tail of the plain, utc, unix and zoned relative formatters.
*
* @param diff the signed distance (target since reference)
* @param locale BCP 47 locale(s) passed through to `Intl.RelativeTimeFormat`
* @param options display style, numeric mode, largest unit and rounding method
* @param calendarTotal the total in `unit` against the caller's anchor; called only when the unit
* is calendrical (month, year) and `diff.total(unit)` throws without a `relativeTo`
* @returns the formatted phrase; throws where `Intl` or Temporal throws, for the caller's sentinel
* @example formatRelativeDuration(Temporal.Duration.from({ minutes: -30 }), "en-GB", {}, () => 0) // "30 minutes ago"
* @example formatRelativeDuration(Temporal.Duration.from({ hours: 36 }), "de-DE", { largestUnit: "hour" }, () => 0) // "in 36 Stunden"
* @example formatRelativeDuration(Temporal.Duration.from({ days: 400 }), "en-GB", {}, () => 1) // "next year" (month/year totals come from calendarTotal)
*/
function formatRelativeDuration(diff, locale, options, calendarTotal) {
const forcedUnit = resolveRelativeUnit(options.largestUnit, RELATIVE_UNITS);
const roundingMethod = options.roundingMethod;
const absSeconds = Math.abs(diff.total("second"));
const unit = forcedUnit === void 0 ? AUTO_UNITS.find((t) => absSeconds < t.maxSeconds)?.unit ?? "year" : forcedUnit;
let total;
try {
total = diff.total(unit);
} catch {
total = calendarTotal(unit);
}
return formatRelativeAmount(total, unit, locale, options, roundingMethod);
}
/**
* Round a distance to a whole number of `unit` and render it with `Intl.RelativeTimeFormat`. The
* shared last step of every `formatRelative*` function.
*
* - `numeric` and `style` are read here, once each (GetOption); `roundingMethod` is passed in
* because the caller has already read it.
*
* @param total the signed fractional distance in `unit`
* @param unit the singular unit the distance is written in
* @param locale BCP 47 locale(s) passed through to `Intl.RelativeTimeFormat`
* @param options the caller's options, read for `numeric` (default `"auto"`) and `style`
* (default `"long"`)
* @param roundingMethod the caller's `roundingMethod`, `undefined` for the default `"round"`
* @returns the formatted phrase; throws where `Intl` throws or the rounding method is invalid, for
* the caller's sentinel
* @example formatRelativeAmount(-1.5, "hour", "en-GB", {}, "floor") // "2 hours ago"
* @example formatRelativeAmount(1, "day", "ja-JP", { numeric: "always" }, undefined) // "1 日後"
* @example formatRelativeAmount(1, "day", "fr-FR", { style: "wide" as never }, undefined) // throws RangeError
*/
function formatRelativeAmount(total, unit, locale, options, roundingMethod) {
const amount = resolveRelativeRounding(total, roundingMethod);
return normalizeDateTime(cachedFormatter("RelativeTimeFormat", locale, {
numeric: stringOptionOrDefault(options.numeric, "auto"),
style: stringOptionOrDefault(options.style, "long")
}).format(amount, unit));
}
export { formatRelativeAmount, formatRelativeDuration };