UNPKG

@toreda/time

Version:

Simple, small footprint library for common time operations and converting between units of time.

93 lines (92 loc) 3.51 kB
import type { Strong } from '@toreda/strong-types'; import { Log } from '@toreda/log'; import type { Time } from '../time'; import type { TimeUnit } from './unit'; /** * Internal state data created and wrapped by Time instances. */ export declare class TimeData { readonly units: Strong<TimeUnit>; private readonly value; readonly log: Log; constructor(units: TimeUnit, value: number, log?: Log | null); private makeLog; /** * Get the current time value in instance's native time unit. * @returns */ get(): number; set(caller: Time, input?: number | Time | null): Time; /** * Add number input to current value. * @param caller Time instance calling this method. * @param input number value to be added. * @returns Returns the Time instance which invoked the function * to support method chaining. */ addNumber(caller: Time, input?: number | null): Time; /** * Subtract number input from the current value. * @param caller Time instance calling this method. * @param input number value to be subtracted. * @returns Returns the Time instance which invoked the function * to support method chaining. */ subNumber(caller: Time, input?: number | null): Time; /** * Get numeric unit value from a Time or number input. Time inputs are converted from * their native TimeUnit to the TimeUnit specified by the `convertTo` arg. * @param convertTo * @param input * @returns */ getUnitValue(convertTo: TimeUnit, input?: Time | number | null): number | null; /** * Convert value from specified units into object's native time units, then * subtract it from the current value. * @param caller Time instance calling this method. * @param input number value to be added. * @returns Returns the Time instance which invoked the function * to support method chaining. */ subUnit(caller: Time, units: TimeUnit, value?: number | null, decimals?: number): Time; /** * Convert value from provided unit type into object's native time units and * add it to the current value. * @param caller Time instance calling this function to be returned by function. * @param units Time Unit of provided value. * @param value Value to be converted and added to current time. * @param decimals Number of decimals to include in final added value. * @returns */ addUnit(caller: Time, units: TimeUnit, value?: number | null, decimals?: number): Time; /** * Invert current value's sign. * @param caller * @param posOnly * @returns */ invert(caller: Time, posOnly?: boolean): Time; timeSinceTime(target: Time): Time | null; timeSinceNumber(target: number): Time | null; /** * Get time object containing time left until target time. May return * negative vamlue when target time is in the past. The returned time * object's time left value uses the same time units as the calling instance. * @param time * @returns */ timeUntilTime(time?: Time | null): Time | null; /** * Get time remaining until target unix timestamp. * @param target * @returns */ timeUntilNumber(target?: number | null): Time | null; /** * Reset internal state variables to their initial values. * @param caller * @returns */ reset(caller: Time): Time; }