UNPKG

date-vir

Version:

Easy and explicit dates and times.

95 lines (94 loc) 2.42 kB
import { getEnumValues, pickObjectKeys, typedObjectFromEntries } from '@augment-vir/common'; import { TimeKey } from '../full-date/full-date-parts.js'; import { overrideDateParts } from './override-date.js'; /** * A {@link FullDate} instance that has the lowest, valid, non-negative, value for each property. * * @category Constants */ export const zeroDate = { year: 0, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, }; /** * A {@link FullDate} instance that has the lowest, valid, non-negative, value for each property. * Alias for {@link zeroDate}. * * @category Constants */ export const emptyDate = zeroDate; /** * An object that contains the time parts of {@link FullDate} all set to `0`. * * @category Constants */ export const zeroTime = pickObjectKeys(zeroDate, getEnumValues(TimeKey)); /** * Clear the time parts of a {@link FullDate}, setting them all to `0`. * * @category Clear * @example * * ```ts * import {clearTime, type FullDate} from 'date-vir'; * * const exampleDate: Readonly<FullDate> = { * year: 2024, * month: 1, * day: 5, * hour: 1, * minute: 1, * second: 1, * millisecond: 1, * timezone: 'UTC', * }; * * clearTime(exampleDate); * // `{year: 2024, month: 1, day: 5, hour: 0, minute: 0, second: 0, millisecond: 0, timezone: 'UTC'}` * ``` */ export function clearTime(inputFullDate) { return clearParts(inputFullDate, getEnumValues(TimeKey)); } /** * Clear all the selected parts of {@link FullDate} by setting them each to their lowest, valid * value. See {@link zeroDate} for the lowest valid values for each property. * * @category Clear * @example * * ```ts * import {clearTime, type FullDate} from 'date-vir'; * * const exampleDate: Readonly<FullDate> = { * year: 2024, * month: 1, * day: 5, * hour: 1, * minute: 1, * second: 1, * millisecond: 1, * timezone: 'UTC', * }; * * clearParts(exampleDate, [ * 'year', * 'day', * ]); * // `{year: 0, month: 1, day: 1, hour: 1, minute: 1, second: 1, millisecond: 1, timezone: 'UTC'}` * ``` */ export function clearParts(inputFullDate, parts) { const clearParts = typedObjectFromEntries(parts.map((keyName) => { return [ keyName, zeroDate[keyName], ]; })); return overrideDateParts(inputFullDate, clearParts); }