unitspan
Version:
Create differential spans for units of measurement, including time measurements, digital (binary) measurements, and more.
220 lines (219 loc) • 7.96 kB
TypeScript
/**
* @extends {UnitSpan<typeof TimeSpanUnitsConverter>}
*/
export class TimeSpan extends UnitSpan<Readonly<{
/** @deprecated */
Nanoseconds: 1;
/** @deprecated */
Microseconds: number;
/** @deprecated */
Milliseconds: number;
/** @deprecated */
Seconds: number;
/** @deprecated */
Minutes: number;
/** @deprecated */
Hours: number;
/** @deprecated */
Days: number;
/** @deprecated */
Weeks: number;
/** @deprecated */
Months: number;
/** @deprecated */
Years: number;
nanoseconds: 1;
microseconds: number;
milliseconds: number;
seconds: number;
minutes: number;
hours: number;
days: number;
weeks: number;
months: number;
years: number;
}>> {
/**
* Create a differential time span between two dates, `date1` and `date2`.
* @param {Date} date1
* Start date
* @param {Date} date2
* End date
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static between(date1: Date, date2: Date): TimeSpan;
/**
* Create a differential time span between `January 1st, 1970 00:00:00 UTC` and now.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static sinceEpoch(): TimeSpan;
/**
* Create a differential time span between a given date, `date`, and now.
* @param {Date} date
* Date to differentiate between now.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static since(date: Date): TimeSpan;
/**
* Create a differential time span between now and a date in the future.
* @param {Date} date
* Date to differentiate between now. (This can be in the past, it will just have negative units)
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static until(date: Date): TimeSpan;
/**
* Create a TimeSpan class with the initial number of nanoseconds.
* @param {number} numNanoseconds
* Number of nanoseconds to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromNanoseconds(numNanoseconds: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of microseconds.
* @param {number} numMicroseconds
* Number of microseconds to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromMicroseconds(numMicroseconds: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of milliseconds.
* @param {number} numMilliseconds
* Number of milliseconds to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromMilliseconds(numMilliseconds: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of seconds.
* @param {number} numSeconds
* Number of seconds to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromSeconds(numSeconds: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of minutes.
* @param {number} numMinutes
* Number of minutes to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromMinutes(numMinutes: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of hours.
* @param {number} numHours
* Number of hours to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromHours(numHours: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of days.
* @param {number} numDays
* Number of days to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromDays(numDays: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of weeks.
* @param {number} numWeeks
* Number of weeks to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromWeeks(numWeeks: number): TimeSpan;
/**
* Create a TimeSpan class with the initial number of months.
* @param {number} numMonths
* Number of months to initialize with.
* @returns {TimeSpan}
* New {@link TimeSpan} class object that can convert between other units of measurement per {@link TimeSpanUnitsConverter}
*/
static fromMonths(numMonths: number): TimeSpan;
/**
* @protected
* @param {number} quantity
*/
protected constructor();
/**
* Create a timeout where `callback` is ran only after the time that this TimeSpan object holds has passed.
* @usage
* ```js
* TimeSpan.fromSeconds(10).timeout(() => {
* console.log(`This will only print after 10 seconds`);
* });
* ```
* @param {() => void|Promise<void>} callback
* Function
* @returns {TimeoutController}
* Object with `clear` and `refresh` functions to control the timeout.
*
*/
timeout(callback: () => void | Promise<void>): TimeoutController;
/**
* Create an interval where `callback` is ran for every time after the time that this TimeSpan object holds has passed.
* @usage
* ```js
* let i = 0;
* const unsubscribe = TimeSpan.fromSeconds(10).interval(() => {
* console.log(`Printing ${i}/3`);
* if(++i === 3) {
* unsubscribe();
* }
* });
*
* // will print each line every 10 seconds:
* // Printing 1/3
* // Printing 2/3
* // Printing 3/3
* ```
* @param {() => void|Promise<void>} callback
* @returns {IntervalController} Function to unsubscribe from the interval.
*/
interval(callback: () => void | Promise<void>): IntervalController;
/**
* Create a Promise that will only resolve after the time that this TimeSpan object holds has passed.
* @returns {Promise<void>}
*/
delay(): Promise<void>;
toYears(): number;
toMonths(): number;
toWeeks(): number;
toDays(): number;
toHours(): number;
toMinutes(): number;
toSeconds(): number;
toMilliseconds(): number;
toMicroseconds(): number;
toNanoseconds(): number;
}
export type TimeoutController = {
cancel: () => void;
refresh: () => TimeoutController;
};
export type IntervalController = {
/**
* Stops the interval.
*/
cancel: () => void;
/**
* Starts the interval again. If the interval is already ongoing, then the interval is restarted.
*/
start: () => IntervalController;
/**
* Returns true if the interval is ongoing.
*/
isStarted: boolean;
/**
* Returns true if the interval is stopped.
*/
isStopped: boolean;
};
import { UnitSpan } from "./unitspan.js";