daet
Version:
Minimal immutable date class that supports relative time, calendar time, and plus/minus of different units.
84 lines • 4.04 kB
TypeScript
import { StrictUnion } from 'simplytyped';
export type SetUnits = 'millisecond' | 'second' | 'minute' | 'hour';
export type ArithmeticUnits = 'millisecond' | 'second' | 'minute' | 'hour' | 'week' | 'day';
export type Input = string | number | Date | Daet;
export declare const Millisecond = 1;
export declare const Second: number;
export declare const Minute: number;
export declare const Hour: number;
export declare const Day: number;
export declare const Week: number;
/** The abstract base tier to use for the human relative date display. */
export interface BaseTier {
/** How long in milliseconds until this particular tier becomes irrelevant? If not specified then it will be detected automatically. */
refresh?: number;
/** Generate the human relative date display for this particular tier. */
message: (opts: {
past: boolean;
delta: number;
when: Daet;
}) => string;
}
/** This tier is relevant until the delta milliseconds is reached. E.g. 1000 milliseconds from now. */
export interface LimitTier extends BaseTier {
/** Generate a millisecond delta for when this tier is no longer relevant. */
limit: number;
}
/** This tier is relevant until the absolute milliseconds are reached. E.g. Tomorrow at midnight. */
export interface WhenTier extends BaseTier {
/** Generate an epoch time for when this tier is no longer relevant. */
when: (opts: {
past: boolean;
}) => number;
}
/** A tier to use for the human relative date display. */
export type Tier = StrictUnion<LimitTier | WhenTier>;
/** A minimal immutable date class that supports relative time, calendar time, and plus/minus of different units. */
export default class Daet {
/** The raw date object behind this daet instance. */
readonly raw: Date;
/** The tiers used for human relative date display. */
static get tiers(): Tier[];
/** Create a new daet instance based on the input */
static create(input?: Input): Daet;
/** Create a new daet instance based on the input */
constructor(input?: Input);
/** Return a new daet instance that is in the past by the value amount. */
minus(value: number, unit: ArithmeticUnits): Daet;
/** Return a new daet instance that is in the future by the value amount. */
plus(value: number, unit: ArithmeticUnits): Daet;
/** Clone this daet instance based on its raw value. */
private rawClone;
/** Clone this daet instance. */
clone(): Daet;
/** Return a new daet instance that has the unit set to the desired value. */
set(value: number, unit: SetUnits): Daet;
/** Return a new daet instance that has the desired unit reset to 0. */
reset(unit: SetUnits): Daet;
/** Get the epoch time of this daet instance. */
getTime: () => number;
/** Get the milliseconds from the passed daet instance to this daet instance. */
getMillisecondsFrom(from: Daet): number;
/** Get the milliseconds from now to this daet instance */
getMillisecondsFromNow(): number;
/** Use https://devdocs.io/javascript/global_objects/datetimeformat to format our daet instance. */
format(locale: string, options: object): string;
/** Return a new daet instance that is the start of the week proceeding that of this daet instance. */
startOfNextWeek: () => Daet;
/** Return a new daet instance that is the end of the week preceeding that of this daet instance. */
endOfLastWeek: () => Daet;
/** Get the human absolute date display for this daet instance. */
calendar(): string;
/** Return the human relative display from now, as well as the millisecond delta before a refresh is needed. */
fromNowDetails(): {
message: string;
refresh: number;
};
/** Return the human relative display from now. */
fromNow(): string;
/** Return the ISO string for this daet instance. */
toISOString: () => string;
/** Return the JSON string for this daet instance. */
toJSON: () => string;
}
//# sourceMappingURL=index.d.ts.map