@pawel-up/jexl
Version:
Javascript Expression Language: Powerful context-based expression parser and evaluator
124 lines • 3.64 kB
JavaScript
export const ABS = Math.abs;
export const CEIL = Math.ceil;
export const FLOOR = Math.floor;
export const ROUND = Math.round;
export const RANDOM = Math.random;
export const MAX = Math.max;
export const MIN = Math.min;
export const SIN = Math.sin;
export const COS = Math.cos;
export const TAN = Math.tan;
export const ASIN = Math.asin;
export const ACOS = Math.acos;
export const ATAN = Math.atan;
export const ATAN2 = Math.atan2;
export const SQRT = Math.sqrt;
export const CBRT = Math.cbrt;
export const EXP = Math.exp;
export const LOG = Math.log;
export const LOG10 = Math.log10;
export const LOG2 = Math.log2;
export const POW = Math.pow;
export const SIGN = Math.sign;
export const TRUNC = Math.trunc;
export const SUM = (...args) => {
if (Array.isArray(args[0])) {
return args[0].reduce((a, b) => a + b, 0);
}
return args.reduce((a, b) => a + b, 0);
};
export const AVG = (...args) => {
if (Array.isArray(args[0])) {
const arr = args[0];
if (arr.length === 0)
return 0;
return arr.reduce((a, b) => a + b, 0) / arr.length;
}
if (args.length === 0)
return 0;
return args.reduce((a, b) => a + b, 0) / args.length;
};
export const MEDIAN = (...args) => {
const arr = Array.isArray(args[0]) ? args[0] : args;
if (arr.length === 0)
return 0;
const sorted = [...arr].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
};
export const MODE = (...args) => {
const arr = Array.isArray(args[0]) ? args[0] : args;
if (arr.length === 0)
return 0;
const frequency = {};
let maxFreq = 0;
let modeValue = arr[0];
for (const num of arr) {
frequency[num] = (frequency[num] || 0) + 1;
if (frequency[num] > maxFreq) {
maxFreq = frequency[num];
modeValue = num;
}
}
return modeValue;
};
export const VARIANCE = (...args) => {
const arr = Array.isArray(args[0]) ? args[0] : args;
if (arr.length === 0)
return 0;
const mean = arr.reduce((a, b) => a + b, 0) / arr.length;
return arr.reduce((sum, num) => sum + Math.pow(num - mean, 2), 0) / arr.length;
};
export const STDDEV = (...args) => {
const arr = Array.isArray(args[0]) ? args[0] : args;
return Math.sqrt(VARIANCE(...arr));
};
export const CLAMP = (value, min, max) => {
return Math.min(Math.max(value, min), max);
};
export const LERP = (start, end, t) => {
return start + (end - start) * t;
};
export const TO_RADIANS = (degrees) => {
return degrees * (Math.PI / 180);
};
export const TO_DEGREES = (radians) => {
return radians * (180 / Math.PI);
};
export const GCD = (a, b) => {
a = Math.abs(Math.floor(a));
b = Math.abs(Math.floor(b));
return b === 0 ? a : GCD(b, a % b);
};
export const LCM = (a, b) => {
if (a === 0 || b === 0)
return 0;
return Math.abs(a * b) / GCD(a, b);
};
export const FACTORIAL = (n) => {
n = Math.floor(n);
if (n < 0)
return NaN;
if (n === 0 || n === 1)
return 1;
return n * FACTORIAL(n - 1);
};
export const IS_PRIME = (n) => {
n = Math.floor(n);
if (n < 2)
return false;
if (n === 2)
return true;
if (n % 2 === 0)
return false;
for (let i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i === 0)
return false;
}
return true;
};
export const ROUND_TO = (value, decimals) => {
const factor = Math.pow(10, decimals);
return Math.round(value * factor) / factor;
};
//# sourceMappingURL=math.js.map