-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
helpers.core.js
351 lines (305 loc) · 9.14 KB
/
helpers.core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* @namespace Chart.helpers
*/
/**
* An empty function that can be used, for example, for optional callback.
*/
export function noop() {}
/**
* Returns a unique id, sequentially generated from a global variable.
* @returns {number}
* @function
*/
export const uid = (function() {
let id = 0;
return function() {
return id++;
};
}());
/**
* Returns true if `value` is neither null nor undefined, else returns false.
* @param {*} value - The value to test.
* @returns {boolean}
* @since 2.7.0
*/
export function isNullOrUndef(value) {
return value === null || typeof value === 'undefined';
}
/**
* Returns true if `value` is an array (including typed arrays), else returns false.
* @param {*} value - The value to test.
* @returns {boolean}
* @function
*/
export function isArray(value) {
if (Array.isArray && Array.isArray(value)) {
return true;
}
const type = Object.prototype.toString.call(value);
if (type.slice(0, 7) === '[object' && type.slice(-6) === 'Array]') {
return true;
}
return false;
}
/**
* Returns true if `value` is an object (excluding null), else returns false.
* @param {*} value - The value to test.
* @returns {boolean}
* @since 2.7.0
*/
export function isObject(value) {
return value !== null && Object.prototype.toString.call(value) === '[object Object]';
}
/**
* Returns true if `value` is a finite number, else returns false
* @param {*} value - The value to test.
* @returns {boolean}
*/
const isNumberFinite = (value) => (typeof value === 'number' || value instanceof Number) && isFinite(+value);
export {
isNumberFinite as isFinite,
};
/**
* Returns `value` if finite, else returns `defaultValue`.
* @param {*} value - The value to return if defined.
* @param {*} defaultValue - The value to return if `value` is not finite.
* @returns {*}
*/
export function finiteOrDefault(value, defaultValue) {
return isNumberFinite(value) ? value : defaultValue;
}
/**
* Returns `value` if defined, else returns `defaultValue`.
* @param {*} value - The value to return if defined.
* @param {*} defaultValue - The value to return if `value` is undefined.
* @returns {*}
*/
export function valueOrDefault(value, defaultValue) {
return typeof value === 'undefined' ? defaultValue : value;
}
export const toPercentage = (value, dimension) =>
typeof value === 'string' && value.endsWith('%') ?
parseFloat(value) / 100
: value / dimension;
export const toDimension = (value, dimension) =>
typeof value === 'string' && value.endsWith('%') ?
parseFloat(value) / 100 * dimension
: +value;
/**
* Calls `fn` with the given `args` in the scope defined by `thisArg` and returns the
* value returned by `fn`. If `fn` is not a function, this method returns undefined.
* @param {function} fn - The function to call.
* @param {Array|undefined|null} args - The arguments with which `fn` should be called.
* @param {object} [thisArg] - The value of `this` provided for the call to `fn`.
* @returns {*}
*/
export function callback(fn, args, thisArg) {
if (fn && typeof fn.call === 'function') {
return fn.apply(thisArg, args);
}
}
/**
* Note(SB) for performance sake, this method should only be used when loopable type
* is unknown or in none intensive code (not called often and small loopable). Else
* it's preferable to use a regular for() loop and save extra function calls.
* @param {object|Array} loopable - The object or array to be iterated.
* @param {function} fn - The function to call for each item.
* @param {object} [thisArg] - The value of `this` provided for the call to `fn`.
* @param {boolean} [reverse] - If true, iterates backward on the loopable.
*/
export function each(loopable, fn, thisArg, reverse) {
let i, len, keys;
if (isArray(loopable)) {
len = loopable.length;
if (reverse) {
for (i = len - 1; i >= 0; i--) {
fn.call(thisArg, loopable[i], i);
}
} else {
for (i = 0; i < len; i++) {
fn.call(thisArg, loopable[i], i);
}
}
} else if (isObject(loopable)) {
keys = Object.keys(loopable);
len = keys.length;
for (i = 0; i < len; i++) {
fn.call(thisArg, loopable[keys[i]], keys[i]);
}
}
}
/**
* Returns true if the `a0` and `a1` arrays have the same content, else returns false.
* @param {Array} a0 - The array to compare
* @param {Array} a1 - The array to compare
* @returns {boolean}
* @private
*/
export function _elementsEqual(a0, a1) {
let i, ilen, v0, v1;
if (!a0 || !a1 || a0.length !== a1.length) {
return false;
}
for (i = 0, ilen = a0.length; i < ilen; ++i) {
v0 = a0[i];
v1 = a1[i];
if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) {
return false;
}
}
return true;
}
/**
* Returns a deep copy of `source` without keeping references on objects and arrays.
* @param {*} source - The value to clone.
* @returns {*}
*/
export function clone(source) {
if (isArray(source)) {
return source.map(clone);
}
if (isObject(source)) {
const target = Object.create(null);
const keys = Object.keys(source);
const klen = keys.length;
let k = 0;
for (; k < klen; ++k) {
target[keys[k]] = clone(source[keys[k]]);
}
return target;
}
return source;
}
function isValidKey(key) {
return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1;
}
/**
* The default merger when Chart.helpers.merge is called without merger option.
* Note(SB): also used by mergeConfig and mergeScaleConfig as fallback.
* @private
*/
export function _merger(key, target, source, options) {
if (!isValidKey(key)) {
return;
}
const tval = target[key];
const sval = source[key];
if (isObject(tval) && isObject(sval)) {
// eslint-disable-next-line no-use-before-define
merge(tval, sval, options);
} else {
target[key] = clone(sval);
}
}
/**
* Recursively deep copies `source` properties into `target` with the given `options`.
* IMPORTANT: `target` is not cloned and will be updated with `source` properties.
* @param {object} target - The target object in which all sources are merged into.
* @param {object|object[]} source - Object(s) to merge into `target`.
* @param {object} [options] - Merging options:
* @param {function} [options.merger] - The merge method (key, target, source, options)
* @returns {object} The `target` object.
*/
export function merge(target, source, options) {
const sources = isArray(source) ? source : [source];
const ilen = sources.length;
if (!isObject(target)) {
return target;
}
options = options || {};
const merger = options.merger || _merger;
for (let i = 0; i < ilen; ++i) {
source = sources[i];
if (!isObject(source)) {
continue;
}
const keys = Object.keys(source);
for (let k = 0, klen = keys.length; k < klen; ++k) {
merger(keys[k], target, source, options);
}
}
return target;
}
/**
* Recursively deep copies `source` properties into `target` *only* if not defined in target.
* IMPORTANT: `target` is not cloned and will be updated with `source` properties.
* @param {object} target - The target object in which all sources are merged into.
* @param {object|object[]} source - Object(s) to merge into `target`.
* @returns {object} The `target` object.
*/
export function mergeIf(target, source) {
// eslint-disable-next-line no-use-before-define
return merge(target, source, {merger: _mergerIf});
}
/**
* Merges source[key] in target[key] only if target[key] is undefined.
* @private
*/
export function _mergerIf(key, target, source) {
if (!isValidKey(key)) {
return;
}
const tval = target[key];
const sval = source[key];
if (isObject(tval) && isObject(sval)) {
mergeIf(tval, sval);
} else if (!Object.prototype.hasOwnProperty.call(target, key)) {
target[key] = clone(sval);
}
}
/**
* @private
*/
export function _deprecated(scope, value, previous, current) {
if (value !== undefined) {
console.warn(scope + ': "' + previous +
'" is deprecated. Please use "' + current + '" instead');
}
}
const emptyString = '';
const dot = '.';
function indexOfDotOrLength(key, start) {
const idx = key.indexOf(dot, start);
return idx === -1 ? key.length : idx;
}
export function resolveObjectKey(obj, key) {
if (key === emptyString) {
return obj;
}
let pos = 0;
let idx = indexOfDotOrLength(key, pos);
while (obj && idx > pos) {
obj = obj[key.slice(pos, idx)];
pos = idx + 1;
idx = indexOfDotOrLength(key, pos);
}
return obj;
}
/**
* @private
*/
export function _capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export const defined = (value) => typeof value !== 'undefined';
export const isFunction = (value) => typeof value === 'function';
// Adapted from https://stackoverflow.com/questions/31128855/comparing-ecma6-sets-for-equality#31129384
export const setsEqual = (a, b) => {
if (a.size !== b.size) {
return false;
}
for (const item of a) {
if (!b.has(item)) {
return false;
}
}
return true;
};
/**
* @param {import('../../types/index.esm').ChartEvent} e - The event
* @returns {boolean}
* @private
*/
export function _isClickEvent(e) {
return e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu';
}