units-converter-ts
Version:
A simple utility library for converting units
64 lines (63 loc) • 2.81 kB
TypeScript
import { IMeasure, IUnitDescription } from './types';
export declare class Convert<TSystems extends string, TUnitNames extends string> {
/**
* Throws error with custom error message.
*
* @param {string} unitName - The name of the unsupported unit that caused the error. Value is empty string by default.
* @param {string} msg - The custom message. Value is empty string by default.
*/
private static throwUnsupportedUnitError;
/**
* Returns a measure object and its name for a given unit.
*
* @param {string} unitName - The name of the unit for which to retrieve the measure object.
*/
private static getMeasure;
/**
* Returns an object containing the system and unit details corresponding to the provided unit name.
*
* @param {string} unitName - The unit name for which to retrieve information.
* @param {string} measureName - Name of the measure to retrieve unit from.
*/
private static getUnit;
/**
* Returns a description of a unit.
*
* @param {string} unitName - The name of the unit to describe.
* @param {string} measureName - The name of the measure to which the unit belongs.
*/
private static describeUnit;
static allPossibilities(): string[];
static measures(): string[];
static possibilities(...measures: string[]): string[];
static describe(unitName: string): IUnitDescription;
static list(...measures: string[]): IUnitDescription[];
static convert(from: string, to: string, value: number): number;
private measure;
private measureName;
constructor(measure: IMeasure<TSystems, TUnitNames>, measureName: string);
/**
* Returns an array containing names of all available units for a specified measure.
*/
possibilities(): string[];
/**
* Returns a description for the specified unit. Deprecated: Use the convert.describe method instead.
*
* @param {string} unitName - The name of the unit to describe (e.g., 'cm' for centimeter).
* @deprecated This method is deprecated and will be removed in future versions. Please use the new convert.describe method
*/
describe(unitName: string): IUnitDescription;
/**
* Returns an array of descriptions for all units for a specified measure.
*/
list(): IUnitDescription[];
/**
* Converts a numeric value from one unit to another within the same measure system.
*
* @param {string} from - The unit name to convert from (e.g., 'cm' for centimeter).
* @param {string} to - The unit name to convert to (e.g., 'in' for inch).
* @param {number} value - The numeric value to convert.
* @returns {number} The converted numeric value.
*/
convert(from: string, to: string, value: number): number;
}