@andrew_l/toolkit 0.2.12 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -198,6 +198,22 @@ const avg = (values) => {
198
198
  return sum / amount;
199
199
  };
200
200
 
201
+ const avgCircular = (values, max) => {
202
+ const len = values.length;
203
+ if (!len) return 0;
204
+ let sumX = 0;
205
+ let sumY = 0;
206
+ let angle;
207
+ let value;
208
+ for (value of values) {
209
+ angle = value / max * 2 * Math.PI;
210
+ sumX += Math.cos(angle);
211
+ sumY += Math.sin(angle);
212
+ }
213
+ angle = Math.atan2(sumY / len, sumX / len);
214
+ return (angle / (2 * Math.PI) * max + max) % max;
215
+ };
216
+
201
217
  function chunk(list, size = 1) {
202
218
  return list.reduce((res, item, index) => {
203
219
  if (index % size === 0) {
@@ -341,6 +357,90 @@ function shuffle(arr) {
341
357
  return result;
342
358
  }
343
359
 
360
+ class SortedArray extends Array {
361
+ #compareFn;
362
+ /**
363
+ * Creates a new SortedArray instance.
364
+ *
365
+ * @param compareFn The comparison function to determine the sort order
366
+ * @param items Optional initial items to add to the array (will be sorted immediately)
367
+ */
368
+ constructor(compareFn, items = []) {
369
+ super();
370
+ this.#compareFn = compareFn;
371
+ if (items.length > 0) {
372
+ super.push.apply(this, items.toSorted(this.#compareFn));
373
+ }
374
+ }
375
+ /**
376
+ * Inserts multiple items while maintaining sort order
377
+ * @param items The items to insert
378
+ * @returns The new length of the array
379
+ */
380
+ push(...items) {
381
+ var newItems = items.toSorted(this.#compareFn);
382
+ var newItemsLen = newItems.length;
383
+ var originalLen = this.length;
384
+ var result = new Array(originalLen + newItemsLen);
385
+ var i = 0, j = 0, k = 0;
386
+ while (i < originalLen && j < newItemsLen) {
387
+ if (this.#compareFn(this[i], newItems[j]) <= 0) {
388
+ result[k++] = this[i++];
389
+ } else {
390
+ result[k++] = newItems[j++];
391
+ }
392
+ }
393
+ while (i < originalLen) {
394
+ result[k++] = this[i++];
395
+ }
396
+ while (j < newItemsLen) {
397
+ result[k++] = newItems[j++];
398
+ }
399
+ this.length = 0;
400
+ super.push.apply(this, result);
401
+ return this.length;
402
+ }
403
+ /**
404
+ * Override Array methods that would break the sorted order
405
+ */
406
+ unshift(...items) {
407
+ return this.push(...items);
408
+ }
409
+ /**
410
+ * Creates a new SortedArray with the same comparison function
411
+ * @returns A new SortedArray instance
412
+ */
413
+ slice(start, end) {
414
+ var result = new SortedArray(this.#compareFn);
415
+ var sliced = super.slice(start, end);
416
+ super.push.apply(result, sliced);
417
+ return result;
418
+ }
419
+ /**
420
+ * Concatenates arrays or values while maintaining sort order
421
+ * @param items Arrays or values to concatenate
422
+ * @returns A new SortedArray with the concatenated elements
423
+ */
424
+ concat(...items) {
425
+ var result = new SortedArray(this.#compareFn, this);
426
+ for (const item of items) {
427
+ if (Array.isArray(item)) {
428
+ result.push.apply(result, item);
429
+ } else {
430
+ result.push(item);
431
+ }
432
+ }
433
+ return result;
434
+ }
435
+ }
436
+ ["map", "filter", "flatMap", "flat", "reverse", "sort"].forEach((key) => {
437
+ SortedArray.prototype[key] = function() {
438
+ const arr = Array.prototype[key].apply(this, arguments);
439
+ Object.setPrototypeOf(arr, Array.prototype);
440
+ return arr;
441
+ };
442
+ });
443
+
344
444
  const sum = (values) => {
345
445
  return values.reduce((a, b) => a + (isNumber(b) ? b : 0), 0);
346
446
  };
@@ -2480,46 +2580,274 @@ const ColorParser = {
2480
2580
  HEX: parseHEX
2481
2581
  };
2482
2582
 
2483
- const T0 = signed_crc_table();
2484
- function signed_crc_table() {
2485
- var c = 0, table = new Array(256);
2486
- for (var n = 0; n != 256; ++n) {
2487
- c = n;
2488
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2489
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2490
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2491
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2492
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2493
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2494
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2495
- c = c & 1 ? -2097792136 ^ c >>> 1 : c >>> 1;
2496
- table[n] = c;
2497
- }
2498
- return Int32Array ? new Int32Array(table) : table;
2499
- }
2500
- function crc32(str, seed = 0) {
2501
- var C = seed ^ -1;
2502
- for (var i = 0, L = str.length, c = 0, d = 0; i < L; ) {
2503
- c = str.charCodeAt(i++);
2504
- if (c < 128) {
2505
- C = C >>> 8 ^ T0[(C ^ c) & 255];
2506
- } else if (c < 2048) {
2507
- C = C >>> 8 ^ T0[(C ^ (192 | c >> 6 & 31)) & 255];
2508
- C = C >>> 8 ^ T0[(C ^ (128 | c & 63)) & 255];
2509
- } else if (c >= 55296 && c < 57344) {
2510
- c = (c & 1023) + 64;
2511
- d = str.charCodeAt(i++) & 1023;
2512
- C = C >>> 8 ^ T0[(C ^ (240 | c >> 8 & 7)) & 255];
2513
- C = C >>> 8 ^ T0[(C ^ (128 | c >> 2 & 63)) & 255];
2514
- C = C >>> 8 ^ T0[(C ^ (128 | d >> 6 & 15 | (c & 3) << 4)) & 255];
2515
- C = C >>> 8 ^ T0[(C ^ (128 | d & 63)) & 255];
2516
- } else {
2517
- C = C >>> 8 ^ T0[(C ^ (224 | c >> 12 & 15)) & 255];
2518
- C = C >>> 8 ^ T0[(C ^ (128 | c >> 6 & 63)) & 255];
2519
- C = C >>> 8 ^ T0[(C ^ (128 | c & 63)) & 255];
2520
- }
2583
+ var encoder = new TextEncoder();
2584
+ var TABLE = new Int32Array([
2585
+ 0,
2586
+ 1996959894,
2587
+ 3993919788,
2588
+ 2567524794,
2589
+ 124634137,
2590
+ 1886057615,
2591
+ 3915621685,
2592
+ 2657392035,
2593
+ 249268274,
2594
+ 2044508324,
2595
+ 3772115230,
2596
+ 2547177864,
2597
+ 162941995,
2598
+ 2125561021,
2599
+ 3887607047,
2600
+ 2428444049,
2601
+ 498536548,
2602
+ 1789927666,
2603
+ 4089016648,
2604
+ 2227061214,
2605
+ 450548861,
2606
+ 1843258603,
2607
+ 4107580753,
2608
+ 2211677639,
2609
+ 325883990,
2610
+ 1684777152,
2611
+ 4251122042,
2612
+ 2321926636,
2613
+ 335633487,
2614
+ 1661365465,
2615
+ 4195302755,
2616
+ 2366115317,
2617
+ 997073096,
2618
+ 1281953886,
2619
+ 3579855332,
2620
+ 2724688242,
2621
+ 1006888145,
2622
+ 1258607687,
2623
+ 3524101629,
2624
+ 2768942443,
2625
+ 901097722,
2626
+ 1119000684,
2627
+ 3686517206,
2628
+ 2898065728,
2629
+ 853044451,
2630
+ 1172266101,
2631
+ 3705015759,
2632
+ 2882616665,
2633
+ 651767980,
2634
+ 1373503546,
2635
+ 3369554304,
2636
+ 3218104598,
2637
+ 565507253,
2638
+ 1454621731,
2639
+ 3485111705,
2640
+ 3099436303,
2641
+ 671266974,
2642
+ 1594198024,
2643
+ 3322730930,
2644
+ 2970347812,
2645
+ 795835527,
2646
+ 1483230225,
2647
+ 3244367275,
2648
+ 3060149565,
2649
+ 1994146192,
2650
+ 31158534,
2651
+ 2563907772,
2652
+ 4023717930,
2653
+ 1907459465,
2654
+ 112637215,
2655
+ 2680153253,
2656
+ 3904427059,
2657
+ 2013776290,
2658
+ 251722036,
2659
+ 2517215374,
2660
+ 3775830040,
2661
+ 2137656763,
2662
+ 141376813,
2663
+ 2439277719,
2664
+ 3865271297,
2665
+ 1802195444,
2666
+ 476864866,
2667
+ 2238001368,
2668
+ 4066508878,
2669
+ 1812370925,
2670
+ 453092731,
2671
+ 2181625025,
2672
+ 4111451223,
2673
+ 1706088902,
2674
+ 314042704,
2675
+ 2344532202,
2676
+ 4240017532,
2677
+ 1658658271,
2678
+ 366619977,
2679
+ 2362670323,
2680
+ 4224994405,
2681
+ 1303535960,
2682
+ 984961486,
2683
+ 2747007092,
2684
+ 3569037538,
2685
+ 1256170817,
2686
+ 1037604311,
2687
+ 2765210733,
2688
+ 3554079995,
2689
+ 1131014506,
2690
+ 879679996,
2691
+ 2909243462,
2692
+ 3663771856,
2693
+ 1141124467,
2694
+ 855842277,
2695
+ 2852801631,
2696
+ 3708648649,
2697
+ 1342533948,
2698
+ 654459306,
2699
+ 3188396048,
2700
+ 3373015174,
2701
+ 1466479909,
2702
+ 544179635,
2703
+ 3110523913,
2704
+ 3462522015,
2705
+ 1591671054,
2706
+ 702138776,
2707
+ 2966460450,
2708
+ 3352799412,
2709
+ 1504918807,
2710
+ 783551873,
2711
+ 3082640443,
2712
+ 3233442989,
2713
+ 3988292384,
2714
+ 2596254646,
2715
+ 62317068,
2716
+ 1957810842,
2717
+ 3939845945,
2718
+ 2647816111,
2719
+ 81470997,
2720
+ 1943803523,
2721
+ 3814918930,
2722
+ 2489596804,
2723
+ 225274430,
2724
+ 2053790376,
2725
+ 3826175755,
2726
+ 2466906013,
2727
+ 167816743,
2728
+ 2097651377,
2729
+ 4027552580,
2730
+ 2265490386,
2731
+ 503444072,
2732
+ 1762050814,
2733
+ 4150417245,
2734
+ 2154129355,
2735
+ 426522225,
2736
+ 1852507879,
2737
+ 4275313526,
2738
+ 2312317920,
2739
+ 282753626,
2740
+ 1742555852,
2741
+ 4189708143,
2742
+ 2394877945,
2743
+ 397917763,
2744
+ 1622183637,
2745
+ 3604390888,
2746
+ 2714866558,
2747
+ 953729732,
2748
+ 1340076626,
2749
+ 3518719985,
2750
+ 2797360999,
2751
+ 1068828381,
2752
+ 1219638859,
2753
+ 3624741850,
2754
+ 2936675148,
2755
+ 906185462,
2756
+ 1090812512,
2757
+ 3747672003,
2758
+ 2825379669,
2759
+ 829329135,
2760
+ 1181335161,
2761
+ 3412177804,
2762
+ 3160834842,
2763
+ 628085408,
2764
+ 1382605366,
2765
+ 3423369109,
2766
+ 3138078467,
2767
+ 570562233,
2768
+ 1426400815,
2769
+ 3317316542,
2770
+ 2998733608,
2771
+ 733239954,
2772
+ 1555261956,
2773
+ 3268935591,
2774
+ 3050360625,
2775
+ 752459403,
2776
+ 1541320221,
2777
+ 2607071920,
2778
+ 3965973030,
2779
+ 1969922972,
2780
+ 40735498,
2781
+ 2617837225,
2782
+ 3943577151,
2783
+ 1913087877,
2784
+ 83908371,
2785
+ 2512341634,
2786
+ 3803740692,
2787
+ 2075208622,
2788
+ 213261112,
2789
+ 2463272603,
2790
+ 3855990285,
2791
+ 2094854071,
2792
+ 198958881,
2793
+ 2262029012,
2794
+ 4057260610,
2795
+ 1759359992,
2796
+ 534414190,
2797
+ 2176718541,
2798
+ 4139329115,
2799
+ 1873836001,
2800
+ 414664567,
2801
+ 2282248934,
2802
+ 4279200368,
2803
+ 1711684554,
2804
+ 285281116,
2805
+ 2405801727,
2806
+ 4167216745,
2807
+ 1634467795,
2808
+ 376229701,
2809
+ 2685067896,
2810
+ 3608007406,
2811
+ 1308918612,
2812
+ 956543938,
2813
+ 2808555105,
2814
+ 3495958263,
2815
+ 1231636301,
2816
+ 1047427035,
2817
+ 2932959818,
2818
+ 3654703836,
2819
+ 1088359270,
2820
+ 936918e3,
2821
+ 2847714899,
2822
+ 3736837829,
2823
+ 1202900863,
2824
+ 817233897,
2825
+ 3183342108,
2826
+ 3401237130,
2827
+ 1404277552,
2828
+ 615818150,
2829
+ 3134207493,
2830
+ 3453421203,
2831
+ 1423857449,
2832
+ 601450431,
2833
+ 3009837614,
2834
+ 3294710456,
2835
+ 1567103746,
2836
+ 711928724,
2837
+ 3020668471,
2838
+ 3272380065,
2839
+ 1510334235,
2840
+ 755167117
2841
+ ]);
2842
+ function crc32(current, seed) {
2843
+ if (isString(current)) {
2844
+ return crc32(encoder.encode(current), seed);
2845
+ }
2846
+ var crc = seed === 0 ? 0 : ~~seed ^ -1;
2847
+ for (var index = 0; index < current.length; index++) {
2848
+ crc = TABLE[(crc ^ current[index]) & 255] ^ crc >>> 8;
2521
2849
  }
2522
- return ~C;
2850
+ return crc ^ -1;
2523
2851
  }
2524
2852
 
2525
2853
  function isDateObject(value) {
@@ -4569,6 +4897,7 @@ exports.BrowserAssertionError = BrowserAssertionError;
4569
4897
  exports.CancellablePromise = CancellablePromise;
4570
4898
  exports.ColorParser = ColorParser;
4571
4899
  exports.EJSON = instance;
4900
+ exports.EJSONInstance = EJSON;
4572
4901
  exports.EJSONStream = EJSONStream;
4573
4902
  exports.FindMean = FindMean;
4574
4903
  exports.FixedMap = FixedMap;
@@ -4576,6 +4905,7 @@ exports.FixedWeakMap = FixedWeakMap;
4576
4905
  exports.LruCache = LruCache;
4577
4906
  exports.Queue = Queue;
4578
4907
  exports.SimpleEventEmitter = SimpleEventEmitter;
4908
+ exports.SortedArray = SortedArray;
4579
4909
  exports.TWEMOJI_REGEX = TWEMOJI_REGEX;
4580
4910
  exports.TimeBucket = TimeBucket;
4581
4911
  exports.TimeSpan = TimeSpan;
@@ -4587,6 +4917,7 @@ exports.asyncFind = asyncFind;
4587
4917
  exports.asyncForEach = asyncForEach;
4588
4918
  exports.asyncMap = asyncMap;
4589
4919
  exports.avg = avg;
4920
+ exports.avgCircular = avgCircular;
4590
4921
  exports.base64ToBytes = base64ToBytes;
4591
4922
  exports.bigIntBytes = bigIntBytes;
4592
4923
  exports.bigIntFromBytes = bigIntFromBytes;