@hypothesis/frontend-shared
Version:
Shared components, styles and utilities for Hypothesis projects
72 lines (71 loc) • 2.54 kB
TypeScript
/**
* Clears the cache of formatters.
*/
export declare function clearFormatters(): void;
type IntlType = typeof window.Intl;
/**
* @return formatted date
*/
export type DateFormatter = (date: Date, now: Date, intl?: IntlType) => string;
export type Breakpoint = {
test: (date: Date, now: Date) => boolean;
formatter: DateFormatter;
nextUpdate: number | null;
};
/**
* Return the number of milliseconds until the next update for a given date
* should be handled, based on the delta between `date` and `now`.
*
* @return ms until next update or `null` if no update should occur
*/
export declare function nextFuzzyUpdate(date: Date | null, now: Date): number | null;
/**
* Start an interval whose frequency depends on the age of a timestamp.
*
* This is useful for refreshing UI components displaying timestamps generated
* by `formatRelativeDate`, since the output changes less often for older timestamps.
*
* @param date - Date string to use to determine the interval frequency
* @param callback - Interval callback
* @return A function that cancels the interval
*/
export declare function decayingInterval(date: string, callback: () => void): () => void;
/**
* Formats a date as a short approximate string relative to the current date.
*
* The level of precision is proportional to how recent the date is.
*
* For example:
*
* - "Just now"
* - "5 minutes ago"
* - "25 Oct 2018"
*
* @param date - The date to consider as the timestamp to format.
* @param now - The date to consider as the current time.
* @param Intl - Test seam. JS `Intl` API implementation.
* @return A 'fuzzy' string describing the relative age of the date.
*/
export declare function formatRelativeDate(date: Date | null, now: Date, Intl?: IntlType): string;
export type FormatDateTimeOptions = {
/**
* Whether the formatted date should include the week day or not.
* Defaults to `false`.
*/
includeWeekday?: boolean;
/**
* Whether the formatted date should include the time (hours and minutes) or not.
* Defaults to `true`.
*/
includeTime?: boolean;
};
/**
* Formats a date as an absolute string in a human-readable format.
*
* The exact format will vary depending on the locale, but the verbosity will
* be consistent across locales. In en-US for example this will look like:
*
* "Dec 17, 2017, 10:00 AM"
*/
export declare function formatDateTime(date: Date | string, { includeWeekday, includeTime }?: FormatDateTimeOptions, Intl?: IntlType): string;
export {};