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
254 lines (253 loc) • 10.1 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _NullishMath_value;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NullishMath = void 0;
exports.nm = nm;
class NullishMath {
constructor(value) {
_NullishMath_value.set(this, void 0);
__classPrivateFieldSet(this, _NullishMath_value, NullishMath.unwrap(value), "f");
}
/**
* Converts the input to either `number | null`. General-purpose equivalent of `nm.end()`
*/
static unwrap(value) {
if (value === null)
return null;
if (value === undefined)
return null;
if (typeof value === 'number')
return value;
return value.end();
}
/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given number.
*/
add(number) {
const n = NullishMath.unwrap(number);
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null || n === null)
return new NullishMath(null);
return new NullishMath(__classPrivateFieldGet(this, _NullishMath_value, "f") + n);
}
/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given numbers.
*/
addMany(...numbers) {
let result = __classPrivateFieldGet(this, _NullishMath_value, "f");
if (result === null)
return new NullishMath(null);
for (const _n of numbers) {
const n = NullishMath.unwrap(_n);
if (n === null)
return new NullishMath(null);
result = result + n;
}
return new NullishMath(result);
}
/**
* Returns true if the two numbers are equal, including the case where both are null.
*/
eq(toCompare) {
const n = NullishMath.unwrap(toCompare);
return __classPrivateFieldGet(this, _NullishMath_value, "f") === n;
}
/**
* Returns true if toCompare is strictly greater than the current number. Returns null if either number is null
*/
gt(toCompare) {
const n = NullishMath.unwrap(toCompare);
if (n === null)
return null;
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null)
return null;
return __classPrivateFieldGet(this, _NullishMath_value, "f") > n;
}
/**
* Returns true if toCompare is greater than or equal to the current number. Returns null if either number is null
*/
gte(toCompare) {
const n = NullishMath.unwrap(toCompare);
if (n === null)
return null;
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null)
return null;
return __classPrivateFieldGet(this, _NullishMath_value, "f") >= n;
}
/**
* Returns true if toCompare is strictly less than the current number. Returns null if either number is null
*/
lt(toCompare) {
const n = NullishMath.unwrap(toCompare);
if (n === null)
return null;
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null)
return null;
return __classPrivateFieldGet(this, _NullishMath_value, "f") < n;
}
/**
* Returns true if toCompare is less than or equal to the current number. Returns null if either number is null
*/
lte(toCompare) {
const n = NullishMath.unwrap(toCompare);
if (n === null)
return null;
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null)
return null;
return __classPrivateFieldGet(this, _NullishMath_value, "f") <= n;
}
/**
* Returns true if the two numbers are not equal, also returns false when both numbers are null
*/
neq(toCompare) {
const n = NullishMath.unwrap(toCompare);
return __classPrivateFieldGet(this, _NullishMath_value, "f") !== n;
}
/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given number.
*/
subtract(number) {
const n = NullishMath.unwrap(number);
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null || n === null)
return new NullishMath(null);
return new NullishMath(__classPrivateFieldGet(this, _NullishMath_value, "f") - n);
}
/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given numbers.
*/
subtractMany(...numbers) {
let result = __classPrivateFieldGet(this, _NullishMath_value, "f");
if (result === null)
return new NullishMath(null);
for (const _n of numbers) {
const n = NullishMath.unwrap(_n);
if (n === null)
return new NullishMath(null);
result = result - n;
}
return new NullishMath(result);
}
/**
* Returns a new instance of `NullishMath` with the product of the current value and the given number.
*/
multiply(number) {
const n = NullishMath.unwrap(number);
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null || n === null)
return new NullishMath(null);
return new NullishMath(__classPrivateFieldGet(this, _NullishMath_value, "f") * n);
}
/**
* Returns a new instance of `NullishMath` with the product of the current value and the given numbers.
*/
multiplyMany(...numbers) {
let result = __classPrivateFieldGet(this, _NullishMath_value, "f");
if (result === null)
return new NullishMath(null);
for (const _n of numbers) {
const n = NullishMath.unwrap(_n);
if (n === null)
return new NullishMath(null);
result = result * n;
}
return new NullishMath(result);
}
/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given number.
*/
divide(number) {
const n = NullishMath.unwrap(number);
if (__classPrivateFieldGet(this, _NullishMath_value, "f") === null || n === null)
return new NullishMath(null);
return new NullishMath(__classPrivateFieldGet(this, _NullishMath_value, "f") / n);
}
/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given numbers.
*/
divideMany(...numbers) {
let result = __classPrivateFieldGet(this, _NullishMath_value, "f");
if (result === null)
return new NullishMath(null);
for (const _n of numbers) {
const n = NullishMath.unwrap(_n);
if (n === null)
return new NullishMath(null);
result = result / n;
}
return new NullishMath(result);
}
/**
* 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() {
return __classPrivateFieldGet(this, _NullishMath_value, "f");
}
}
exports.NullishMath = NullishMath;
_NullishMath_value = new WeakMap();
/**
* 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.
*/
NullishMath.average = (numbers, options = {
treatNullishAsZero: false,
}) => {
let countValid = 0;
let sumValid = 0;
for (const rawNumber of numbers) {
const number = NullishMath.unwrap(rawNumber);
if (number === null) {
if (options.treatNullishAsZero)
countValid += 1;
continue;
}
countValid += 1;
sumValid += number;
}
// division by zero
if (countValid === 0)
return nm(options.treatNullishAsZero ? 0 : null);
return nm(sumValid).divide(countValid);
};
/**
* Calculates the maximum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
NullishMath.max = (numbers) => {
let max = null;
for (const rawNumber of numbers) {
const number = NullishMath.unwrap(rawNumber);
if (number === null)
continue;
if (max === null || number > max)
max = number;
}
return nm(max);
};
/**
* Calculates the minimum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
NullishMath.min = (numbers) => {
let min = null;
for (const rawNumber of numbers) {
const number = NullishMath.unwrap(rawNumber);
if (number === null)
continue;
if (min === null || number < min)
min = number;
}
return nm(min);
};
/**
* Creates a new `NullishMath` object with an initial value.
*/
function nm(value) {
return new NullishMath(value);
}