unitspan
Version:
Create differential spans for units of measurement, including time measurements, digital (binary) measurements, and more.
162 lines (161 loc) • 7.17 kB
TypeScript
/** @typedef {Record<string, number|[convertToBaseUnit: (units: number) => number, convertFromBaseUnit: (baseUnits: number) => { decimal: number, remainder: number, fraction: number }]>} Converter */
/**
* @template {Record<string, number|[convertToBaseUnit: (units: number) => number, convertFromBaseUnit: (baseUnits: number) => { decimal: number, remainder: number, fraction: number }]>} T
*/
export class UnitSpan<T extends Record<string, number | [convertToBaseUnit: (units: number) => number, convertFromBaseUnit: (baseUnits: number) => {
decimal: number;
remainder: number;
fraction: number;
}]>> {
/**
* @protected
* @param {number} quantity
* @param {T} converter
* @param {{[K in keyof T]: { padStart: number }}} formatter
* @param {number} precision
*/
protected constructor();
/**
* Stored value of units, this should always be in one type of unit.
*
* For example, in the `TempSpan` class, this value will always be measured in units of `Kelvin`.
* @protected
* @type {number}
*/
protected __baseUnitValue: number;
/**
* @param {(obj: {[K in keyof T]: (quantity: number) => void}) => void} callback
* @returns {this}
*/
add(callback: (obj: { [K in keyof T]: (quantity: number) => void; }) => void): this;
/**
* @param {(obj: {[K in keyof T]: (quantity: number) => void}) => void} callback
* @returns {this}
*/
sub(callback: (obj: { [K in keyof T]: (quantity: number) => void; }) => void): this;
/**
* @overload Overload 1
* @param {(model: PropertyRetriever<T>) => keyof T} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {number}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {(model: PropertyRetriever<T>) => string} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {keyof T} key
* Some string property of the conversion object to use when converting.
* @returns {number}
*
* @overload
* @param {string} key
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* Get the conversion of units as specified from the property reference (and return value) from the `model` parameter used in `callback`.
* @param {((model: PropertyRetriever<T>) => keyof T|string)|keyof T|string} callback
* @returns {string|number}
*/
to(callback: (model: PropertyRetriever<T>) => keyof T): number;
/**
* @overload Overload 1
* @param {(model: PropertyRetriever<T>) => keyof T} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {number}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {(model: PropertyRetriever<T>) => string} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {keyof T} key
* Some string property of the conversion object to use when converting.
* @returns {number}
*
* @overload
* @param {string} key
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* Get the conversion of units as specified from the property reference (and return value) from the `model` parameter used in `callback`.
* @param {((model: PropertyRetriever<T>) => keyof T|string)|keyof T|string} callback
* @returns {string|number}
*/
to(callback: (model: PropertyRetriever<T>) => string): string;
/**
* @overload Overload 1
* @param {(model: PropertyRetriever<T>) => keyof T} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {number}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {(model: PropertyRetriever<T>) => string} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {keyof T} key
* Some string property of the conversion object to use when converting.
* @returns {number}
*
* @overload
* @param {string} key
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* Get the conversion of units as specified from the property reference (and return value) from the `model` parameter used in `callback`.
* @param {((model: PropertyRetriever<T>) => keyof T|string)|keyof T|string} callback
* @returns {string|number}
*/
to(key: keyof T): number;
/**
* @overload Overload 1
* @param {(model: PropertyRetriever<T>) => keyof T} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {number}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {(model: PropertyRetriever<T>) => string} callback
* Callback used to get the type of conversion (or otherwise, the key of conversion) to use when converting.
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* @overload
* @param {keyof T} key
* Some string property of the conversion object to use when converting.
* @returns {number}
*
* @overload
* @param {string} key
* @returns {string}
* The converted number from the unit quantity held by this UnitSpan class object.
*
* Get the conversion of units as specified from the property reference (and return value) from the `model` parameter used in `callback`.
* @param {((model: PropertyRetriever<T>) => keyof T|string)|keyof T|string} callback
* @returns {string|number}
*/
to(key: string): string;
/**
* Set the precision of the resulting conversions.
* @param {number} numDigits
* @returns {this}
*/
precision(numDigits: number): this;
#private;
}
export type Converter = Record<string, number | [convertToBaseUnit: (units: number) => number, convertFromBaseUnit: (baseUnits: number) => {
decimal: number;
remainder: number;
fraction: number;
}]>;
export type PropertyRetriever<T extends Record<string, any>> = { [K in keyof T]: K; };