UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

25 lines (24 loc) 1.59 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { requireDate } from "../../util/date.js"; import { formatAgo, formatUntil, formatWhen, getSecondsAgo } from "../../util/duration.js"; import { formatDateTime } from "../../util/format.js"; const _OPTIONS = { unitDisplay: "narrow" }; function _getWhen(formatter, { target: possibleTarget, current: possibleCurrent = "now", full = false, children }, caller) { const target = requireDate(possibleTarget, caller); const current = requireDate(possibleCurrent, caller); const title = possibleCurrent === "now" ? formatDateTime(target) : `${formatDateTime(target)} to ${formatDateTime(current)}`; const formatted = children ?? (full ? `${title} (${formatter(target, current, _OPTIONS)})` : formatter(target, current, _OPTIONS)); return (_jsx("time", { dateTime: possibleCurrent === "now" ? target.toISOString() : `PT${getSecondsAgo(target, current)}S`, title: title, children: formatted })); } /** Show a string like `in 6d` or `3w ago` wrapped with a `<time>` element providing the machine-readable format of the same date. */ export function When(props) { return _getWhen(formatWhen, props, When); } /** Show a string like `6d` or `3w` wrapped with a `<time>` element providing the machine-readable format of the same date. */ export function Ago(props) { return _getWhen(formatAgo, props, Ago); } /** Show a string like `6d` or `3w` wrapped with a `<time>` element providing the machine-readable format of the same date. */ export function Until(props) { return _getWhen(formatUntil, props, Until); }