UNPKG

@date-vir/duration

Version:

Durations units an utils for date-vir.

57 lines (56 loc) 1.78 kB
import { type AllDurations } from './duration.js'; import { DurationUnit } from './units/duration-unit.js'; /** * Select a set of duration units to use in {@link DurationBySelection}. * * @category Internal * @example * * ```ts * import {type DurationUnitSelection} from 'date-vir'; * * const select: DurationUnitSelection = { * days: true, * months: true, * }; * ``` */ export type DurationUnitSelection = Partial<Record<DurationUnit, boolean | undefined>>; /** * Reduce a {@link DurationUnitSelection} object into an array of the selected units, in order from * smallest unit (at index 0) to largest. * * @category Internal */ export declare function flattenUnitSelection(units: Readonly<DurationUnitSelection>): DurationUnit[]; /** * Pick a subset of {@link Duration} keys by a {@link DurationUnitSelection} input. * * @category Internal * @example * * ```ts * import {type DurationBySelection} from 'date-vir'; * * type MySelection = DurationBySelection<{days: true; months: true}>; // `{days: number, months: number}` * ``` */ export type DurationBySelection<SelectedUnits extends Readonly<DurationUnitSelection> | undefined> = undefined extends SelectedUnits ? AllDurations : { [Unit in keyof AllDurations as Unit extends keyof SelectedUnits ? SelectedUnits[Unit] extends true ? Unit : never : never]: AllDurations[Unit]; }; /** * An {@link DurationUnitSelection} instance that sets all duration units to `true`. * * @category Util */ export declare const selectAllDurationUnits: { readonly years: true; readonly quarters: true; readonly months: true; readonly weeks: true; readonly days: true; readonly hours: true; readonly minutes: true; readonly seconds: true; readonly milliseconds: true; };