Files
2025-11-28 19:45:44 -07:00

157 lines
4.8 KiB
JavaScript

import get from 'es-toolkit/compat/get';
export var mathSign = value => {
if (value === 0) {
return 0;
}
if (value > 0) {
return 1;
}
return -1;
};
export var isNan = value => {
// eslint-disable-next-line eqeqeq
return typeof value == 'number' && value != +value;
};
export var isPercent = value => typeof value === 'string' && value.indexOf('%') === value.length - 1;
export var isNumber = value => (typeof value === 'number' || value instanceof Number) && !isNan(value);
export var isNumOrStr = value => isNumber(value) || typeof value === 'string';
var idCounter = 0;
export var uniqueId = prefix => {
var id = ++idCounter;
return "".concat(prefix || '').concat(id);
};
/**
* Calculates the numeric value represented by a percent string or number, based on a total value.
*
* - If `percent` is not a number or string, returns `defaultValue`.
* - If `percent` is a percent string but `totalValue` is null/undefined, returns `defaultValue`.
* - If the result is NaN, returns `defaultValue`.
* - If `validate` is true and the result exceeds `totalValue`, returns `totalValue`.
*
* @param percent - The percent value to convert. Can be a number (e.g. 25) or a string ending with '%' (e.g. '25%').
* If a string, it must end with '%' to be treated as a percent; otherwise, it is parsed as a number.
* @param totalValue - The total value to calculate the percent of. Required if `percent` is a percent string.
* @param defaultValue - The value returned if `percent` is undefined, invalid, or cannot be converted to a number.
* @param validate - If true, ensures the result does not exceed `totalValue` (when provided).
* @returns The calculated value, or `defaultValue` for invalid input.
*/
export var getPercentValue = function getPercentValue(percent, totalValue) {
var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var validate = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
if (!isNumber(percent) && typeof percent !== 'string') {
return defaultValue;
}
var value;
if (isPercent(percent)) {
if (totalValue == null) {
return defaultValue;
}
var index = percent.indexOf('%');
value = totalValue * parseFloat(percent.slice(0, index)) / 100;
} else {
value = +percent;
}
if (isNan(value)) {
value = defaultValue;
}
if (validate && totalValue != null && value > totalValue) {
value = totalValue;
}
return value;
};
export var hasDuplicate = ary => {
if (!Array.isArray(ary)) {
return false;
}
var len = ary.length;
var cache = {};
for (var i = 0; i < len; i++) {
if (!cache[ary[i]]) {
cache[ary[i]] = true;
} else {
return true;
}
}
return false;
};
export function interpolate(start, end, t) {
if (isNumber(start) && isNumber(end)) {
return start + t * (end - start);
}
return end;
}
export function findEntryInArray(ary, specifiedKey, specifiedValue) {
if (!ary || !ary.length) {
return undefined;
}
return ary.find(entry => entry && (typeof specifiedKey === 'function' ? specifiedKey(entry) : get(entry, specifiedKey)) === specifiedValue);
}
/**
* The least square linear regression
* @param {Array} data The array of points
* @returns {Object} The domain of x, and the parameter of linear function
*/
export var getLinearRegression = data => {
var len = data.length;
var xsum = 0;
var ysum = 0;
var xysum = 0;
var xxsum = 0;
var xmin = Infinity;
var xmax = -Infinity;
var xcurrent = 0;
var ycurrent = 0;
for (var i = 0; i < len; i++) {
xcurrent = data[i].cx || 0;
ycurrent = data[i].cy || 0;
xsum += xcurrent;
ysum += ycurrent;
xysum += xcurrent * ycurrent;
xxsum += xcurrent * xcurrent;
xmin = Math.min(xmin, xcurrent);
xmax = Math.max(xmax, xcurrent);
}
var a = len * xxsum !== xsum * xsum ? (len * xysum - xsum * ysum) / (len * xxsum - xsum * xsum) : 0;
return {
xmin,
xmax,
a,
b: (ysum - a * xsum) / len
};
};
/**
* Checks if the value is null or undefined
* @param value The value to check
* @returns true if the value is null or undefined
*/
export var isNullish = value => {
return value === null || typeof value === 'undefined';
};
/**
* Uppercase the first letter of a string
* @param {string} value The string to uppercase
* @returns {string} The uppercased string
*/
export var upperFirst = value => {
if (isNullish(value)) {
return value;
}
return "".concat(value.charAt(0).toUpperCase()).concat(value.slice(1));
};
/**
* Checks if the value is not null nor undefined.
* @param value The value to check
* @returns true if the value is not null nor undefined
*/
export function isNotNil(value) {
return value != null;
}
/**
* No-operation function that does nothing.
* Useful as a placeholder or default callback function.
*/
export function noop() {}