forked from deepakshrma/30-seconds-of-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
1262 lines (1262 loc) · 41.7 KB
/
util.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var HTMLEscapeChars1;
(function (HTMLEscapeChars1) {
HTMLEscapeChars1["&"] = "&";
HTMLEscapeChars1["<"] = "<";
HTMLEscapeChars1[">"] = ">";
HTMLEscapeChars1["'"] = "'";
HTMLEscapeChars1['"'] = """;
})(HTMLEscapeChars1 || (HTMLEscapeChars1 = {}));
var HTMLUnEscapeChars1;
(function (HTMLUnEscapeChars1) {
HTMLUnEscapeChars1["&"] = "&";
HTMLUnEscapeChars1["<"] = "<";
HTMLUnEscapeChars1[">"] = ">";
HTMLUnEscapeChars1["'"] = "'";
HTMLUnEscapeChars1["""] = '"';
})(HTMLUnEscapeChars1 || (HTMLUnEscapeChars1 = {}));
const htmlEscapeReg = new RegExp(
`[${Object.keys(HTMLEscapeChars1).join("")}]`,
"g"
);
const htmlUnEscapeReg = new RegExp(
`${Object.keys(HTMLUnEscapeChars1).join("|")}`,
"g"
);
const accumulate1 = (...nums) =>
nums.reduce((acc, n) => [...acc, n + +acc.slice(-1)], []);
const all1 = (arr, fn = Boolean) => arr.every(fn);
const allEqual1 = (arr) => arr.every((val) => val === arr[0]);
const and1 = (a, b) => Boolean(a) && Boolean(b);
const any1 = (arr, fn = Boolean) => arr.some(fn);
const some1 = (arr, fn = Boolean) => arr.some(fn);
const aperture1 = (n, arr) =>
n >= arr.length
? []
: arr.slice(n - 1).map((v, i) => [...arr.slice(i, i + n - 1), v]);
const approximatelyEqual1 = (v1, v2, epsilon = 0.001) =>
Math.abs(v1 - v2) < epsilon;
const arrayToCSV1 = (arr, delimiter = ",") =>
arr
.map((v) =>
v
.map((x) => (typeof x === "string" ? `"${x.replace(/"/g, '""')}"` : x))
.join(delimiter)
)
.join("\n");
const arrayToHtmlList1 = (arr, listID) => {
let el = document.querySelector("#" + listID);
if (el) {
el.innerHTML += arr.map((item) => `<li>${item}</li>`).join("");
}
};
const ary1 = (fn, n) => (...args) => fn(...args.slice(0, n));
const attempt1 = (fn, ...args) => {
try {
return fn(...args);
} catch (e) {
return e instanceof Error ? e : new Error(e);
}
};
const attempt21 = (fn, ...args) => {
try {
return [fn(...args), null];
} catch (e) {
return [null, e instanceof Error ? e : new Error(e)];
}
};
const average1 = (...nums) =>
nums.reduce((acc, val) => acc + val, 0) / nums.length;
const averageBy1 = (arr, fn) => {
const mapper = typeof fn === "function" ? fn : (val) => val[fn];
return arr.reduce((acc, val) => acc + mapper(val), 0) / arr.length;
};
const bifurcate1 = (arr, filter) =>
arr.reduce(
(acc, val, i) => {
acc[filter[i] ? 0 : 1].push(val);
return acc;
},
[[], []]
);
const bifurcateBy1 = (arr, filter) =>
arr.reduce(
(acc, val) => {
acc[filter(val) ? 0 : 1].push(val);
return acc;
},
[[], []]
);
const binary1 = (fn) => (...[v1, v2]) => fn(v1, v2);
const bind1 = (fn, context, ...boundArgs) => (...args) =>
fn.apply(context, [...boundArgs, ...args]);
const bindAll1 = (obj, ...fns) =>
fns.forEach((key) => {
if (typeof obj[key] === "function") {
const f = obj[key];
obj[key] = function (...args) {
return f.apply(obj, args);
};
}
});
const binomialCoefficient1 = (n, k) => {
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
if (k === 1 || k === n - 1) return n;
if (n - k < k) k = n - k;
let res = n;
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
return Math.round(res);
};
const both1 = (f, g) => (...args) => f(...args) && g(...args);
const call1 = (key, ...args) => (context) => context[key](...args);
const capitalize1 = (str = "", lowerRest = false) =>
str.slice(0, 1).toUpperCase() +
(lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
const capitalizeEveryWord1 = (str = "") =>
str.replace(/\b[a-z]/g, (__char) => __char.toUpperCase());
const castArray1 = (val) => (Array.isArray(val) ? val : [val]);
const celsiusToFahrenheit1 = (degrees) => 1.8 * degrees + 32;
const chunk1 = (arr, size) =>
Array.from(
{
length: Math.ceil(arr.length / size),
},
(_, i) => arr.slice(i * size, i * size + size)
);
const colorize1 = new (class {
color = (code, ended = false, ...messages) =>
`\x1b[${code}m${messages.join(" ")}${ended ? "\x1b[0m" : ""}`;
black = this.color.bind(null, 30, false);
red = this.color.bind(null, 31, false);
green = this.color.bind(null, 32, false);
yellow = this.color.bind(this, 33, false);
blue = this.color.bind(this, 34, false);
magenta = this.color.bind(this, 35, false);
cyan = this.color.bind(this, 36, false);
white = this.color.bind(this, 37, false);
bgBlack = this.color.bind(this, 40, true);
bgRed = this.color.bind(this, 41, true);
bgGreen = this.color.bind(this, 42, true);
bgYellow = this.color.bind(this, 43, true);
bgBlue = this.color.bind(this, 44, true);
bgMagenta = this.color.bind(this, 45, true);
bgCyan = this.color.bind(this, 46, true);
bgWhite = this.color.bind(this, 47, true);
})();
const colors1 = colorize1;
const compact1 = (arr) => arr.filter(Boolean);
const compactWhitespace1 = (str) => str.replace(/\s{2,}/g, " ");
const complement1 = (fn) => (...args) => !fn(...args);
const compose1 = (...fns) =>
fns.reduce((f, g) => (...args) => f(...castArray1(g(...args))));
const composeRight1 = (...fns) =>
fns.reduce((f, g) => (...args) => g(...castArray1(f(...args))));
const contains1 = (s1, s2 = "") =>
s1.toLowerCase().indexOf(s2.toLowerCase()) !== -1;
const containsWhitespace1 = (str) => /\s/.test(str);
const countBy1 = (arr, fn) => {
const mapper = typeof fn === "function" ? fn : (val) => val[fn];
return arr.reduce((acc, val) => {
const value = mapper(val);
acc[value] = (acc[value] || 0) + 1;
return acc;
}, {});
};
const countOccurrences1 = (arr, val) =>
arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
const createElement1 = (str) => {
const el = document.createElement("div");
el.innerHTML = str;
return el.firstElementChild;
};
const createEventHub1 = () => ({
hub: Object.create(null),
emit(event, data) {
(this.hub[event] || []).forEach((handler) => handler(data));
},
on(event, handler) {
if (!this.hub[event]) this.hub[event] = [];
this.hub[event].push(handler);
},
off(event, handler) {
const i = (this.hub[event] || []).findIndex((h) => h === handler);
if (i > -1) this.hub[event].splice(i, 1);
if (this.hub[event]?.length === 0) delete this.hub[event];
},
});
const CSVToArray1 = (data, delimiter = ",", omitFirstRow = false) =>
data
.slice(omitFirstRow ? data.indexOf("\n") + 1 : 0)
.split("\n")
.map((v) => v.split(delimiter));
const CSVToJSON1 = (data, delimiter = ",") => {
const titles = data.slice(0, data.indexOf("\n")).split(delimiter);
return data
.slice(data.indexOf("\n") + 1)
.split("\n")
.map((v) => {
const values = v.split(delimiter);
return titles.reduce(
(obj, title, index) => ((obj[title] = values[index]), obj),
{}
);
});
};
const curry1 = (fn, arity = fn.length, ...args) =>
arity <= args.length ? fn(...args) : curry1.bind(null, fn, arity, ...args);
const dayOfYear1 = (date) => {
if (isString1(date)) {
date = new Date(date);
}
if (!isValidDate1(date)) throw new Error(`Invalid Date string`);
return Math.floor(
(date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) /
1000 /
60 /
60 /
24
);
};
const debounce1 = (fn, ms = 300) => {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};
const deepClone1 = (obj) => {
if (obj === null) return null;
let clone = {
...obj,
};
Object.keys(clone).forEach(
(key) =>
(clone[key] =
typeof obj[key] === "object" ? deepClone1(obj[key]) : obj[key])
);
return Array.isArray(obj) && obj.length
? (clone.length = obj.length) && Array.from(clone)
: Array.isArray(obj)
? Array.from(obj)
: clone;
};
const deepFlatten1 = (arr) => {
if (typeof Array.prototype.flat !== "undefined") return arr.flat(Infinity);
return [].concat(...arr.map((v) => (Array.isArray(v) ? deepFlatten1(v) : v)));
};
const deepFreeze1 = (obj) => {
Object.keys(obj).forEach((prop) => {
if (typeof obj[prop] === "object" && !Object.isFrozen(obj[prop])) {
deepFreeze1(obj[prop]);
}
});
return Object.freeze(obj);
};
const deepGet1 = (obj, keys, defaultValue = undefined, delimiter = ".") => {
if (isString1(keys)) {
keys = keys.split(delimiter);
}
return keys.reduce((xs, x) => (xs && xs[x] ? xs[x] : defaultValue), obj);
};
const defaults1 = (obj, ...defs) => Object.assign({}, obj, ...defs, obj);
const delay1 = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
const delayedPromise1 = (wait = 300, ...args) =>
new Promise((resolve) => {
delay1(resolve, wait, ...args);
});
const either1 = (f, g) => (...args) => f(...args) || g(...args);
const equals1 = (a, b) => {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
if (!a || !b || (typeof a !== "object" && typeof b !== "object")) {
return a === b;
}
const objA = a;
const objB = b;
if (objA.prototype !== objA.prototype) return false;
let keys = Object.keys(objA);
if (keys.length !== Object.keys(objB).length) return false;
return keys.every((k) => equals1(objA[k], objB[k]));
};
const deepEquals1 = (a, b) => {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
if (!a || !b || (typeof a !== "object" && typeof b !== "object")) {
return a === b;
}
const objA = a;
const objB = b;
if (objA.prototype !== objA.prototype) return false;
let keys = Object.keys(objA);
if (keys.length !== Object.keys(objB).length) return false;
return keys.every((k) => equals1(objA[k], objB[k]));
};
const downloadCSV1 = (csvContent, name = "download.csv") => {
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", name);
document.body.appendChild(link);
link.click();
};
const escapeHTML1 = (str) =>
str.replace(htmlEscapeReg, (tag) => HTMLEscapeChars1[tag] || tag);
const unescapeHTML1 = (str) =>
str.replace(htmlUnEscapeReg, (tag) => HTMLUnEscapeChars1[tag] || tag);
const escapeRegExp1 = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const factorial1 = (n) => (n <= 1 ? 1 : n * factorial1(n - 1));
const memoize1 = (fn) => {
const cache = new Map();
const cached = function (val) {
return cache.has(val)
? cache.get(val)
: cache.set(val, fn.call(this, val)) && cache.get(val);
};
cached.cache = cache;
return cached;
};
const fahrenheitToCelsius1 = (degrees) => ((degrees - 32) * 5) / 9;
const filterNonUnique1 = (arr) =>
arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i));
const filterNonUniqueBy1 = (arr, fn) =>
arr.filter((v, i) => arr.every((x, j) => (i === j) === fn(v, x, i, j)));
const findKey1 = (obj, fn) =>
Object.keys(obj).find((key) => fn(obj[key], key, obj));
const flatten1 = (arr, depth = 1) => {
if (typeof Array.prototype.flat !== "undefined") return arr.flat(depth);
return arr.reduce(
(a, v) =>
a.concat(depth > 1 && Array.isArray(v) ? flatten1(v, depth - 1) : v),
[]
);
};
const forEachRight1 = (array = [], callback) => {
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
callback(element, index, array);
}
};
const formatDuration1 = (ms) => {
ms = Math.abs(ms);
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000,
};
return Object.entries(time)
.filter((val) => val[1] !== 0)
.map(([key, val]) => `${val} ${key}${val !== 1 ? "s" : ""}`)
.join(", ");
};
const padLeft = (str, num = 2, fill = "0") => String(str).padStart(num, fill);
const formatDate1 = (formatStr, date) => {
const d = new Date(date);
const time = {
YY: padLeft(d.getFullYear()).substr(2, 4),
YYYY: padLeft(d.getFullYear()),
MM: padLeft(d.getMonth() + 1),
DD: padLeft(d.getDate()),
hh: padLeft(d.getHours()),
mm: padLeft(d.getMinutes()),
ss: padLeft(d.getSeconds()),
M: padLeft(d.getMilliseconds(), 3),
};
return formatStr.replace(
new RegExp(`${Object.keys(time).join("|")}`, "g"),
(subStr) => {
return time[subStr] || "";
}
);
};
const get1 = (from, selector, defaultValue = undefined) =>
selector
.replace(/\[([^\[\]]*)\]/g, ".$1.")
.split(".")
.filter((t) => t !== "")
.reduce((prev, cur) => prev && prev[cur], from) || defaultValue;
const getAll1 = (from, ...selectors) =>
[...selectors].map((s) => get1(from, s));
const getBaseURL1 = (url) =>
url.indexOf("?") > 0 ? url.slice(0, url.indexOf("?")) : url;
const getType1 = (v) =>
v === undefined
? "undefined"
: v === null
? "null"
: v.constructor.name.toLowerCase();
const getURLParameters1 = (url) => {
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce((a, v) => {
const [key, value] = v.split("=");
if (a[key]) {
a[key] = (typeof a[key] === "string" ? [a[key]] : a[key]).concat(value);
} else {
a[key] = value;
}
return a;
}, {});
};
const mapToObject1 = (map) => {
let result = {};
for (let [key, value] of map.entries()) {
result[key] = value;
}
return result;
};
const groupBy1 = (arr, fn) =>
arr.map(isString1(fn) ? (val) => val[fn] : fn).reduce((acc, val, i) => {
acc[val] = (acc[val] || []).concat(arr[i]);
return acc;
}, {});
const hasFlags1 = (args, ...flags) =>
flags.every((flag) =>
args.includes(/^-{1,2}/.test(flag) ? flag : "--" + flag)
);
const hexToRGB1 = (hex) => {
hex = hex.startsWith("#") ? hex.slice(1) : hex;
if (hex.length === 3) {
hex = Array.from(hex).reduce((str, x) => str + x + x, "");
}
const values = hex
.split(/([a-z0-9]{2,2})/)
.filter(Boolean)
.map((x) => parseInt(x, 16));
return `rgb${values.length == 4 ? "a" : ""}(${values.join(", ")})`;
};
const hexToRGB21 = (hex) => {
let hexChars = Array.from(hex.startsWith("#") ? hex.slice(1) : hex);
if (hexChars.length === 3) {
hexChars = hexChars.reduce((str, x) => [...str, x, x], []);
}
const values = chunk1(hexChars, 2).map(([v1, v2]) => parseInt(v1 + v2, 16));
return `rgb${values.length == 4 ? "a" : ""}(${values.join(", ")})`;
};
const hide1 = (...el) => [...el].forEach((e) => (e.style.display = "none"));
const httpsRedirect1 = () => {
if (location.protocol !== "https:")
location.replace("https://" + location.href.split("//")[1]);
};
const includesAll1 = (arr, values) => values.every((v) => arr.includes(v));
const indentString1 = (str, count, indent = " ") => {
indent = indent.repeat(count);
return str.replace(/^/gm, indent);
};
const isFunction1 = (str) => {
return typeof str === "function";
};
const isArrayLike1 = (obj) => {
return (
obj[Symbol.iterator] instanceof Function && obj.entries instanceof Function
);
};
const isString1 = (str) => {
return typeof str === "string";
};
function isValidDate1(date) {
return date instanceof Date && !isNaN(date.getTime());
}
const fillArray1 = (n, val = 0) => Array(n).fill(val);
const initializeArray1 = (n, val = 0) => Array(n).fill(val);
const inRange1 = (n, start, end) => {
if (end && start > end) [end, start] = [start, end];
return end === undefined ? n >= 0 && n < start : n >= start && n < end;
};
const insertAt1 = (arr, i, ...v) => {
arr.splice(i + 1, 0, ...v);
return arr;
};
const insertAtImmutable1 = (arr, i, ...v) => {
return [...arr.slice(0, i + 1), ...v, ...arr.slice(i + 1)];
};
const intersection1 = (a, b) => {
const s = new Set(b);
return [...new Set(a)].filter((x) => s.has(x));
};
const intersectionBy1 = (a, b, fn) => {
const s = new Set(b.map(fn));
return [...new Set(a)].filter((x) => s.has(fn(x)));
};
const intersectionWith1 = (a, b, comp) =>
a.filter((x) => b.findIndex((y) => comp(x, y)) !== -1);
const is1 = (type, val) =>
![, null].includes(val) &&
(isString1(type) ? val.constructor.name === type : val.constructor === type);
const isAfterDate1 = (dateA, dateB) => dateA > dateB;
const isBeforeDate1 = (dateA, dateB) => dateA < dateB;
const isBrowser1 = () =>
![typeof window, typeof document].includes("undefined");
const isEmpty1 = (val) => val == null || !(Object.keys(val) || val).length;
const isLeapYear1 = (year) => new Date(year, 1, 29).getMonth() === 1;
const isLowerCase1 = (str) => str === str.toLowerCase();
const isNil1 = (val) => val === undefined || val === null;
const isNull1 = (val) => val === null;
const isNumber1 = (val) => typeof val === "number" && val === val;
const isObject1 = (obj) => obj === Object(obj);
const isPlainObject1 = (val) =>
!!val && typeof val === "object" && val.constructor === Object;
const isPrimitive1 = (val) => Object(val) !== val;
const isSameDate1 = (dateA, dateB) =>
dateA.toISOString() === dateB.toISOString();
const isSorted1 = (arr) => {
let direction = -(arr[0] - arr[1]);
for (let [i, val] of arr.entries()) {
direction = !direction ? -(arr[i - 1] - arr[i]) : direction;
if (i === arr.length - 1)
return !direction ? 0 : direction / Math.abs(direction);
else if ((val - arr[i + 1]) * direction > 0) return 0;
}
};
const isUpperCase1 = (str) => str === str.toUpperCase();
const isValidJSON1 = (str) => {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
const JSONtoCSV1 = (arr, columns, delimiter = ",") =>
[
columns.join(delimiter),
...arr.map((obj) =>
columns.reduce(
(acc, key) =>
`${acc}${!acc.length ? "" : delimiter}"${!obj[key] ? "" : obj[key]}"`,
""
)
),
].join("\n");
const last1 = (arr = []) => arr[arr.length - 1];
const listenOnce1 = (el, evt, fn) => {
let fired = false;
el.addEventListener(evt, (e) => {
if (!fired) fn(e);
fired = true;
});
};
const lowercaseKeys1 = (obj, deep = false) =>
Object.keys(obj).reduce((acc, key) => {
acc[key.toLowerCase()] =
deep && typeof obj[key] === "object"
? lowercaseKeys1(obj[key])
: obj[key];
return acc;
}, {});
const mapKeys1 = (obj, fn) =>
Object.keys(obj).reduce((acc, k) => {
acc[fn(obj[k], k, obj)] = obj[k];
return acc;
}, {});
const mapObject1 = (arr, fn) =>
arr.reduce((acc, el, i) => {
acc[el] = fn(el, i, arr);
return acc;
}, {});
const mapString1 = (str, fn) => {
const chars = [...str];
return chars.map((c, i) => fn(c, i, chars)).join("");
};
const map1 = (array, fn) => {
const chars = Array.isArray(array) ? array : [...array];
return chars.map((c, i) => fn(c, i, chars));
};
const mask1 = (cc, num = 4, mask1 = "*") =>
String(cc).slice(-num).padStart(String(cc).length, mask1);
const matches1 = (obj, source) =>
Object.keys(source).every(
(key) => obj.hasOwnProperty(key) && obj[key] === source[key]
);
const matchesWith1 = (obj, source, fn) =>
Object.keys(source).every((key) =>
obj.hasOwnProperty(key) && fn
? fn(obj[key], source[key], key, obj, source)
: obj[key] == source[key]
);
const maxBy1 = (arr, fn) =>
Math.max(...arr.map(isString1(fn) ? (val) => val[fn] : fn));
const maxDate1 = (dates) => new Date(Math.max(...dates.map(Number)));
const maxN1 = (arr, n = 1, order = 1) =>
[...arr].sort((a, b) => order * (b - a)).slice(0, n);
const merge1 = (...objs) =>
[...objs].reduce(
(acc, obj) =>
Object.keys(obj).reduce((a, k) => {
acc[k] = acc.hasOwnProperty(k)
? [].concat(acc[k]).concat(obj[k])
: obj[k];
return acc;
}, {}),
{}
);
const midpoint1 = ([x1, y1], [x2, y2]) => [(x1 + x2) / 2, (y1 + y2) / 2];
const minBy1 = (arr, fn) =>
Math.min(...arr.map(isString1(fn) ? (val) => val[fn] : fn));
const sortBy1 = (
arr,
$fn = (s1, s2) => order * String(s1).localeCompare(String(s2)),
order = SortByOrder1.ASC
) => {
let fn = $fn;
return [...arr].sort(fn);
};
var SortByOrder1;
(function (SortByOrder1) {
SortByOrder1[(SortByOrder1["ASC"] = 1)] = "ASC";
SortByOrder1[(SortByOrder1["DESC"] = -1)] = "DESC";
})(SortByOrder1 || (SortByOrder1 = {}));
const sortByKey1 = (arr, key, order = SortByOrder1.ASC) => {
return [...arr].sort(
(s1, s2) => order * String(s1[key]).localeCompare(String(s2[key]))
);
};
const mostFrequent1 = (arr) =>
Object.entries(
arr.reduce((a, v) => {
a[String(v)] = a[String(v)] ? a[String(v)] + 1 : 1;
return a;
}, {})
).reduce((a, v) => (v[1] >= a[1] ? v : a), [-1, 0])[0];
const mostPerformant1 = (fns, iterations = 10000) => {
const times = fns.map((fn) => {
const before = performance.now();
for (let i = 0; i < iterations; i++) fn();
return performance.now() - before;
});
return times.indexOf(Math.min(...times));
};
const negate1 = (func) => (...args) => !func(...args);
const nest1 = (items, id = null, link = "parent_id") =>
items
.filter((item) => item[link] === id)
.map((item) => ({
...item,
children: nest1(items, item.id, link),
}));
const nodeListToArray1 = (nodeList) => [...nodeList];
const toArray1 = (arrLike) => [...arrLike];
const none1 = (arr, fn = Boolean) => !arr.some(fn);
const not1 = (a) => !a;
const nthArg1 = (n) => (...args) => args.slice(n)[0];
const nthElement1 = curry1(
(n = 0, arr) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0],
2
);
const objectToQueryString1 = (queryParameters) => {
return queryParameters
? Object.entries(queryParameters).reduce(
(queryString, [key, val], index) => {
const symbol = queryString.length === 0 ? "?" : "&";
queryString += val ? `${symbol}${key}=${val}` : "";
return queryString;
},
""
)
: "";
};
const offset1 = (arr, offset1) => [
...arr.slice(offset1),
...arr.slice(0, offset1),
];
const omit1 = (obj, arr) =>
Object.keys(obj)
.filter((k) => !arr.includes(k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
const omitBy1 = (obj, fn) =>
Object.keys(obj)
.filter((k) => !fn(obj[k], k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
const or1 = (a, b) => a || b;
const orderBy1 = (arr, props, orders) =>
[...arr].sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
const [p1, p2] =
orders && orders[i] === "desc"
? [b[prop], a[prop]]
: [a[prop], b[prop]];
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
}
return acc;
}, 0)
);
const orderByFunc1 = (arr, props, fn) =>
[...arr].sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
const [p1, p2] = [a[prop], b[prop]];
acc = fn(p1, p2, prop);
}
return acc;
}, 0)
);
const pad1 = (str, length, __char = " ") => {
const s = String(str);
return s.padStart((s.length + length) / 2, __char).padEnd(length, __char);
};
const parseCookie1 = (str) =>
str
.split(";")
.map((v) => v.split("="))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
const partial1 = (fn, ...partials) => (...args) => fn(...partials, ...args);
const partialRight1 = (fn, ...partials) => (...args) =>
fn(...args, ...partials);
const partition1 = (arr, fn) =>
arr.reduce(
(acc, val, i, arr1) => {
acc[fn(val, i, arr1) ? 0 : 1].push(val);
return acc;
},
[[], []]
);
const partitionBy1 = (arr, fn) =>
arr.reduce(
({ res, last: last1 }, v, i, a) => {
const next = fn(v, i, a);
if (next !== last1) res.push([v]);
else res[res.length - 1].push(v);
return {
res,
last: next,
};
},
{
res: [],
}
).res;
const pick1 = (obj, arr) =>
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
const pickBy1 = (obj, fn) =>
Object.keys(obj)
.filter((k) => fn(obj[k], k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
const pipeAsyncFunctions1 = (...fns) => (arg) =>
fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
const pipeFunctions1 = (...fns) =>
fns.reduce((f, g) => (...args) => g(f(...args)));
const pluralize1 = (num, word, plural = word + "s") =>
[1, -1].includes(Number(num)) ? word : plural;
const prefix1 = (prop) => {
const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1);
const prefixes = ["", "webkit", "moz", "ms", "o"];
const i = prefixes.findIndex(
(prefix1) =>
typeof document.body.style[prefix1 ? prefix1 + capitalizedProp : prop] !==
"undefined"
);
return i !== -1 ? (i === 0 ? prop : prefixes[i] + capitalizedProp) : null;
};
const prettyBytes1 = (num, precision = 3, addSpace = " ") => {
const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if (Math.abs(num) < 1) return num + addSpace + UNITS[0];
const exponent = Math.min(
Math.floor(Math.log10(num < 0 ? -num : num) / 3),
UNITS.length - 1
);
const n = Number(
((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision)
);
return (num < 0 ? "-" : "") + n + addSpace + UNITS[exponent];
};
const prettyBytesT1 = (strings, bytes, precision = 3) => {
return prettyBytes1(bytes, precision, strings.join(""));
};
const promisify1 = (func) => (...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) => (err ? reject(err) : resolve(result)))
);
const radsToDegrees1 = (rad) => (rad * 180) / Math.PI;
const randomHexColorCode1 = () => {
let n = (Math.random() * 1048575 * 1000000).toString(16);
return "#" + n.slice(0, 6);
};
const randomInt1 = (min = 0, max = 100) =>
Math.floor(Math.random() * (max - min + 1)) + min;
const randomNumber1 = (min = 0, max = 100) =>
Math.random() * (max - min + 1) + min;
const reduceWhich1 = (arr, comparator = (a, b) => a - b) =>
arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
const removeNonASCII1 = (str) => str.replace(/[^\x20-\x7E]/g, "");
const renderElement1 = ({ type, props = {} }, container) => {
const isTextElement = !type;
const element = isTextElement
? document.createTextNode("")
: document.createElement(type);
const isListener = (p) => p.startsWith("on");
const isAttribute = (p) => !isListener(p) && p !== "children";
Object.keys(props).forEach((p) => {
if (isAttribute(p)) element[p] = props[p];
if (!isTextElement && isListener(p))
element.addEventListener(p.toLowerCase().slice(2), props[p]);
});
if (!isTextElement && props.children && props.children.length)
props.children.forEach((childElement) =>
renderElement1(childElement, element)
);
container.appendChild(element);
};
const reverseString1 = (str) => {
let s = "";
for (let __char of str) {
s = __char + s;
}
return s;
};
const RGBToHex1 = (r, g, b, hash = "") =>
hash + ((r << 16) + (g << 8) + b).toString(16).padStart(6, "0");
const round1 = (n, decimals = 0) =>
Number(`${Math.round(Number(`${n}e${decimals}`))}e-${decimals}`);
const runPromisesInSeries1 = (ps) =>
ps.reduce((p, next) => p.then(next), Promise.resolve());
const scrollToTop1 = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop1);
window.scrollTo(0, c - c / 8);
}
};
const serializeCookie1 = (name, val) =>
`${encodeURIComponent(name)}=${encodeURIComponent(val)}`;
const show1 = (...el) => [...el].forEach((e) => (e.style.display = ""));
const shuffle1 = ([...arr]) => {
let m = arr.length;
while (m) {
const i = Math.floor(Math.random() * m--);
[arr[m], arr[i]] = [arr[i], arr[m]];
}
return arr;
};
const size1 = (val) =>
Array.isArray(val)
? val.length
: val && typeof val === "object"
? val.size || val.length || Object.keys(val).length
: typeof val === "string"
? new Blob([val]).size
: 0;
const sleep1 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const splitLines1 = (str) => str.split(/\r?\n/);
const spreadOver1 = (fn) => (argsArr) => fn(...argsArr);
const stableSort1 = (arr, compare) =>
arr
.map((item, index) => ({
item,
index,
}))
.sort((a, b) => compare(a.item, b.item) || a.index - b.index)
.map(({ item }) => item);
const stripHTMLTags1 = (str) => str.replace(/<[^>]*>/g, "");
const sum1 = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
const sumBy1 = (arr, fn) => {
return arr
.map(typeof fn === "function" ? fn : (val) => val[fn])
.reduce((acc, val) => acc + val, 0);
};
const tail1 = (arr) => (arr.length > 1 ? arr.slice(1) : arr);
const take1 = (arr, n = 1) => arr.slice(0, n);
const takeRight1 = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
const takeWhile1 = (arr, func) =>
arr.reduce((acc, el) => (func(el) ? acc : acc.concat(el)), []);
const takeRightWhile1 = (arr, func) =>
arr.reduceRight((acc, el) => (func(el) ? acc : [el].concat(acc)), []);
const throttle1 = (fn, wait = 300) => {
let inThrottle, lastFn, lastTime;
return function () {
const context = this,
args = arguments;
if (!inThrottle) {
fn.apply(context, args);
lastTime = Date.now();
inThrottle = true;
} else {
clearTimeout(lastFn);
lastFn = setTimeout(() => {
if (Date.now() - lastTime >= wait) {
fn.apply(context, args);
lastTime = Date.now();
}
}, Math.max(wait - (Date.now() - lastTime), 0));
}
};
};
const times1 = (n, fn, context = undefined) => {
let i = 0;
let result;
do {
result = fn.call(context, i);
} while (result !== false && ++i < n);
return result;
};
const timeTaken1 = (callback) => {
const id = `timeTaken_${Date.now()}`;
console.time(id);
const r = callback();
console.timeEnd(id);
return r;
};
const toCamelCase1 = (str) => {
let s = str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
.join("");
return s && s.slice(0, 1).toLowerCase() + s.slice(1);
};
const toDecimalMark1 = (num) => num.toLocaleString("en-US");
const toKebabCase1 = (str) =>
str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.toLowerCase())
.join("-");
const humanizeUrl1 = (str, preserveUndersore = false, preserveCase = false) => {
const re = preserveUndersore ? /[\W]+/g : /[\W_]+/g;
return preserveCase
? str.replace(re, "-")
: str.replace(re, "-").toLowerCase();
};
const toOrdinalSuffix1 = (num) => {
const __int = parseInt(String(num)),
digits = [__int % 10, __int % 100],
ordinals = ["st", "nd", "rd", "th"],
oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1])
? __int + ordinals[digits[0] - 1]
: __int + ordinals[3];
};
const toPairs1 = (obj) =>
!obj
? []
: isArrayLike1(obj)
? Array.from(obj.entries())
: Object.entries(obj);
const toSnakeCase1 = (str) =>
str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.toLowerCase())
.join("_");
const toTitleCase1 = (str) =>
str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.charAt(0).toUpperCase() + x.slice(1))
.join(" ");
const transform1 = (obj, fn, acc = {}) =>
Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
const triggerEvent1 = (el, eventType, detail) =>
el.dispatchEvent &&
el.dispatchEvent(
new CustomEvent(eventType, {
detail,
})
);
const truncateString1 = (str, num = str.length, ellipsisStr = "...") =>
str.length >= num
? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) +
ellipsisStr
: str;
const ellipsis1 = (str, num = str.length, ellipsisStr = "...") =>
str.length >= num
? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) +
ellipsisStr
: str;
const unary1 = (fn) => (val) => fn(val);
const uncurry1 = (fn, n = 1) => (...args) => {
const next = (acc) => (args1) => args1.reduce((x, y) => x(y), acc);
if (n > args.length) throw new RangeError("Arguments too few!");
return next(fn)(args.slice(0, n));
};
const union1 = (a, b) => Array.from(new Set([...a, ...b]));
const unionBy1 = (a, b, fn) => {
const s = new Set(a.map(fn));
return Array.from(new Set([...a, ...b.filter((x) => !s.has(fn(x)))]));
};
const unionWith1 = (a, b, comp) =>
Array.from(
new Set([...a, ...b.filter((x) => a.findIndex((y) => comp(x, y)) === -1)])
);
const unique1 = (arr) => [...new Set(arr)];
const uniqueBy1 = (arr, fn) =>
arr.reduce((acc, v) => {
if (!acc.some((x) => fn(v, x))) acc.push(v);
return acc;
}, []);
const uniqueByRight1 = (arr, fn) =>
arr.reduceRight((acc, v) => {
if (!acc.some((x) => fn(v, x))) acc.push(v);
return acc;
}, []);
const unzip1 = (arr, size1 = 0) => {
return arr.reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({
length: size1 || Math.max(...arr.map((x) => x.length)),