UNPKG

unitsnet-js

Version:

A better way to hold unit variables and easily convert to the destination unit

134 lines (133 loc) 5.78 kB
export interface ToStringOptions { /** * The maximum number of fractional digits to include in the output string. * * @example * // With fractionalDigits: 2, "1.23456" becomes "1.23" * // With fractionalDigits: 4, "1.23456" becomes "1.2346" */ fractionalDigits?: number; /** * When true, displays digits up to the first non-zero decimal place, even if it * exceeds the specified `fractionalDigits` limit. * * This option only has an effect when `fractionalDigits` is also provided. * * @example * // For number 1.000012: * // With fractionalDigits: 2 and extendDigitsToFirstFraction: false → "1.00" * // With fractionalDigits: 2 and extendDigitsToFirstFraction: true → "1.00001" */ extendDigitsToFirstFraction?: boolean; } export declare enum ArithmeticOperation { /** An plus arithmetic operation (JS default "+") */ Add = "Add", /** An subtract arithmetic operation (JS default "-") */ Subtract = "Subtract", /** An multiply arithmetic operation (JS default "/") */ Multiply = "Multiply", /** An divide arithmetic operation (JS default "*") */ Divide = "Divide", /** An modulo arithmetic operation (JS default "%") */ Modulo = "Modulo", /** An power arithmetic operation (JS default "**") */ Pow = "Pow", /** A Square root operation (JS Default "Math.sqrt") */ Sqrt = "Sqrt" } export declare enum CompareOperation { Equals = "Equals", CompareTo = "CompareTo" } export interface OperatorOverrides { [ArithmeticOperation.Add]?: (a: number, b: number) => number; [ArithmeticOperation.Subtract]?: (a: number, b: number) => number; [ArithmeticOperation.Multiply]?: (a: number, b: number) => number; [ArithmeticOperation.Divide]?: (a: number, b: number) => number; [ArithmeticOperation.Modulo]?: (a: number, b: number) => number; [ArithmeticOperation.Pow]?: (a: number, b: number) => number; [ArithmeticOperation.Sqrt]?: (a: number) => number; [CompareOperation.Equals]?: (valueA: number, valueB: number) => boolean; [CompareOperation.CompareTo]?: (valueA: number, valueB: number) => number; } /** * Set arithmetic formula to be used while calling this operation on two units (e.g. Length + Length) * Instead of the JS default operation (+, -, * etc.) * @param arithmeticOperation The formula's operation * @param replacementFunction The formula to used. */ /** * Sets an arithmetic operator to use the given function. * @description Certain use-cases require the use of high precision mathematics beyond what's defined in the ECMA specification. * This function allows overriding of operators to to facilitate usage of specialized mathematic libraries. * @example * ``` * import NP from 'number-precision' * * setOperatorOverride(ArithmeticOperation.Add, (a, b) => NP.plus(a, b)) * ``` * * @export * @template TOperator * @param {TOperator} arithmeticOperation * @param {(OperatorOverrides[TOperator] | undefined)} replacementFunction */ export declare function setOperatorOverride<TOperator extends ArithmeticOperation | CompareOperation>(arithmeticOperation: TOperator, replacementFunction: OperatorOverrides[TOperator] | undefined): void; /** * Removes the given operator override (i.e., returns operator behavior to default JavaScript implementation) * * @export * @template TOperator The operator to unset * @param {TOperator} arithmeticOperation The operator to unset */ export declare function unsetOperatorOverride<TOperator extends ArithmeticOperation | CompareOperation>(arithmeticOperation: TOperator): void; /** * Removes all operator overrides (i.e., return all operator behaviors to default JavaScript implementation) * * @export */ export declare function unsetAllOperatorOverrides(): void; /** * Gets a boolean indicating whether any operators are currently overridden * * @export * @return {boolean} */ export declare function areAnyOperatorsOverridden(): boolean; export declare abstract class BaseUnit { protected abstract value: number; abstract get BaseValue(): number; protected abstract get baseUnit(): string; /** * Truncates a number to a specified number of fractional digits. * @param num - The number to truncate. * @param options - The options to use when truncating the number. * @returns The truncated number. */ protected truncateFractionDigits(num: number, options?: ToStringOptions): number; abstract convert(toUnit: string): number; abstract toString(unit?: string, fractionalDigits?: number): string; abstract getUnitAbbreviation(unitAbbreviation?: string): string; abstract equals(other: BaseUnit): boolean; abstract compareTo(other: BaseUnit): number; abstract add(other: BaseUnit): BaseUnit; abstract subtract(other: BaseUnit): BaseUnit; abstract multiply(other: BaseUnit): BaseUnit; abstract divide(other: BaseUnit): BaseUnit; abstract modulo(other: BaseUnit): BaseUnit; abstract pow(other: BaseUnit): BaseUnit; abstract toDto(holdInUnit?: string): { value: number; unit: string; }; protected internalEquals(valueA: number, valueB: number): boolean; protected internalCompareTo(valueA: number, valueB: number): number; protected internalAdd(valueA: number, valueB: number): number; protected internalSubtract(valueA: number, valueB: number): number; protected internalMultiply(valueA: number, valueB: number): number; protected internalDivide(valueA: number, valueB: number): number; protected internalModulo(valueA: number, valueB: number): number; protected internalPow(valueA: number, valueB: number): number; protected internalSqrt(value: number): number; }