ixfx
Version:
A framework for programming interactivity
78 lines (76 loc) • 2.48 kB
TypeScript
/**
* Interval types allows for more expressive coding, rather than embedding millisecond values.
*
* That is, we can use `{ mins: 5 }` to mean 5 minutes rather than `5*60*1000`
* or worse, 300000, for the same value.
*
* @example
* ```js
* { hours: 1 }; // 1 hour
* { mins: 5 }; // 5 mins
* { secs: 5 }; // 5 secs
* { millis: 5 }; // 5 milliseconds
* ```
*
* If several fields are used, this sums their value
* ```js
* { secs: 2, millis: 1 }; // equal 2001 milliseconds.
* ```
*
* Wherever ixfx takes an `Interval`, you can also just provide a number instead.
* This will be taken as a millisecond value.
*
* @see {@link intervalToMs} to convert to milliseconds.
* @see {@link isInterval} check whether input is an Interval type
* @see {@link elapsedToHumanString} render interval in human-friendly form
*/
type Interval = number | {
readonly millis?: number;
readonly secs?: number;
readonly hours?: number;
readonly mins?: number;
};
/**
* Return the millisecond value of an Interval.
*
* ```js
* intervalToMs(100); // 100
* intervalToMs({ millis: 100 }); // 100
* ```
*
* Use `defaultNumber` to return a default in the case of
* _undefined_ or invalid input.
*
* ```js
* intervalToMs(undefined); // throws error
* intervalToMs(undefined, 100); // 100
* ```
*
* If no default is provided, an exception is thrown.
* @param interval Interval
* @param defaultNumber Default value if `interval` is _undefined_ or invalid
* @returns Milliseconds
*/
declare function intervalToMs(interval: Interval | undefined, defaultNumber?: number): number;
/**
* Returns _true_ if `interval` matches the {@link Interval} type.
* @param interval
* @returns _True_ if `interval` is an {@link Interval}.
*/
declare function isInterval(interval: number | Interval | undefined): interval is Interval;
/**
* Returns a human-readable representation
* of some elapsed milliseconds
*
* @example
* ```js
* elapsedToHumanString(10); // `10ms`
* elapsedToHumanString(2000); // `2s`
* elapsedToHumanString(65*1000); // `1mins`
* ```
* @param millisOrFunction Milliseconds as a number, {@link Interval} or function that resolve to a number
* @param rounding Rounding (default: 2)
* @returns
*/
declare const elapsedToHumanString: (millisOrFunction: number | (() => number) | Interval, rounding?: number) => string;
export { type Interval as I, isInterval as a, elapsedToHumanString as e, intervalToMs as i };