@h3ravel/support 0.13.0 → 0.14.1
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 +946 -164
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +516 -65
- package/dist/index.d.ts +516 -65
- package/dist/index.js +937 -141
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -12,132 +12,27 @@ import relativeTime from "dayjs/plugin/relativeTime.js";
|
|
|
12
12
|
import timezone from "dayjs/plugin/timezone.js";
|
|
13
13
|
import utc from "dayjs/plugin/utc.js";
|
|
14
14
|
|
|
15
|
-
//#region src/
|
|
16
|
-
var Arr_exports = /* @__PURE__ */ __export({
|
|
17
|
-
alternate: () => alternate,
|
|
18
|
-
chunk: () => chunk,
|
|
19
|
-
collapse: () => collapse,
|
|
20
|
-
combine: () => combine,
|
|
21
|
-
find: () => find,
|
|
22
|
-
first: () => first,
|
|
23
|
-
flatten: () => flatten,
|
|
24
|
-
forget: () => forget,
|
|
25
|
-
isEmpty: () => isEmpty,
|
|
26
|
-
isNotEmpty: () => isNotEmpty,
|
|
27
|
-
last: () => last,
|
|
28
|
-
pop: () => pop,
|
|
29
|
-
prepend: () => prepend,
|
|
30
|
-
range: () => range,
|
|
31
|
-
reverse: () => reverse,
|
|
32
|
-
shift: () => shift,
|
|
33
|
-
take: () => take,
|
|
34
|
-
wrap: () => wrap
|
|
35
|
-
});
|
|
15
|
+
//#region src/Exceptions/InvalidArgumentException.ts
|
|
36
16
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @template T - Type of elements in the array
|
|
40
|
-
* @param arr - The input array
|
|
41
|
-
* @param size - Size of each chunk (default: 2)
|
|
42
|
-
* @returns An array of chunks (arrays)
|
|
17
|
+
* Custom error for invalid type coercion
|
|
43
18
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return chunks;
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Collapse an array of arrays into a single array.
|
|
52
|
-
*/
|
|
53
|
-
const collapse = (arr) => {
|
|
54
|
-
const result = [];
|
|
55
|
-
for (const item of arr) if (Array.isArray(item)) result.push(...item);
|
|
56
|
-
else result.push(item);
|
|
57
|
-
return result;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Alternates between two arrays, creating a zipped result.
|
|
61
|
-
*/
|
|
62
|
-
const alternate = (a, b) => {
|
|
63
|
-
const result = [];
|
|
64
|
-
const max = Math.max(a.length, b.length);
|
|
65
|
-
for (let i = 0; i < max; i++) {
|
|
66
|
-
if (i < a.length) result.push(a[i]);
|
|
67
|
-
if (i < b.length) result.push(b[i]);
|
|
19
|
+
var InvalidArgumentException = class extends Error {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "InvalidArgumentException";
|
|
68
23
|
}
|
|
69
|
-
return result;
|
|
70
|
-
};
|
|
71
|
-
/**
|
|
72
|
-
* Combine arrays and sum their values element by element.
|
|
73
|
-
*/
|
|
74
|
-
const combine = (...arr) => {
|
|
75
|
-
const maxLength = Math.max(...arr.map((a) => a.length));
|
|
76
|
-
const result = new Array(maxLength).fill(0);
|
|
77
|
-
for (let i = 0; i < maxLength; i++) for (const array of arr) result[i] += array[i] || 0;
|
|
78
|
-
return result;
|
|
79
|
-
};
|
|
80
|
-
/** Find the value associated with a given key. */
|
|
81
|
-
const find = (key, arr) => arr.find((item) => item === key) || null;
|
|
82
|
-
/** Returns a new array without the given indices. */
|
|
83
|
-
const forget = (arr, keys) => arr.filter((_, i) => !keys.includes(i));
|
|
84
|
-
/** Remove the first element and return tuple [el, rest]. */
|
|
85
|
-
const first = (arr) => {
|
|
86
|
-
if (!arr.length) throw new Error("Cannot shift from empty array");
|
|
87
|
-
return [arr[0], arr.slice(1)];
|
|
88
|
-
};
|
|
89
|
-
/** Remove the last element and return tuple [el, rest]. */
|
|
90
|
-
const last = (arr) => {
|
|
91
|
-
if (!arr.length) throw new Error("Cannot pop from empty array");
|
|
92
|
-
return [arr[arr.length - 1], arr.slice(0, -1)];
|
|
93
|
-
};
|
|
94
|
-
/** Check if array is empty. */
|
|
95
|
-
const isEmpty = (arr) => {
|
|
96
|
-
if (arr.length === 0) return true;
|
|
97
|
-
else return false;
|
|
98
|
-
};
|
|
99
|
-
/** Check if array is empty. */
|
|
100
|
-
const isNotEmpty = (arr) => arr.length > 0;
|
|
101
|
-
/** Pop the element off the end of array. */
|
|
102
|
-
const pop = (arr) => arr.slice(0, -1);
|
|
103
|
-
/** Add elements to the beginning of array. */
|
|
104
|
-
const prepend = (arr, ...elements) => [...elements, ...arr];
|
|
105
|
-
/** Take first n elements of array. */
|
|
106
|
-
const take = (amount, arr) => arr.slice(0, Math.max(0, amount));
|
|
107
|
-
/** Create a new array in reverse order. */
|
|
108
|
-
const reverse = (arr) => [...arr].reverse();
|
|
109
|
-
/** Alias for first element removal. */
|
|
110
|
-
const shift = first;
|
|
111
|
-
/**
|
|
112
|
-
* Generates an array of sequential numbers.
|
|
113
|
-
*
|
|
114
|
-
* @param size - Number of elements in the range
|
|
115
|
-
* @param startAt - Starting number (default: 0)
|
|
116
|
-
* @returns An array of numbers from startAt to startAt + size - 1
|
|
117
|
-
*/
|
|
118
|
-
const range = (size, startAt = 0) => {
|
|
119
|
-
if (size <= 0 || !Number.isFinite(size)) return [];
|
|
120
|
-
return Array.from({ length: size }, (_, i) => startAt + i);
|
|
121
|
-
};
|
|
122
|
-
/** Flatten multi-dimensional arrays into single level. */
|
|
123
|
-
const flatten = (arr) => {
|
|
124
|
-
const result = [];
|
|
125
|
-
const recurse = (input) => {
|
|
126
|
-
for (const item of input) if (Array.isArray(item)) recurse(item);
|
|
127
|
-
else result.push(item);
|
|
128
|
-
};
|
|
129
|
-
recurse(arr);
|
|
130
|
-
return result;
|
|
131
24
|
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/Exceptions/RuntimeException.ts
|
|
132
28
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* @param {Array} value
|
|
136
|
-
* @return array
|
|
29
|
+
* Custom error for invalid type coercion
|
|
137
30
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
31
|
+
var RuntimeException = class extends Error {
|
|
32
|
+
constructor(message) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "RuntimeException";
|
|
35
|
+
}
|
|
141
36
|
};
|
|
142
37
|
|
|
143
38
|
//#endregion
|
|
@@ -304,10 +199,10 @@ const verifyChecksum = (data, expectedChecksum, algorithm = "sha256") => {
|
|
|
304
199
|
* @param shift - Number of positions to shift (default: 13)
|
|
305
200
|
* @returns Encrypted/decrypted text
|
|
306
201
|
*/
|
|
307
|
-
const caesarCipher = (text, shift
|
|
202
|
+
const caesarCipher = (text, shift = 13) => {
|
|
308
203
|
return text.replace(/[a-zA-Z]/g, (char) => {
|
|
309
204
|
const baseCharCode = char === char.toUpperCase() ? "A" : "a";
|
|
310
|
-
const shiftedCharCode = (char.charCodeAt(0) - baseCharCode.charCodeAt(0) + shift
|
|
205
|
+
const shiftedCharCode = (char.charCodeAt(0) - baseCharCode.charCodeAt(0) + shift) % 26;
|
|
311
206
|
const newCharCode = (shiftedCharCode < 0 ? 26 + shiftedCharCode : shiftedCharCode) + baseCharCode.charCodeAt(0);
|
|
312
207
|
return String.fromCharCode(newCharCode);
|
|
313
208
|
});
|
|
@@ -519,22 +414,27 @@ const toHumanTime = (seconds = 0, worded = false) => {
|
|
|
519
414
|
if (secs || !hours && !minutes) parts.push(`${secs}sec`);
|
|
520
415
|
return parts.join(" ");
|
|
521
416
|
}
|
|
522
|
-
|
|
523
|
-
const mm = (hours > 0 && minutes < 10 ? `0${minutes}` : minutes) + ":";
|
|
524
|
-
const ss = secs < 10 ? `0${secs}` : secs;
|
|
525
|
-
return `${hh}${mm}${ss}`;
|
|
417
|
+
return `${hours > 0 ? `${hours}:` : ""}${(hours > 0 && minutes < 10 ? `0${minutes}` : minutes) + ":"}${secs < 10 ? `0${secs}` : secs}`;
|
|
526
418
|
};
|
|
527
419
|
|
|
528
420
|
//#endregion
|
|
529
421
|
//#region src/Helpers/Obj.ts
|
|
530
422
|
var Obj_exports = /* @__PURE__ */ __export({
|
|
423
|
+
Obj: () => Obj,
|
|
424
|
+
data_fill: () => data_fill,
|
|
425
|
+
data_forget: () => data_forget,
|
|
426
|
+
data_get: () => data_get,
|
|
427
|
+
data_set: () => data_set,
|
|
531
428
|
dot: () => dot,
|
|
532
429
|
extractProperties: () => extractProperties,
|
|
533
430
|
getValue: () => getValue,
|
|
534
431
|
modObj: () => modObj,
|
|
535
432
|
safeDot: () => safeDot,
|
|
536
433
|
setNested: () => setNested,
|
|
537
|
-
slugifyKeys: () => slugifyKeys
|
|
434
|
+
slugifyKeys: () => slugifyKeys,
|
|
435
|
+
toCssClasses: () => toCssClasses,
|
|
436
|
+
toCssStyles: () => toCssStyles,
|
|
437
|
+
undot: () => undot
|
|
538
438
|
});
|
|
539
439
|
/**
|
|
540
440
|
* Flattens a nested object into a single-level object
|
|
@@ -677,6 +577,893 @@ const slugifyKeys = (obj, only = [], separator = "_") => {
|
|
|
677
577
|
if (only.length) entries = entries.filter(([key]) => only.includes(key));
|
|
678
578
|
return Object.fromEntries(entries.map(([key, value]) => [slugify(key), value]));
|
|
679
579
|
};
|
|
580
|
+
/**
|
|
581
|
+
* toCssClasses
|
|
582
|
+
*
|
|
583
|
+
* Convert array/object/string input into a CSS class string.
|
|
584
|
+
* - Arrays: included if truthy
|
|
585
|
+
* - Objects: keys included if value is truthy
|
|
586
|
+
* - Strings: included as-is
|
|
587
|
+
*/
|
|
588
|
+
function toCssClasses(input) {
|
|
589
|
+
if (!input) return "";
|
|
590
|
+
const classes = [];
|
|
591
|
+
if (typeof input === "string") return input.trim();
|
|
592
|
+
if (Array.isArray(input)) input.forEach((item) => {
|
|
593
|
+
if (item) classes.push(String(item).trim());
|
|
594
|
+
});
|
|
595
|
+
else if (typeof input === "object") {
|
|
596
|
+
for (const [key, value] of Object.entries(input)) if (value) classes.push(key);
|
|
597
|
+
}
|
|
598
|
+
return classes.join(" ");
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* toCssStyles
|
|
602
|
+
*
|
|
603
|
+
* Convert object input into CSS style string.
|
|
604
|
+
* - Only includes truthy values (ignores null/undefined/false)
|
|
605
|
+
*/
|
|
606
|
+
function toCssStyles(styles) {
|
|
607
|
+
const parts = [];
|
|
608
|
+
for (const [k, v] of Object.entries(styles)) {
|
|
609
|
+
if (v === null || v === void 0 || v === false) continue;
|
|
610
|
+
parts.push(`${k}:${v}`);
|
|
611
|
+
}
|
|
612
|
+
return parts.join(";");
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* undot
|
|
616
|
+
*
|
|
617
|
+
* Convert a dot-notated object back into nested structure.
|
|
618
|
+
*
|
|
619
|
+
* Example:
|
|
620
|
+
* undot({ 'a.b': 1, 'c.0': 2 }) -> { a: { b: 1 }, c: [2] }
|
|
621
|
+
*/
|
|
622
|
+
function undot(obj) {
|
|
623
|
+
const result = {};
|
|
624
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
625
|
+
const parts = key.split(".");
|
|
626
|
+
let node = result;
|
|
627
|
+
for (let i = 0; i < parts.length; i++) {
|
|
628
|
+
const part = parts[i];
|
|
629
|
+
const isLast = i === parts.length - 1;
|
|
630
|
+
const nextPart = parts[i + 1];
|
|
631
|
+
const isArrayIndex = !isNaN(Number(nextPart));
|
|
632
|
+
if (isLast) node[part] = value;
|
|
633
|
+
else {
|
|
634
|
+
if (!(part in node)) node[part] = isArrayIndex ? [] : {};
|
|
635
|
+
node = node[part];
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return result;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* data_get
|
|
643
|
+
*
|
|
644
|
+
* Get a value from an object using dot notation.
|
|
645
|
+
*/
|
|
646
|
+
function data_get(obj, path, defaultValue) {
|
|
647
|
+
if (!obj) return defaultValue;
|
|
648
|
+
const parts = Array.isArray(path) ? path : path.split(".");
|
|
649
|
+
let current = obj;
|
|
650
|
+
for (const part of parts) {
|
|
651
|
+
if (!current || !(part in current)) return defaultValue;
|
|
652
|
+
current = current[part];
|
|
653
|
+
}
|
|
654
|
+
return current;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* data_set
|
|
658
|
+
*
|
|
659
|
+
* Set a value in an object using dot notation. Mutates the object.
|
|
660
|
+
*/
|
|
661
|
+
function data_set(obj, path, value) {
|
|
662
|
+
const parts = Array.isArray(path) ? path : path.split(".");
|
|
663
|
+
let current = obj;
|
|
664
|
+
for (let i = 0; i < parts.length; i++) {
|
|
665
|
+
const part = parts[i];
|
|
666
|
+
if (i === parts.length - 1) current[part] = value;
|
|
667
|
+
else {
|
|
668
|
+
if (!(part in current) || typeof current[part] !== "object" || current[part] === null) current[part] = {};
|
|
669
|
+
current = current[part];
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* data_fill
|
|
675
|
+
*
|
|
676
|
+
* Like data_set, but only sets the value if the key does NOT exist.
|
|
677
|
+
*/
|
|
678
|
+
function data_fill(obj, path, value) {
|
|
679
|
+
if (data_get(obj, path) === void 0) data_set(obj, path, value);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* data_forget
|
|
683
|
+
*
|
|
684
|
+
* Remove a key from an object using dot notation.
|
|
685
|
+
*/
|
|
686
|
+
function data_forget(obj, path) {
|
|
687
|
+
const parts = Array.isArray(path) ? path : path.split(".");
|
|
688
|
+
let current = obj;
|
|
689
|
+
for (let i = 0; i < parts.length; i++) {
|
|
690
|
+
const part = parts[i];
|
|
691
|
+
if (i === parts.length - 1) delete current[part];
|
|
692
|
+
else {
|
|
693
|
+
if (!current[part] || typeof current[part] !== "object") break;
|
|
694
|
+
current = current[part];
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
var Obj = class Obj {
|
|
699
|
+
/**
|
|
700
|
+
* Check if the value is a non-null object (associative/accessible).
|
|
701
|
+
*/
|
|
702
|
+
static accessible(value) {
|
|
703
|
+
return value !== null && typeof value === "object";
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Add a key-value pair to an object only if the key does not already exist.
|
|
707
|
+
*
|
|
708
|
+
* Returns a new object (does not mutate original).
|
|
709
|
+
*/
|
|
710
|
+
static add(obj, key, value) {
|
|
711
|
+
if (!(key in obj)) return {
|
|
712
|
+
...obj,
|
|
713
|
+
[key]: value
|
|
714
|
+
};
|
|
715
|
+
return obj;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Split object into [keys, values]
|
|
719
|
+
*/
|
|
720
|
+
static divide(obj) {
|
|
721
|
+
return [Object.keys(obj), Object.values(obj)];
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Check if a key exists in the object.
|
|
725
|
+
*/
|
|
726
|
+
static exists(obj, key) {
|
|
727
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Get a value from an object using dot notation.
|
|
731
|
+
*
|
|
732
|
+
* Example:
|
|
733
|
+
* Obj.get({a:{b:1}}, 'a.b') -> 1
|
|
734
|
+
*/
|
|
735
|
+
static get(obj, path, defaultValue) {
|
|
736
|
+
if (!Obj.accessible(obj)) return defaultValue;
|
|
737
|
+
const parts = Array.isArray(path) ? path : path.split(".");
|
|
738
|
+
let current = obj;
|
|
739
|
+
for (const part of parts) {
|
|
740
|
+
if (!Obj.accessible(current) || !(part in current)) return defaultValue;
|
|
741
|
+
current = current[part];
|
|
742
|
+
}
|
|
743
|
+
return current;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Check if the object has a given key or keys (dot notation supported).
|
|
747
|
+
*/
|
|
748
|
+
static has(obj, keys) {
|
|
749
|
+
if (!Obj.accessible(obj)) return false;
|
|
750
|
+
return (Array.isArray(keys) ? keys : [keys]).every((key) => {
|
|
751
|
+
const parts = key.split(".");
|
|
752
|
+
let current = obj;
|
|
753
|
+
for (const part of parts) {
|
|
754
|
+
if (!Obj.accessible(current) || !(part in current)) return false;
|
|
755
|
+
current = current[part];
|
|
756
|
+
}
|
|
757
|
+
return true;
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Check if an object is associative (has at least one non-numeric key).
|
|
762
|
+
*/
|
|
763
|
+
static isAssoc(obj) {
|
|
764
|
+
if (!Obj.accessible(obj)) return false;
|
|
765
|
+
return Object.keys(obj).some((k) => isNaN(Number(k)));
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Add a prefix to all keys of the object.
|
|
769
|
+
*/
|
|
770
|
+
static prependKeysWith(obj, prefix) {
|
|
771
|
+
if (!Obj.accessible(obj)) return {};
|
|
772
|
+
const result = {};
|
|
773
|
+
for (const [k, v] of Object.entries(obj)) result[`${prefix}${k}`] = v;
|
|
774
|
+
return result;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Convert an object into a URL query string.
|
|
778
|
+
*
|
|
779
|
+
* Nested objects/arrays are flattened using bracket notation.
|
|
780
|
+
*/
|
|
781
|
+
static query(obj) {
|
|
782
|
+
const encode = encodeURIComponent;
|
|
783
|
+
const parts = [];
|
|
784
|
+
function build(key, value) {
|
|
785
|
+
if (Array.isArray(value)) value.forEach((v, i) => build(`${key}[${i}]`, v));
|
|
786
|
+
else if (Obj.accessible(value)) Object.entries(value).forEach(([k, v]) => build(`${key}[${k}]`, v));
|
|
787
|
+
else parts.push(`${encode(key)}=${encode(value)}`);
|
|
788
|
+
}
|
|
789
|
+
Object.entries(obj).forEach(([k, v]) => build(k, v));
|
|
790
|
+
return parts.join("&");
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
//#endregion
|
|
795
|
+
//#region src/Helpers/Arr.ts
|
|
796
|
+
/**
|
|
797
|
+
* Arr — Laravel-like array helpers for JavaScript.
|
|
798
|
+
*
|
|
799
|
+
* - Methods aim for clear, predictable JS behavior.
|
|
800
|
+
* - Inputs are validated where useful; functions try not to mutate arguments.
|
|
801
|
+
*/
|
|
802
|
+
var Arr = class Arr {
|
|
803
|
+
/**
|
|
804
|
+
* Helper: is a value an object (but not null).
|
|
805
|
+
*/
|
|
806
|
+
static _isObject(value) {
|
|
807
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Helper: deep clone for safety (simple).
|
|
811
|
+
* Uses JSON methods — good for typical data shapes (no functions, Dates, Maps).
|
|
812
|
+
*/
|
|
813
|
+
static _clone(value) {
|
|
814
|
+
try {
|
|
815
|
+
return JSON.parse(JSON.stringify(value));
|
|
816
|
+
} catch {
|
|
817
|
+
return value;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Retrieve a value using dot notation
|
|
822
|
+
* Throws if value is not an array
|
|
823
|
+
*/
|
|
824
|
+
static array(obj, path, defaultValue) {
|
|
825
|
+
const val = data_get(obj, path, defaultValue);
|
|
826
|
+
if (!Array.isArray(val)) throw new InvalidArgumentException(`Value at "${path}" is not an array.`);
|
|
827
|
+
return val;
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Retrieve a value using dot notation
|
|
831
|
+
* Throws if value is not a boolean
|
|
832
|
+
*/
|
|
833
|
+
static boolean(obj, path, defaultValue) {
|
|
834
|
+
const val = data_get(obj, path, defaultValue);
|
|
835
|
+
if (typeof val !== "boolean") throw new InvalidArgumentException(`Value at "${path}" is not a boolean.`);
|
|
836
|
+
return val;
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Flatten an array of arrays by one level.
|
|
840
|
+
*
|
|
841
|
+
* Example:
|
|
842
|
+
* Arr.collapse([[1,2], [3], 4]) -> [1,2,3,4]
|
|
843
|
+
*/
|
|
844
|
+
static collapse(array) {
|
|
845
|
+
if (!Array.isArray(array)) return [];
|
|
846
|
+
const result = [];
|
|
847
|
+
for (const item of array) if (Array.isArray(item)) result.push(...item);
|
|
848
|
+
else result.push(item);
|
|
849
|
+
return result;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Cartesian product of arrays.
|
|
853
|
+
*
|
|
854
|
+
* Example:
|
|
855
|
+
* Arr.crossJoin([1,2], ['a','b']) -> [[1,'a'], [1,'b'], [2,'a'], [2,'b']]
|
|
856
|
+
*
|
|
857
|
+
* Accepts any number of array arguments (or single array-of-arrays).
|
|
858
|
+
*/
|
|
859
|
+
static crossJoin(...arrays) {
|
|
860
|
+
let inputs = arrays;
|
|
861
|
+
if (arrays.length === 1 && Array.isArray(arrays[0]) && arrays[0].some(Array.isArray)) inputs = arrays[0];
|
|
862
|
+
inputs = inputs.map((a) => Array.isArray(a) ? a : [a]);
|
|
863
|
+
let product = [[]];
|
|
864
|
+
for (const arr of inputs) {
|
|
865
|
+
const next = [];
|
|
866
|
+
for (const prefix of product) for (const value of arr) next.push([...prefix, value]);
|
|
867
|
+
product = next;
|
|
868
|
+
}
|
|
869
|
+
return product;
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Split an array (or object) into two arrays: [keys, values].
|
|
873
|
+
*
|
|
874
|
+
* For arrays, keys are numeric indices. For objects, keys are property names.
|
|
875
|
+
*
|
|
876
|
+
* Example:
|
|
877
|
+
* Arr.divide(['a','b']) -> [[0,1], ['a','b']]
|
|
878
|
+
* Arr.divide({x:1,y:2}) -> [['x','y'], [1,2]]
|
|
879
|
+
*/
|
|
880
|
+
static divide(input) {
|
|
881
|
+
if (Array.isArray(input)) return [input.map((_, i) => i), input.slice()];
|
|
882
|
+
if (Arr._isObject(input)) {
|
|
883
|
+
const keys = Object.keys(input);
|
|
884
|
+
return [keys, keys.map((k) => input[k])];
|
|
885
|
+
}
|
|
886
|
+
return [[], []];
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Flatten a nested array/object structure into a single-level object
|
|
890
|
+
* with dot-notated keys.
|
|
891
|
+
*
|
|
892
|
+
* Example:
|
|
893
|
+
* Arr.dot({ a: { b: 1 }, c: [2,3] }) -> { 'a.b': 1, 'c.0': 2, 'c.1': 3 }
|
|
894
|
+
*
|
|
895
|
+
* Works for arrays and plain objects.
|
|
896
|
+
*/
|
|
897
|
+
static dot(input, prefix = "") {
|
|
898
|
+
const result = {};
|
|
899
|
+
const recurse = (val, path) => {
|
|
900
|
+
if (Array.isArray(val)) for (let i = 0; i < val.length; i++) recurse(val[i], path ? `${path}.${i}` : String(i));
|
|
901
|
+
else if (Arr._isObject(val)) for (const [k, v] of Object.entries(val)) recurse(v, path ? `${path}.${k}` : k);
|
|
902
|
+
else result[path] = val;
|
|
903
|
+
};
|
|
904
|
+
if (Array.isArray(input) || Arr._isObject(input)) recurse(input, prefix);
|
|
905
|
+
return result;
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Checks if all elements satisfy the predicate
|
|
909
|
+
*/
|
|
910
|
+
static every(array, predicate) {
|
|
911
|
+
return array.every(predicate);
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Remove items by keys/indices from an array or properties from an object.
|
|
915
|
+
*
|
|
916
|
+
* For arrays: keys are numeric indices (single number or array of numbers).
|
|
917
|
+
*
|
|
918
|
+
* Returns a shallow-copied result (does not mutate input).
|
|
919
|
+
*
|
|
920
|
+
* Example:
|
|
921
|
+
* Arr.except([10,20,30], [1]) -> [10,30]
|
|
922
|
+
*/
|
|
923
|
+
static except(input, keys) {
|
|
924
|
+
const keySet = new Set(Array.isArray(keys) ? keys : [keys]);
|
|
925
|
+
if (Array.isArray(input)) return input.filter((_, idx) => !keySet.has(idx));
|
|
926
|
+
if (Arr._isObject(input)) {
|
|
927
|
+
const out = {};
|
|
928
|
+
for (const [k, v] of Object.entries(input)) if (!keySet.has(k)) out[k] = v;
|
|
929
|
+
return out;
|
|
930
|
+
}
|
|
931
|
+
return input;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Return the first element of an array that satisfies the predicate,
|
|
935
|
+
* or the first element if no predicate is provided, otherwise the defaultValue.
|
|
936
|
+
*
|
|
937
|
+
* Predicate can be true (boolean), a function or a value to match (strict equality).
|
|
938
|
+
*
|
|
939
|
+
* When predicate is true (boolean), the first element will be removed and a tuple will be returned [el, rest].
|
|
940
|
+
*
|
|
941
|
+
* @param array
|
|
942
|
+
* @param predicate
|
|
943
|
+
* @param defaultValue
|
|
944
|
+
*
|
|
945
|
+
* @returns
|
|
946
|
+
*/
|
|
947
|
+
static first(array, predicate, defaultValue) {
|
|
948
|
+
if (predicate === true) {
|
|
949
|
+
if (!array.length) throw new Error("Cannot shift from empty array");
|
|
950
|
+
return [array[0], array.slice(1)];
|
|
951
|
+
}
|
|
952
|
+
if (!Array.isArray(array) || array.length === 0) return defaultValue;
|
|
953
|
+
if (predicate === void 0) return array[0];
|
|
954
|
+
if (typeof predicate === "function") {
|
|
955
|
+
for (const item of array) if (predicate(item)) return item;
|
|
956
|
+
return defaultValue;
|
|
957
|
+
}
|
|
958
|
+
for (const item of array) if (typeof predicate !== "boolean" && item === predicate) return item;
|
|
959
|
+
return defaultValue;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Recursively flatten an array up to `depth` levels (default: Infinity).
|
|
963
|
+
*
|
|
964
|
+
* Example:
|
|
965
|
+
* Arr.flatten([1, [2, [3]]], 1) -> [1,2,[3]]
|
|
966
|
+
*/
|
|
967
|
+
static flatten(array, depth = Infinity) {
|
|
968
|
+
if (!Array.isArray(array)) return [];
|
|
969
|
+
if (depth < 1) return array.slice();
|
|
970
|
+
const result = [];
|
|
971
|
+
for (const item of array) if (Array.isArray(item) && depth > 0) result.push(...Arr.flatten(item, depth - 1));
|
|
972
|
+
else result.push(item);
|
|
973
|
+
return result;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Retrieve a value from an array/object using dot notation
|
|
977
|
+
* Throws if value is not a float
|
|
978
|
+
*/
|
|
979
|
+
static float(obj, path, defaultValue) {
|
|
980
|
+
const val = data_get(obj, path, defaultValue);
|
|
981
|
+
if (typeof val !== "number" || Number.isNaN(val)) throw new InvalidArgumentException(`Value at "${path}" is not a float.`);
|
|
982
|
+
return val;
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Remove element(s) by index or dot-notated path from an array/object.
|
|
986
|
+
*
|
|
987
|
+
* For arrays: accepts numeric index or array of indices. Returns a new array.
|
|
988
|
+
*
|
|
989
|
+
* For objects: supports dot notation to remove nested keys.
|
|
990
|
+
*
|
|
991
|
+
* Example:
|
|
992
|
+
* Arr.forget([1,2,3], 1) -> [1,3]
|
|
993
|
+
* Arr.forget({a:{b:1}}, 'a.b') -> { a: {} }
|
|
994
|
+
*/
|
|
995
|
+
static forget(input, keys) {
|
|
996
|
+
if (Array.isArray(input)) {
|
|
997
|
+
const removeSet = new Set(Array.isArray(keys) ? keys : [keys]);
|
|
998
|
+
return input.filter((_, i) => !removeSet.has(i));
|
|
999
|
+
}
|
|
1000
|
+
if (Arr._isObject(input)) {
|
|
1001
|
+
const out = Arr._clone(input);
|
|
1002
|
+
const keyList = Array.isArray(keys) ? keys : [keys];
|
|
1003
|
+
for (const key of keyList) {
|
|
1004
|
+
const parts = String(key).split(".");
|
|
1005
|
+
let node = out;
|
|
1006
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1007
|
+
const part = parts[i];
|
|
1008
|
+
if (i === parts.length - 1) {
|
|
1009
|
+
if (node && Object.prototype.hasOwnProperty.call(node, part)) delete node[part];
|
|
1010
|
+
} else {
|
|
1011
|
+
if (!Arr._isObject(node[part])) break;
|
|
1012
|
+
node = node[part];
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
return out;
|
|
1017
|
+
}
|
|
1018
|
+
return input;
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Converts various input types into a plain array
|
|
1022
|
+
* Supports Arrays, Objects, Iterables, Map, WeakMap, and custom toArray/toJSON/jsonSerialize methods
|
|
1023
|
+
*/
|
|
1024
|
+
static from(value) {
|
|
1025
|
+
if (value == null) return [];
|
|
1026
|
+
if (Array.isArray(value)) return value;
|
|
1027
|
+
if (Symbol.iterator in Object(value)) return [...value];
|
|
1028
|
+
if (value.toArray) return value.toArray();
|
|
1029
|
+
if (value.toJSON) return value.toJSON();
|
|
1030
|
+
if (value.jsonSerialize) return value.jsonSerialize();
|
|
1031
|
+
if (value instanceof Map) return Array.from(value.entries());
|
|
1032
|
+
if (value instanceof WeakMap) return [];
|
|
1033
|
+
if (typeof value === "object") return Object.values(value);
|
|
1034
|
+
return [value];
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Checks if an object has all the specified keys
|
|
1038
|
+
*/
|
|
1039
|
+
static hasAll(obj, keys) {
|
|
1040
|
+
return keys.every((k) => k in obj);
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* For arrays: check if the array contains any of the provided values.
|
|
1044
|
+
*
|
|
1045
|
+
* values can be single value or array of values.
|
|
1046
|
+
*
|
|
1047
|
+
* Example:
|
|
1048
|
+
* Arr.hasAny([1,2,3], [4,2]) -> true
|
|
1049
|
+
*/
|
|
1050
|
+
static hasAny(array, values) {
|
|
1051
|
+
if (!Array.isArray(array)) return false;
|
|
1052
|
+
const vs = Array.isArray(values) ? values : [values];
|
|
1053
|
+
const set = new Set(array);
|
|
1054
|
+
for (const v of vs) if (set.has(v)) return true;
|
|
1055
|
+
return false;
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Retrieve a value using dot notation
|
|
1059
|
+
* Throws if value is not an integer
|
|
1060
|
+
*/
|
|
1061
|
+
static integer(obj, path, defaultValue) {
|
|
1062
|
+
const val = data_get(obj, path, defaultValue);
|
|
1063
|
+
if (!Number.isInteger(val)) throw new InvalidArgumentException(`Value at "${path}" is not an integer.`);
|
|
1064
|
+
return val;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Determine if the input is a "list-like" array: an Array with
|
|
1068
|
+
* contiguous numeric indices starting at 0 (no gaps).
|
|
1069
|
+
*
|
|
1070
|
+
* Example:
|
|
1071
|
+
* Arr.isList([1,2,3]) -> true
|
|
1072
|
+
* const a = []; a[2] = 5; Arr.isList(a) -> false
|
|
1073
|
+
*/
|
|
1074
|
+
static isList(value) {
|
|
1075
|
+
if (!Array.isArray(value)) return false;
|
|
1076
|
+
for (let i = 0; i < value.length; i++) if (!(i in value)) return false;
|
|
1077
|
+
return true;
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Join array elements into a string using the given separator.
|
|
1081
|
+
*
|
|
1082
|
+
* Example:
|
|
1083
|
+
* Arr.join([1,2,3], '-') -> '1-2-3'
|
|
1084
|
+
*/
|
|
1085
|
+
static join(array, separator = ",") {
|
|
1086
|
+
return Array.isArray(array) ? array.join(separator) : "";
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Create an object indexed by a key or callback function.
|
|
1090
|
+
*
|
|
1091
|
+
* Example:
|
|
1092
|
+
* Arr.keyBy([{id:1},{id:2}], 'id') -> { '1': {id:1}, '2': {id:2} }
|
|
1093
|
+
*/
|
|
1094
|
+
static keyBy(array, key) {
|
|
1095
|
+
const result = {};
|
|
1096
|
+
if (!Array.isArray(array)) return result;
|
|
1097
|
+
for (const item of array) {
|
|
1098
|
+
let k;
|
|
1099
|
+
if (typeof key === "function") k = key(item);
|
|
1100
|
+
else k = String(item[key]);
|
|
1101
|
+
result[k] = item;
|
|
1102
|
+
}
|
|
1103
|
+
return result;
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Get the last element of an array, optionally matching a predicate,
|
|
1107
|
+
* or the last element if no predicate is provided, otherwise the defaultValue.
|
|
1108
|
+
*
|
|
1109
|
+
* Predicate can be a true (boolean), a function or a value to match (strict equality).
|
|
1110
|
+
*
|
|
1111
|
+
* When predicate is true (boolean), the last element will be removed and a tuple will be returned [el, rest].
|
|
1112
|
+
*
|
|
1113
|
+
* @param array
|
|
1114
|
+
* @param predicate
|
|
1115
|
+
* @param defaultValue
|
|
1116
|
+
*
|
|
1117
|
+
* @returns
|
|
1118
|
+
*/
|
|
1119
|
+
static last(array, predicate, defaultValue) {
|
|
1120
|
+
if (predicate === true) {
|
|
1121
|
+
if (!array.length) throw new Error("Cannot pop from empty array");
|
|
1122
|
+
return [array[array.length - 1], array.slice(0, -1)];
|
|
1123
|
+
}
|
|
1124
|
+
if (!Array.isArray(array) || array.length === 0) return defaultValue;
|
|
1125
|
+
if (!predicate) return array[array.length - 1];
|
|
1126
|
+
if (typeof predicate === "function") {
|
|
1127
|
+
for (let i = array.length - 1; i >= 0; i--) if (predicate(array[i])) return array[i];
|
|
1128
|
+
} else for (let i = array.length - 1; i >= 0; i--) if (array[i] === predicate) return array[i];
|
|
1129
|
+
return defaultValue;
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Transform each element in an array using a callback.
|
|
1133
|
+
*/
|
|
1134
|
+
static map(array, callback) {
|
|
1135
|
+
return Array.isArray(array) ? array.map(callback) : [];
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* Maps a multi-dimensional array with a spread callback
|
|
1139
|
+
*/
|
|
1140
|
+
static mapSpread(array, callback) {
|
|
1141
|
+
return array.map((item) => callback(...Array.isArray(item) ? item : [item]));
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Map each element to a key-value pair.
|
|
1145
|
+
*
|
|
1146
|
+
* Example:
|
|
1147
|
+
* Arr.mapWithKeys([{id:1, name:'A'}], x => [x.id, x.name])
|
|
1148
|
+
* -> { '1': 'A' }
|
|
1149
|
+
*/
|
|
1150
|
+
static mapWithKeys(array, callback) {
|
|
1151
|
+
const result = {};
|
|
1152
|
+
if (!Array.isArray(array)) return result;
|
|
1153
|
+
array.forEach((item, idx) => {
|
|
1154
|
+
const [k, v] = callback(item, idx);
|
|
1155
|
+
result[String(k)] = v;
|
|
1156
|
+
});
|
|
1157
|
+
return result;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Return only elements at the given indices.
|
|
1161
|
+
*
|
|
1162
|
+
* Example:
|
|
1163
|
+
* Arr.only([10,20,30], [0,2]) -> [10,30]
|
|
1164
|
+
*/
|
|
1165
|
+
static only(array, keys) {
|
|
1166
|
+
if (!Array.isArray(array)) return [];
|
|
1167
|
+
const keyArray = Array.isArray(keys) ? keys : [keys];
|
|
1168
|
+
return array.filter((_, idx) => keyArray.includes(idx));
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Split an array into two arrays based on a predicate
|
|
1172
|
+
*/
|
|
1173
|
+
static partition(array, predicate) {
|
|
1174
|
+
const truthy = [];
|
|
1175
|
+
const falsy = [];
|
|
1176
|
+
array.forEach((item) => (predicate(item) ? truthy : falsy).push(item));
|
|
1177
|
+
return [truthy, falsy];
|
|
1178
|
+
}
|
|
1179
|
+
/**
|
|
1180
|
+
* Extract a property from each element in an array of objects.
|
|
1181
|
+
*
|
|
1182
|
+
* Example:
|
|
1183
|
+
* Arr.pluck([{name:'A'},{name:'B'}], 'name') -> ['A','B']
|
|
1184
|
+
*/
|
|
1185
|
+
static pluck(array, key) {
|
|
1186
|
+
if (!Array.isArray(array)) return [];
|
|
1187
|
+
return array.map((item) => item[key]);
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Add elements to the beginning of an array and return a new array.
|
|
1191
|
+
*
|
|
1192
|
+
* @param array
|
|
1193
|
+
* @param value
|
|
1194
|
+
* @returns
|
|
1195
|
+
*/
|
|
1196
|
+
static prepend(array, ...value) {
|
|
1197
|
+
return [...value, ...Array.isArray(array) ? array : []];
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* Remove a value from an array by index and return it.
|
|
1201
|
+
* Returns a tuple: [newArray, removedValue]
|
|
1202
|
+
*/
|
|
1203
|
+
static pull(array, key) {
|
|
1204
|
+
if (!Array.isArray(array) || key < 0 || key >= array.length) return [array, void 0];
|
|
1205
|
+
const copy = array.slice();
|
|
1206
|
+
const [removed] = copy.splice(key, 1);
|
|
1207
|
+
return [copy, removed];
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Append values to an array (mutable)
|
|
1211
|
+
*/
|
|
1212
|
+
static push(array, ...values) {
|
|
1213
|
+
array.push(...values);
|
|
1214
|
+
return array;
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Pick one or more random elements from an array.
|
|
1218
|
+
*/
|
|
1219
|
+
static random(array, count = 1) {
|
|
1220
|
+
if (!Array.isArray(array) || array.length === 0) return void 0;
|
|
1221
|
+
const shuffled = Arr.shuffle(array);
|
|
1222
|
+
if (count === 1) return shuffled[0];
|
|
1223
|
+
return shuffled.slice(0, count);
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Returns array elements that do NOT satisfy the predicate
|
|
1227
|
+
*/
|
|
1228
|
+
static reject(array, predicate) {
|
|
1229
|
+
return array.filter((item) => !predicate(item));
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Pick keys from an array of objects or an object
|
|
1233
|
+
*/
|
|
1234
|
+
static select(obj, keys) {
|
|
1235
|
+
const result = {};
|
|
1236
|
+
keys.forEach((k) => {
|
|
1237
|
+
if (k in obj) result[k] = obj[k];
|
|
1238
|
+
});
|
|
1239
|
+
return result;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Returns the only element that passes a callback, throws if none or multiple
|
|
1243
|
+
*/
|
|
1244
|
+
static sole(array, predicate) {
|
|
1245
|
+
const filtered = array.filter(predicate);
|
|
1246
|
+
if (filtered.length === 0) throw new InvalidArgumentException("No element satisfies the condition.");
|
|
1247
|
+
if (filtered.length > 1) throw new InvalidArgumentException("Multiple elements satisfy the condition.");
|
|
1248
|
+
return filtered[0];
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Checks if at least one element satisfies the predicate
|
|
1252
|
+
*/
|
|
1253
|
+
static some(array, predicate) {
|
|
1254
|
+
return array.some(predicate);
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Randomly shuffle an array and return a new array.
|
|
1258
|
+
*/
|
|
1259
|
+
static shuffle(array) {
|
|
1260
|
+
if (!Array.isArray(array)) return [];
|
|
1261
|
+
const copy = array.slice();
|
|
1262
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
1263
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
1264
|
+
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
1265
|
+
}
|
|
1266
|
+
return copy;
|
|
1267
|
+
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Sort an array ascending using optional comparator.
|
|
1270
|
+
*/
|
|
1271
|
+
static sort(array, comparator) {
|
|
1272
|
+
if (!Array.isArray(array)) return [];
|
|
1273
|
+
return array.slice().sort(comparator);
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Sort an array descending using optional comparator.
|
|
1277
|
+
*/
|
|
1278
|
+
static sortDesc(array, comparator) {
|
|
1279
|
+
return Arr.sort(array, comparator ? (a, b) => comparator(b, a) : void 0);
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Recursively sort arrays inside an array.
|
|
1283
|
+
*/
|
|
1284
|
+
static sortRecursive(array) {
|
|
1285
|
+
if (!Array.isArray(array)) return [];
|
|
1286
|
+
return array.map((item) => Array.isArray(item) ? Arr.sortRecursive(item) : item).sort();
|
|
1287
|
+
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Recursively sort arrays inside an array descending.
|
|
1290
|
+
*/
|
|
1291
|
+
static sortRecursiveDesc(array) {
|
|
1292
|
+
if (!Array.isArray(array)) return [];
|
|
1293
|
+
return array.map((item) => Array.isArray(item) ? Arr.sortRecursiveDesc(item) : item).sort().reverse();
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* Retrieve a value using dot notation
|
|
1297
|
+
* Throws if value is not a string
|
|
1298
|
+
*/
|
|
1299
|
+
static string(obj, path, defaultValue) {
|
|
1300
|
+
const val = data_get(obj, path, defaultValue);
|
|
1301
|
+
if (typeof val !== "string") throw new InvalidArgumentException(`Value at "${path}" is not a string.`);
|
|
1302
|
+
return val;
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Return the first N elements of an array.
|
|
1306
|
+
*
|
|
1307
|
+
* @param array
|
|
1308
|
+
* @param count
|
|
1309
|
+
* @returns
|
|
1310
|
+
*/
|
|
1311
|
+
static take(array, count) {
|
|
1312
|
+
if (!Array.isArray(array)) return [];
|
|
1313
|
+
return array.slice(0, count);
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* Filter an array based on a predicate function or key-value match.
|
|
1317
|
+
*/
|
|
1318
|
+
static where(array, predicate) {
|
|
1319
|
+
if (!Array.isArray(array)) return [];
|
|
1320
|
+
if (typeof predicate === "function") return array.filter(predicate);
|
|
1321
|
+
return array.filter((item) => Object.entries(predicate).every(([k, v]) => item[k] === v));
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Filter an array of objects, keeping elements where the given key is not null/undefined.
|
|
1325
|
+
*/
|
|
1326
|
+
static whereNotNull(array, key) {
|
|
1327
|
+
if (!Array.isArray(array)) return [];
|
|
1328
|
+
return array.filter((item) => item[key] !== null && item[key] !== void 0);
|
|
1329
|
+
}
|
|
1330
|
+
/**
|
|
1331
|
+
* If the given value is not an array and not null, wrap it in one.
|
|
1332
|
+
*
|
|
1333
|
+
* Non-array values become [value]; null/undefined becomes [].
|
|
1334
|
+
*
|
|
1335
|
+
* @param value
|
|
1336
|
+
* @returns
|
|
1337
|
+
*/
|
|
1338
|
+
static wrap(value) {
|
|
1339
|
+
if (value === null || value === void 0) return [];
|
|
1340
|
+
return Array.isArray(value) ? value : [value];
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Return the first element of an array, undefined if empty.
|
|
1344
|
+
*/
|
|
1345
|
+
static head(array) {
|
|
1346
|
+
return Array.isArray(array) && array.length ? array[0] : void 0;
|
|
1347
|
+
}
|
|
1348
|
+
/**
|
|
1349
|
+
* Splits an array into chunks of a specified size.
|
|
1350
|
+
*
|
|
1351
|
+
* @template T - Type of elements in the array
|
|
1352
|
+
* @param arr - The input array
|
|
1353
|
+
* @param size - Size of each chunk (default: 2)
|
|
1354
|
+
* @returns An array of chunks (arrays)
|
|
1355
|
+
*/
|
|
1356
|
+
static chunk = (arr, size = 2) => {
|
|
1357
|
+
if (size <= 0) throw new Error("Chunk size must be greater than 0");
|
|
1358
|
+
const chunks = [];
|
|
1359
|
+
for (let i = 0; i < arr.length; i += size) chunks.push(arr.slice(i, i + size));
|
|
1360
|
+
return chunks;
|
|
1361
|
+
};
|
|
1362
|
+
/**
|
|
1363
|
+
* Alternates between two arrays, creating a zipped result.
|
|
1364
|
+
*
|
|
1365
|
+
* @param a
|
|
1366
|
+
* @param b
|
|
1367
|
+
* @returns
|
|
1368
|
+
*/
|
|
1369
|
+
static alternate(a, b) {
|
|
1370
|
+
const result = [];
|
|
1371
|
+
const max = Math.max(a.length, b.length);
|
|
1372
|
+
for (let i = 0; i < max; i++) {
|
|
1373
|
+
if (i < a.length) result.push(a[i]);
|
|
1374
|
+
if (i < b.length) result.push(b[i]);
|
|
1375
|
+
}
|
|
1376
|
+
return result;
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* Combine arrays and sum their values element by element.
|
|
1380
|
+
*
|
|
1381
|
+
* @param arr
|
|
1382
|
+
* @returns
|
|
1383
|
+
*/
|
|
1384
|
+
static combine(...arr) {
|
|
1385
|
+
const maxLength = Math.max(...arr.map((a) => a.length));
|
|
1386
|
+
const result = new Array(maxLength).fill(0);
|
|
1387
|
+
for (let i = 0; i < maxLength; i++) for (const array of arr) result[i] += array[i] || 0;
|
|
1388
|
+
return result;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Find the value associated with a given key.
|
|
1392
|
+
*
|
|
1393
|
+
* @param key
|
|
1394
|
+
* @param arr
|
|
1395
|
+
* @returns
|
|
1396
|
+
*/
|
|
1397
|
+
static find(key, arr) {
|
|
1398
|
+
return arr.find((item) => item === key) || null;
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Check if array is empty.
|
|
1402
|
+
*
|
|
1403
|
+
* @param arr
|
|
1404
|
+
* @returns
|
|
1405
|
+
*/
|
|
1406
|
+
static isEmpty(arr) {
|
|
1407
|
+
if (arr.length === 0) return true;
|
|
1408
|
+
else return false;
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1411
|
+
* Check if array is empty.
|
|
1412
|
+
*
|
|
1413
|
+
* @param arr
|
|
1414
|
+
* @returns
|
|
1415
|
+
*/
|
|
1416
|
+
static isNotEmpty(arr) {
|
|
1417
|
+
return arr.length > 0;
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Pop the element off the end of array.
|
|
1421
|
+
*
|
|
1422
|
+
* @param arr
|
|
1423
|
+
* @returns
|
|
1424
|
+
*/
|
|
1425
|
+
static pop(arr) {
|
|
1426
|
+
return arr.slice(0, -1);
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Create a new array in reverse order.
|
|
1430
|
+
*
|
|
1431
|
+
* @param arr
|
|
1432
|
+
* @returns
|
|
1433
|
+
*/
|
|
1434
|
+
static reverse(arr) {
|
|
1435
|
+
return [...arr].reverse();
|
|
1436
|
+
}
|
|
1437
|
+
/**
|
|
1438
|
+
* Return the first element of an array that satisfies the predicate,
|
|
1439
|
+
* or the first element if no predicate is provided, otherwise the defaultValue.
|
|
1440
|
+
*
|
|
1441
|
+
* Predicate can be true (boolean), a function or a value to match (strict equality).
|
|
1442
|
+
*
|
|
1443
|
+
* When predicate is true (boolean), the first element will be removed and a tuple will be returned [el, rest].
|
|
1444
|
+
*
|
|
1445
|
+
* @param array
|
|
1446
|
+
* @param predicate
|
|
1447
|
+
* @param defaultValue
|
|
1448
|
+
*
|
|
1449
|
+
* @alias Arr.first()
|
|
1450
|
+
* @returns
|
|
1451
|
+
*/
|
|
1452
|
+
static shift(array, predicate, defaultValue) {
|
|
1453
|
+
return Arr.first(array, predicate, defaultValue);
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Generates an array of sequential numbers.
|
|
1457
|
+
*
|
|
1458
|
+
* @param size - Number of elements in the range
|
|
1459
|
+
* @param startAt - Starting number (default: 0)
|
|
1460
|
+
* @returns An array of numbers from startAt to startAt + size - 1
|
|
1461
|
+
*/
|
|
1462
|
+
static range(size, startAt = 0) {
|
|
1463
|
+
if (size <= 0 || !Number.isFinite(size)) return [];
|
|
1464
|
+
return Array.from({ length: size }, (_, i) => startAt + i);
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
680
1467
|
|
|
681
1468
|
//#endregion
|
|
682
1469
|
//#region src/Helpers/Time.ts
|
|
@@ -5201,10 +5988,10 @@ function matchCase(value, comparison) {
|
|
|
5201
5988
|
*/
|
|
5202
5989
|
function loadHelpers(target = globalThis) {
|
|
5203
5990
|
const globalHelpers = {
|
|
5204
|
-
Arr
|
|
5991
|
+
Arr,
|
|
5205
5992
|
Crypto: Crypto_exports,
|
|
5206
5993
|
Number: Number_exports,
|
|
5207
|
-
Obj
|
|
5994
|
+
Obj,
|
|
5208
5995
|
Str,
|
|
5209
5996
|
DateTime,
|
|
5210
5997
|
DumpDie: DumpDie_exports,
|
|
@@ -5220,24 +6007,26 @@ function loadHelpers(target = globalThis) {
|
|
|
5220
6007
|
plural: Str.plural,
|
|
5221
6008
|
singular: Str.singular,
|
|
5222
6009
|
capitalize: Str.capitalize,
|
|
5223
|
-
collapse,
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
isEmpty,
|
|
5230
|
-
isNotEmpty,
|
|
5231
|
-
prepend,
|
|
5232
|
-
range,
|
|
5233
|
-
flatten,
|
|
6010
|
+
collapse: Arr.collapse,
|
|
6011
|
+
forget: Arr.forget,
|
|
6012
|
+
first: Arr.first,
|
|
6013
|
+
last: Arr.last,
|
|
6014
|
+
prepend: Arr.prepend,
|
|
6015
|
+
flatten: Arr.flatten,
|
|
5234
6016
|
dot,
|
|
6017
|
+
undot,
|
|
5235
6018
|
extractProperties,
|
|
5236
6019
|
getValue,
|
|
5237
6020
|
modObj,
|
|
5238
6021
|
safeDot,
|
|
5239
6022
|
setNested,
|
|
6023
|
+
toCssClasses,
|
|
5240
6024
|
slugifyKeys,
|
|
6025
|
+
toCssStyles,
|
|
6026
|
+
data_get,
|
|
6027
|
+
data_set,
|
|
6028
|
+
data_fill,
|
|
6029
|
+
data_forget,
|
|
5241
6030
|
uuid,
|
|
5242
6031
|
random,
|
|
5243
6032
|
randomSecure,
|
|
@@ -5319,6 +6108,13 @@ function cleanHelpers(target = globalThis) {
|
|
|
5319
6108
|
"safeDot",
|
|
5320
6109
|
"setNested",
|
|
5321
6110
|
"slugifyKeys",
|
|
6111
|
+
"toCssClasses",
|
|
6112
|
+
"undot",
|
|
6113
|
+
"toCssStyles",
|
|
6114
|
+
"data_get",
|
|
6115
|
+
"data_set",
|
|
6116
|
+
"data_fill",
|
|
6117
|
+
"data_forget",
|
|
5322
6118
|
"Crypto",
|
|
5323
6119
|
"uuid",
|
|
5324
6120
|
"random",
|
|
@@ -5356,5 +6152,5 @@ function cleanHelpers(target = globalThis) {
|
|
|
5356
6152
|
}
|
|
5357
6153
|
|
|
5358
6154
|
//#endregion
|
|
5359
|
-
export {
|
|
6155
|
+
export { Arr, Crypto_exports as Crypto, DateTime, DumpDie_exports as DumpDie, HtmlString, InvalidArgumentException, Mode, Number_exports as Number, Obj_exports as Obj, RuntimeException, Str, Stringable, abbreviate, base64Decode, base64Encode, caesarCipher, checksum, cleanHelpers, data_fill, data_forget, data_get, data_set, dd, dot, dump, extractProperties, format, getValue, hash, hmac, humanize, loadHelpers, modObj, random, randomColor, randomPassword, randomSecure, safeDot, secureToken, setNested, slugifyKeys, str, toBytes, toCssClasses, toCssStyles, toHumanTime, undot, uuid, verifyChecksum, xor };
|
|
5360
6156
|
//# sourceMappingURL=index.js.map
|