nullish-math
Version:
`nullish-math` is a lightweight TypeScript package that provides basic math operations with support for `null` and `undefined` values and an immutable, chainable API. It is useful when working with numeric data that may contain nullish values, as it ensur
88 lines (87 loc) • 3.94 kB
TypeScript
export type NullishNumber = NullishMath | number | null | undefined;
export declare class NullishMath {
#private;
constructor(value: NullishNumber);
/**
* Calculates the average of the provided numbers. By default, `null`s are excluded from the average. This can be changed by setting the `treatNullishAsZero` option. With this flag, nullish numbers get counted as a `0` and thus impact the average.
*/
static average: (numbers: NullishNumber[], options?: {
treatNullishAsZero?: boolean;
}) => NullishMath;
/**
* Calculates the maximum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
static max: (numbers: NullishNumber[]) => NullishMath;
/**
* Calculates the minimum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
static min: (numbers: NullishNumber[]) => NullishMath;
/**
* Converts the input to either `number | null`. General-purpose equivalent of `nm.end()`
*/
static unwrap(value: NullishNumber): number | null;
/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given number.
*/
add(number: NullishNumber): NullishMath;
/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given numbers.
*/
addMany(...numbers: NullishNumber[]): NullishMath;
/**
* Returns true if the two numbers are equal, including the case where both are null.
*/
eq(toCompare: NullishNumber): boolean;
/**
* Returns true if toCompare is strictly greater than the current number. Returns null if either number is null
*/
gt(toCompare: NullishNumber): boolean | null;
/**
* Returns true if toCompare is greater than or equal to the current number. Returns null if either number is null
*/
gte(toCompare: NullishNumber): boolean | null;
/**
* Returns true if toCompare is strictly less than the current number. Returns null if either number is null
*/
lt(toCompare: NullishNumber): boolean | null;
/**
* Returns true if toCompare is less than or equal to the current number. Returns null if either number is null
*/
lte(toCompare: NullishNumber): boolean | null;
/**
* Returns true if the two numbers are not equal, also returns false when both numbers are null
*/
neq(toCompare: NullishNumber): boolean;
/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given number.
*/
subtract(number: NullishNumber): NullishMath;
/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given numbers.
*/
subtractMany(...numbers: NullishNumber[]): NullishMath;
/**
* Returns a new instance of `NullishMath` with the product of the current value and the given number.
*/
multiply(number: NullishNumber): NullishMath;
/**
* Returns a new instance of `NullishMath` with the product of the current value and the given numbers.
*/
multiplyMany(...numbers: NullishNumber[]): NullishMath;
/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given number.
*/
divide(number: NullishNumber): NullishMath;
/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given numbers.
*/
divideMany(...numbers: NullishNumber[]): NullishMath;
/**
* Returns the final value of the `NullishMath` instance. If any of the values passed to the math operation methods are `null` or `undefined`, the final value will be `null`.
*/
end(): number | null;
}
/**
* Creates a new `NullishMath` object with an initial value.
*/
export declare function nm(value: NullishNumber): NullishMath;