@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.
50 lines • 3.47 kB
TypeScript
import { Temporal } from "./temporal.js";
import { RelativeRoundingMethod } from "../types/relative-rounding-method.js";
import { RelativeTimeFormatOptions } from "../types/relative-time-options.js";
import { RelativeUnit } from "../types/relative-unit.js";
import "../types/index.js";
/** A relative unit, singular or plural (`Intl.RelativeTimeFormat` and Temporal accept both). */
type RelativeUnitName = RelativeUnit | `${RelativeUnit}s`;
/** The display fields every seven-unit `formatRelative*` function reads. */
export interface RelativeDurationFormatOptions {
style?: RelativeTimeFormatOptions["style"];
numeric?: RelativeTimeFormatOptions["numeric"];
largestUnit?: RelativeUnitName;
roundingMethod?: RelativeRoundingMethod;
}
/**
* 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)
*/
export declare function formatRelativeDuration(diff: Temporal.Duration, locale: string | string[] | undefined, options: RelativeDurationFormatOptions, calendarTotal: (unit: RelativeUnit) => number): string;
/**
* 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
*/
export declare function formatRelativeAmount(total: number, unit: RelativeUnit, locale: string | string[] | undefined, options: Pick<RelativeDurationFormatOptions, "numeric" | "style">, roundingMethod: RelativeRoundingMethod | undefined): string;
export {};