source: node_modules/recharts/lib/util/DataUtils.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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