@date-vir/duration
Version:
Durations units an utils for date-vir.
71 lines (70 loc) • 2.37 kB
TypeScript
import type { RequireAtLeastOne, UnionToIntersection } from 'type-fest';
import { DurationUnit } from './units/duration-unit.js';
/** Copied from `@augment-vir/common` so this package doesn't depend on augment-vir. */
type RequiredAndNotNull<T> = {
[P in keyof T]-?: NonNullable<T[P]>;
};
/**
* A looser type with all possible options, based on the stricter {@link Duration} type. Matches the
* DurationObjectUnits type from the luxon package.
*
* @category Duration
*/
export type AnyDuration = Partial<Record<DurationUnit, number | undefined>>;
/**
* Requires at least one duration unit to be set.
*
* @category Util
*/
export type AtLeastOneDuration = RequireAtLeastOne<RequiredAndNotNull<AnyDuration>>;
/**
* All possible duration units at once. Whether these duration units should be added together to
* form the whole picture or whether each duration is the same value but calculated in different
* units depends on the context of this type's usage.
*
* @category Util
*/
export type AllDurations = RequiredAndNotNull<AnyDuration>;
/**
* Not a single date instance, but a description of a date duration. Used to calculate diffs between
* dates, add offsets to an existing date, or describe a single time duration. Usually only one
* property is set on this at any given time.
*
* Settings a type parameter of `true` allows any {@link DurationUnit}.
*
* @category Duration
*/
export type Duration<DurationKeys extends DurationUnit | true> = UnionToIntersection<DurationKeys extends true ? AnyDuration : DurationKeys extends DurationUnit ? Pick<AllDurations, DurationKeys> : never>;
/**
* An object with all {@link DurationUnit} keys set to `0`.
*
* @category Constants
*/
export declare const zeroDuration: {
readonly years: 0;
readonly quarters: 0;
readonly months: 0;
readonly weeks: 0;
readonly days: 0;
readonly hours: 0;
readonly minutes: 0;
readonly seconds: 0;
readonly milliseconds: 0;
};
/**
* An object with all {@link DurationUnit} keys set to `0`. Alias for {@link zeroDuration}.
*
* @category Constants
*/
export declare const emptyDuration: {
readonly years: 0;
readonly quarters: 0;
readonly months: 0;
readonly weeks: 0;
readonly days: 0;
readonly hours: 0;
readonly minutes: 0;
readonly seconds: 0;
readonly milliseconds: 0;
};
export {};