-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
utils.js
635 lines (478 loc) · 13.8 KB
/
utils.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import { domEnv, nav } from './dom';
// binary search for index of closest value
export function closestIdx(num, arr, lo, hi) {
let mid;
lo = lo || 0;
hi = hi || arr.length - 1;
let bitwise = hi <= 2147483647;
while (hi - lo > 1) {
mid = bitwise ? (lo + hi) >> 1 : floor((lo + hi) / 2);
if (arr[mid] < num)
lo = mid;
else
hi = mid;
}
if (num - arr[lo] <= arr[hi] - num)
return lo;
return hi;
}
export function nonNullIdx(data, _i0, _i1, dir) {
for (let i = dir == 1 ? _i0 : _i1; i >= _i0 && i <= _i1; i += dir) {
if (data[i] != null)
return i;
}
return -1;
}
export function getMinMax(data, _i0, _i1, sorted) {
// console.log("getMinMax()");
let _min = inf;
let _max = -inf;
if (sorted == 1) {
_min = data[_i0];
_max = data[_i1];
}
else if (sorted == -1) {
_min = data[_i1];
_max = data[_i0];
}
else {
for (let i = _i0; i <= _i1; i++) {
let v = data[i];
if (v != null) {
if (v < _min)
_min = v;
if (v > _max)
_max = v;
}
}
}
return [_min, _max];
}
export function getMinMaxLog(data, _i0, _i1) {
// console.log("getMinMax()");
let _min = inf;
let _max = -inf;
for (let i = _i0; i <= _i1; i++) {
let v = data[i];
if (v != null && v > 0) {
if (v < _min)
_min = v;
if (v > _max)
_max = v;
}
}
return [_min, _max];
}
export function rangeLog(min, max, base, fullMags) {
let minSign = sign(min);
let maxSign = sign(max);
if (min == max) {
if (minSign == -1) {
min *= base;
max /= base;
}
else {
min /= base;
max *= base;
}
}
let logFn = base == 10 ? log10 : log2;
let growMinAbs = minSign == 1 ? floor : ceil;
let growMaxAbs = maxSign == 1 ? ceil : floor;
let minExp = growMinAbs(logFn(abs(min)));
let maxExp = growMaxAbs(logFn(abs(max)));
let minIncr = pow(base, minExp);
let maxIncr = pow(base, maxExp);
// fix values like Math.pow(10, -5) === 0.000009999999999999999
if (base == 10) {
if (minExp < 0)
minIncr = roundDec(minIncr, -minExp);
if (maxExp < 0)
maxIncr = roundDec(maxIncr, -maxExp);
}
if (fullMags || base == 2) {
min = minIncr * minSign;
max = maxIncr * maxSign;
}
else {
min = incrRoundDn(min, minIncr);
max = incrRoundUp(max, maxIncr);
}
return [min, max];
}
export function rangeAsinh(min, max, base, fullMags) {
let minMax = rangeLog(min, max, base, fullMags);
if (min == 0)
minMax[0] = 0;
if (max == 0)
minMax[1] = 0;
return minMax;
}
export const rangePad = 0.1;
export const autoRangePart = {
mode: 3,
pad: rangePad,
};
const _eqRangePart = {
pad: 0,
soft: null,
mode: 0,
};
const _eqRange = {
min: _eqRangePart,
max: _eqRangePart,
};
// this ensures that non-temporal/numeric y-axes get multiple-snapped padding added above/below
// TODO: also account for incrs when snapping to ensure top of axis gets a tick & value
export function rangeNum(_min, _max, mult, extra) {
if (isObj(mult))
return _rangeNum(_min, _max, mult);
_eqRangePart.pad = mult;
_eqRangePart.soft = extra ? 0 : null;
_eqRangePart.mode = extra ? 3 : 0;
return _rangeNum(_min, _max, _eqRange);
}
// nullish coalesce
export function ifNull(lh, rh) {
return lh == null ? rh : lh;
}
// checks if given index range in an array contains a non-null value
// aka a range-bounded Array.some()
export function hasData(data, idx0, idx1) {
idx0 = ifNull(idx0, 0);
idx1 = ifNull(idx1, data.length - 1);
while (idx0 <= idx1) {
if (data[idx0] != null)
return true;
idx0++;
}
return false;
}
function _rangeNum(_min, _max, cfg) {
let cmin = cfg.min;
let cmax = cfg.max;
let padMin = ifNull(cmin.pad, 0);
let padMax = ifNull(cmax.pad, 0);
let hardMin = ifNull(cmin.hard, -inf);
let hardMax = ifNull(cmax.hard, inf);
let softMin = ifNull(cmin.soft, inf);
let softMax = ifNull(cmax.soft, -inf);
let softMinMode = ifNull(cmin.mode, 0);
let softMaxMode = ifNull(cmax.mode, 0);
let delta = _max - _min;
let deltaMag = log10(delta);
let scalarMax = max(abs(_min), abs(_max));
let scalarMag = log10(scalarMax);
let scalarMagDelta = abs(scalarMag - deltaMag);
// this handles situations like 89.7, 89.69999999999999
// by assuming 0.001x deltas are precision errors
// if (delta > 0 && delta < abs(_max) / 1e3)
// delta = 0;
// treat data as flat if delta is less than 1 billionth
// or range is 11+ orders of magnitude below raw values, e.g. 99999999.99999996 - 100000000.00000004
if (delta < 1e-9 || scalarMagDelta > 10) {
delta = 0;
// if soft mode is 2 and all vals are flat at 0, avoid the 0.1 * 1e3 fallback
// this prevents 0,0,0 from ranging to -100,100 when softMin/softMax are -1,1
if (_min == 0 || _max == 0) {
delta = 1e-9;
if (softMinMode == 2 && softMin != inf)
padMin = 0;
if (softMaxMode == 2 && softMax != -inf)
padMax = 0;
}
}
let nonZeroDelta = delta || scalarMax || 1e3;
let mag = log10(nonZeroDelta);
let base = pow(10, floor(mag));
let _padMin = nonZeroDelta * (delta == 0 ? (_min == 0 ? .1 : 1) : padMin);
let _newMin = roundDec(incrRoundDn(_min - _padMin, base/10), 9);
let _softMin = _min >= softMin && (softMinMode == 1 || softMinMode == 3 && _newMin <= softMin || softMinMode == 2 && _newMin >= softMin) ? softMin : inf;
let minLim = max(hardMin, _newMin < _softMin && _min >= _softMin ? _softMin : min(_softMin, _newMin));
let _padMax = nonZeroDelta * (delta == 0 ? (_max == 0 ? .1 : 1) : padMax);
let _newMax = roundDec(incrRoundUp(_max + _padMax, base/10), 9);
let _softMax = _max <= softMax && (softMaxMode == 1 || softMaxMode == 3 && _newMax >= softMax || softMaxMode == 2 && _newMax <= softMax) ? softMax : -inf;
let maxLim = min(hardMax, _newMax > _softMax && _max <= _softMax ? _softMax : max(_softMax, _newMax));
if (minLim == maxLim && minLim == 0)
maxLim = 100;
return [minLim, maxLim];
}
// alternative: https://stackoverflow.com/a/2254896
const numFormatter = new Intl.NumberFormat(domEnv ? nav.language : 'en-US');
export const fmtNum = val => numFormatter.format(val);
const M = Math;
export const PI = M.PI;
export const abs = M.abs;
export const floor = M.floor;
export const round = M.round;
export const ceil = M.ceil;
export const min = M.min;
export const max = M.max;
export const pow = M.pow;
export const sqrt = M.sqrt;
export const sign = M.sign;
export const log10 = M.log10;
export const log2 = M.log2;
// TODO: seems like this needs to match asinh impl if the passed v is tweaked?
export const sinh = (v, linthresh = 1) => M.sinh(v) * linthresh;
export const asinh = (v, linthresh = 1) => M.asinh(v / linthresh);
export const inf = Infinity;
export function numIntDigits(x) {
return (log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}
export function clamp(num, _min, _max) {
return min(max(num, _min), _max);
}
export function fnOrSelf(v) {
return typeof v == "function" ? v : () => v;
}
export const noop = () => {};
export const retArg0 = _0 => _0;
export const retArg1 = (_0, _1) => _1;
export const retNull = _ => null;
export const retTrue = _ => true;
export const retEq = (a, b) => a == b;
// this will probably prevent tick incrs > 14 decimal places
// (we generate up to 17 dec, see fixedDec const)
const fixFloat = v => roundDec(v, 14);
export function incrRound(num, incr) {
return fixFloat(roundDec(fixFloat(num/incr))*incr);
}
export function incrRoundUp(num, incr) {
return fixFloat(ceil(fixFloat(num/incr))*incr);
}
export function incrRoundDn(num, incr) {
return fixFloat(floor(fixFloat(num/incr))*incr);
}
// https://stackoverflow.com/a/48764436
// rounds half away from zero
export function roundDec(val, dec = 0) {
if (isInt(val))
return val;
// else if (dec == 0)
// return round(val);
let p = 10 ** dec;
let n = (val * p) * (1 + Number.EPSILON);
return round(n) / p;
}
// https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript/28203456#28203456
export function numDigits(x) {
return (log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}
export const fixedDec = new Map();
export function guessDec(num) {
return ((""+num).split(".")[1] || "").length;
}
export function genIncrs(base, minExp, maxExp, mults) {
let incrs = [];
let multDec = mults.map(guessDec);
for (let exp = minExp; exp < maxExp; exp++) {
let expa = abs(exp);
let mag = roundDec(pow(base, exp), expa);
for (let i = 0; i < mults.length; i++) {
let _incr = mults[i] * mag;
let dec = (_incr >= 0 && exp >= 0 ? 0 : expa) + (exp >= multDec[i] ? 0 : multDec[i]);
let incr = roundDec(_incr, dec);
incrs.push(incr);
fixedDec.set(incr, dec);
}
}
return incrs;
}
//export const assign = Object.assign;
export const EMPTY_OBJ = {};
export const EMPTY_ARR = [];
export const nullNullTuple = [null, null];
export const isArr = Array.isArray;
export const isInt = Number.isInteger;
export const isUndef = v => v === void 0;
export function isStr(v) {
return typeof v == 'string';
}
export function cmpObj(a, b) {
for (let k in a) {
if (b[k] != a[k])
return false;
}
return true;
}
export function isObj(v) {
let is = false;
if (v != null) {
let c = v.constructor;
is = c == null || c == Object;
}
return is;
}
export function fastIsObj(v) {
return v != null && typeof v == 'object';
}
const TypedArray = Object.getPrototypeOf(Uint8Array);
export function copy(o, _isObj = isObj) {
let out;
if (isArr(o)) {
let val = o.find(v => v != null);
if (isArr(val) || _isObj(val)) {
out = Array(o.length);
for (let i = 0; i < o.length; i++)
out[i] = copy(o[i], _isObj);
}
else
out = o.slice();
}
else if (o instanceof TypedArray) // also (ArrayBuffer.isView(o) && !(o instanceof DataView))
out = o.slice();
else if (_isObj(o)) {
out = {};
for (let k in o)
out[k] = copy(o[k], _isObj);
}
else
out = o;
return out;
}
export function assign(targ) {
let args = arguments;
for (let i = 1; i < args.length; i++) {
let src = args[i];
for (let key in src) {
if (isObj(targ[key]))
assign(targ[key], copy(src[key]));
else
targ[key] = copy(src[key]);
}
}
return targ;
}
// nullModes
const NULL_REMOVE = 0; // nulls are converted to undefined (e.g. for spanGaps: true)
const NULL_RETAIN = 1; // nulls are retained, with alignment artifacts set to undefined (default)
const NULL_EXPAND = 2; // nulls are expanded to include any adjacent alignment artifacts
// sets undefined values to nulls when adjacent to existing nulls (minesweeper)
function nullExpand(yVals, nullIdxs, alignedLen) {
for (let i = 0, xi, lastNullIdx = -1; i < nullIdxs.length; i++) {
let nullIdx = nullIdxs[i];
if (nullIdx > lastNullIdx) {
xi = nullIdx - 1;
while (xi >= 0 && yVals[xi] == null)
yVals[xi--] = null;
xi = nullIdx + 1;
while (xi < alignedLen && yVals[xi] == null)
yVals[lastNullIdx = xi++] = null;
}
}
}
// nullModes is a tables-matched array indicating how to treat nulls in each series
// output is sorted ASC on the joined field (table[0]) and duplicate join values are collapsed
export function join(tables, nullModes) {
if (allHeadersSame(tables)) {
// console.log('cheap join!');
let table = tables[0].slice();
for (let i = 1; i < tables.length; i++)
table.push(...tables[i].slice(1));
if (!isAsc(table[0]))
table = sortCols(table);
return table;
}
let xVals = new Set();
for (let ti = 0; ti < tables.length; ti++) {
let t = tables[ti];
let xs = t[0];
let len = xs.length;
for (let i = 0; i < len; i++)
xVals.add(xs[i]);
}
let data = [Array.from(xVals).sort((a, b) => a - b)];
let alignedLen = data[0].length;
let xIdxs = new Map();
for (let i = 0; i < alignedLen; i++)
xIdxs.set(data[0][i], i);
for (let ti = 0; ti < tables.length; ti++) {
let t = tables[ti];
let xs = t[0];
for (let si = 1; si < t.length; si++) {
let ys = t[si];
let yVals = Array(alignedLen).fill(undefined);
let nullMode = nullModes ? nullModes[ti][si] : NULL_RETAIN;
let nullIdxs = [];
for (let i = 0; i < ys.length; i++) {
let yVal = ys[i];
let alignedIdx = xIdxs.get(xs[i]);
if (yVal === null) {
if (nullMode != NULL_REMOVE) {
yVals[alignedIdx] = yVal;
if (nullMode == NULL_EXPAND)
nullIdxs.push(alignedIdx);
}
}
else
yVals[alignedIdx] = yVal;
}
nullExpand(yVals, nullIdxs, alignedLen);
data.push(yVals);
}
}
return data;
}
export const microTask = typeof queueMicrotask == "undefined" ? fn => Promise.resolve().then(fn) : queueMicrotask;
// TODO: https://github.com/dy/sort-ids (~2x faster for 1e5+ arrays)
function sortCols(table) {
let head = table[0];
let rlen = head.length;
let idxs = Array(rlen);
for (let i = 0; i < idxs.length; i++)
idxs[i] = i;
idxs.sort((i0, i1) => head[i0] - head[i1]);
let table2 = [];
for (let i = 0; i < table.length; i++) {
let row = table[i];
let row2 = Array(rlen);
for (let j = 0; j < rlen; j++)
row2[j] = row[idxs[j]];
table2.push(row2);
}
return table2;
}
// test if we can do cheap join (all join fields same)
function allHeadersSame(tables) {
let vals0 = tables[0][0];
let len0 = vals0.length;
for (let i = 1; i < tables.length; i++) {
let vals1 = tables[i][0];
if (vals1.length != len0)
return false;
if (vals1 != vals0) {
for (let j = 0; j < len0; j++) {
if (vals1[j] != vals0[j])
return false;
}
}
}
return true;
}
function isAsc(vals, samples = 100) {
const len = vals.length;
// empty or single value
if (len <= 1)
return true;
// skip leading & trailing nullish
let firstIdx = 0;
let lastIdx = len - 1;
while (firstIdx <= lastIdx && vals[firstIdx] == null)
firstIdx++;
while (lastIdx >= firstIdx && vals[lastIdx] == null)
lastIdx--;
// all nullish or one value surrounded by nullish
if (lastIdx <= firstIdx)
return true;
const stride = max(1, floor((lastIdx - firstIdx + 1) / samples));
for (let prevVal = vals[firstIdx], i = firstIdx + stride; i <= lastIdx; i += stride) {
const v = vals[i];
if (v != null) {
if (v <= prevVal)
return false;
prevVal = v;
}
}
return true;
}