| [a762898] | 1 | import get from 'es-toolkit/compat/get';
|
|---|
| 2 | import { round } from './round';
|
|---|
| 3 | export var mathSign = value => {
|
|---|
| 4 | if (value === 0) {
|
|---|
| 5 | return 0;
|
|---|
| 6 | }
|
|---|
| 7 | if (value > 0) {
|
|---|
| 8 | return 1;
|
|---|
| 9 | }
|
|---|
| 10 | return -1;
|
|---|
| 11 | };
|
|---|
| 12 | export var isNan = value => {
|
|---|
| 13 | // eslint-disable-next-line eqeqeq
|
|---|
| 14 | return typeof value == 'number' && value != +value;
|
|---|
| 15 | };
|
|---|
| 16 | export var isPercent = value => typeof value === 'string' && value.indexOf('%') === value.length - 1;
|
|---|
| 17 | export var isNumber = value => (typeof value === 'number' || value instanceof Number) && !isNan(value);
|
|---|
| 18 | export var isNumOrStr = value => isNumber(value) || typeof value === 'string';
|
|---|
| 19 | var idCounter = 0;
|
|---|
| 20 | export var uniqueId = prefix => {
|
|---|
| 21 | var id = ++idCounter;
|
|---|
| 22 | return "".concat(prefix || '').concat(id);
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Calculates the numeric value represented by a percent string or number, based on a total value.
|
|---|
| 27 | *
|
|---|
| 28 | * - If `percent` is not a number or string, returns `defaultValue`.
|
|---|
| 29 | * - If `percent` is a percent string but `totalValue` is null/undefined, returns `defaultValue`.
|
|---|
| 30 | * - If the result is NaN, returns `defaultValue`.
|
|---|
| 31 | * - If `validate` is true and the result exceeds `totalValue`, returns `totalValue`.
|
|---|
| 32 | *
|
|---|
| 33 | * @param percent - The percent value to convert. Can be a number (e.g. 25) or a string ending with '%' (e.g. '25%').
|
|---|
| 34 | * If a string, it must end with '%' to be treated as a percent; otherwise, it is parsed as a number.
|
|---|
| 35 | * @param totalValue - The total value to calculate the percent of. Required if `percent` is a percent string.
|
|---|
| 36 | * @param defaultValue - The value returned if `percent` is undefined, invalid, or cannot be converted to a number.
|
|---|
| 37 | * @param validate - If true, ensures the result does not exceed `totalValue` (when provided).
|
|---|
| 38 | * @returns The calculated value, or `defaultValue` for invalid input.
|
|---|
| 39 | */
|
|---|
| 40 | export var getPercentValue = function getPercentValue(percent, totalValue) {
|
|---|
| 41 | var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|---|
| 42 | var validate = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|---|
| 43 | if (!isNumber(percent) && typeof percent !== 'string') {
|
|---|
| 44 | return defaultValue;
|
|---|
| 45 | }
|
|---|
| 46 | var value;
|
|---|
| 47 | if (isPercent(percent)) {
|
|---|
| 48 | if (totalValue == null) {
|
|---|
| 49 | return defaultValue;
|
|---|
| 50 | }
|
|---|
| 51 | var index = percent.indexOf('%');
|
|---|
| 52 | value = totalValue * parseFloat(percent.slice(0, index)) / 100;
|
|---|
| 53 | } else {
|
|---|
| 54 | value = +percent;
|
|---|
| 55 | }
|
|---|
| 56 | if (isNan(value)) {
|
|---|
| 57 | value = defaultValue;
|
|---|
| 58 | }
|
|---|
| 59 | if (validate && totalValue != null && value > totalValue) {
|
|---|
| 60 | value = totalValue;
|
|---|
| 61 | }
|
|---|
| 62 | return value;
|
|---|
| 63 | };
|
|---|
| 64 | export var hasDuplicate = ary => {
|
|---|
| 65 | if (!Array.isArray(ary)) {
|
|---|
| 66 | return false;
|
|---|
| 67 | }
|
|---|
| 68 | var len = ary.length;
|
|---|
| 69 | var cache = {};
|
|---|
| 70 | for (var i = 0; i < len; i++) {
|
|---|
| 71 | if (!cache[String(ary[i])]) {
|
|---|
| 72 | cache[String(ary[i])] = true;
|
|---|
| 73 | } else {
|
|---|
| 74 | return true;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | return false;
|
|---|
| 78 | };
|
|---|
| 79 | export function interpolate(start, end, t) {
|
|---|
| 80 | if (isNumber(start) && isNumber(end)) {
|
|---|
| 81 | return round(start + t * (end - start));
|
|---|
| 82 | }
|
|---|
| 83 | return end;
|
|---|
| 84 | }
|
|---|
| 85 | export function findEntryInArray(ary, specifiedKey, specifiedValue) {
|
|---|
| 86 | if (!ary || !ary.length) {
|
|---|
| 87 | return undefined;
|
|---|
| 88 | }
|
|---|
| 89 | return ary.find(entry => entry && (typeof specifiedKey === 'function' ? specifiedKey(entry) : get(entry, specifiedKey)) === specifiedValue);
|
|---|
| 90 | }
|
|---|
| 91 | /**
|
|---|
| 92 | * The least square linear regression
|
|---|
| 93 | * @param {Array} data The array of points
|
|---|
| 94 | * @returns {Object} The domain of x, and the parameter of linear function
|
|---|
| 95 | */
|
|---|
| 96 | export var getLinearRegression = data => {
|
|---|
| 97 | var len = data.length;
|
|---|
| 98 | var xsum = 0;
|
|---|
| 99 | var ysum = 0;
|
|---|
| 100 | var xysum = 0;
|
|---|
| 101 | var xxsum = 0;
|
|---|
| 102 | var xmin = Infinity;
|
|---|
| 103 | var xmax = -Infinity;
|
|---|
| 104 | var xcurrent = 0;
|
|---|
| 105 | var ycurrent = 0;
|
|---|
| 106 | for (var i = 0; i < len; i++) {
|
|---|
| 107 | var _data$i, _data$i2;
|
|---|
| 108 | xcurrent = ((_data$i = data[i]) === null || _data$i === void 0 ? void 0 : _data$i.cx) || 0;
|
|---|
| 109 | ycurrent = ((_data$i2 = data[i]) === null || _data$i2 === void 0 ? void 0 : _data$i2.cy) || 0;
|
|---|
| 110 | xsum += xcurrent;
|
|---|
| 111 | ysum += ycurrent;
|
|---|
| 112 | xysum += xcurrent * ycurrent;
|
|---|
| 113 | xxsum += xcurrent * xcurrent;
|
|---|
| 114 | xmin = Math.min(xmin, xcurrent);
|
|---|
| 115 | xmax = Math.max(xmax, xcurrent);
|
|---|
| 116 | }
|
|---|
| 117 | var a = len * xxsum !== xsum * xsum ? (len * xysum - xsum * ysum) / (len * xxsum - xsum * xsum) : 0;
|
|---|
| 118 | return {
|
|---|
| 119 | xmin,
|
|---|
| 120 | xmax,
|
|---|
| 121 | a,
|
|---|
| 122 | b: (ysum - a * xsum) / len
|
|---|
| 123 | };
|
|---|
| 124 | };
|
|---|
| 125 | /**
|
|---|
| 126 | * Checks if the value is null or undefined
|
|---|
| 127 | * @param value The value to check
|
|---|
| 128 | * @returns true if the value is null or undefined
|
|---|
| 129 | */
|
|---|
| 130 | export var isNullish = value => {
|
|---|
| 131 | return value === null || typeof value === 'undefined';
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Uppercase the first letter of a string
|
|---|
| 136 | * @param {string} value The string to uppercase
|
|---|
| 137 | * @returns {string} The uppercased string
|
|---|
| 138 | */
|
|---|
| 139 | export var upperFirst = value => {
|
|---|
| 140 | if (isNullish(value)) {
|
|---|
| 141 | return value;
|
|---|
| 142 | }
|
|---|
| 143 | return "".concat(value.charAt(0).toUpperCase()).concat(value.slice(1));
|
|---|
| 144 | };
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Checks if the value is not null nor undefined.
|
|---|
| 148 | * @param value The value to check
|
|---|
| 149 | * @returns true if the value is not null nor undefined
|
|---|
| 150 | */
|
|---|
| 151 | export function isNotNil(value) {
|
|---|
| 152 | return value != null;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * No-operation function that does nothing.
|
|---|
| 157 | * Useful as a placeholder or default callback function.
|
|---|
| 158 | */
|
|---|
| 159 | export function noop() {} |
|---|