@stacksjs/arrays 0.53.1 → 0.56.3
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.d.ts +92 -30
- package/dist/index.mjs +154 -28
- package/package.json +10 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,80 +1,83 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { Nullable, Arrayable } from '@stacksjs/types';
|
|
2
|
+
import { Macroable } from 'macroable';
|
|
3
|
+
|
|
4
|
+
declare function average(arr: number[]): number;
|
|
5
|
+
declare function avg(arr: number[]): number;
|
|
6
|
+
declare function median(arr: number[]): number;
|
|
7
|
+
declare function mode(arr: number[]): number;
|
|
8
|
+
declare function sum(array: readonly number[]): number;
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Convert `Arrayable<T>` to `Array<T>`
|
|
10
12
|
*
|
|
11
13
|
* @category Array
|
|
12
14
|
*/
|
|
13
|
-
|
|
15
|
+
declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
|
|
14
16
|
/**
|
|
15
17
|
* Convert `Arrayable<T>` to `Array<T>` and flatten it
|
|
16
18
|
*
|
|
17
19
|
* @category Array
|
|
18
20
|
*/
|
|
19
|
-
|
|
21
|
+
declare function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
|
|
20
22
|
/**
|
|
21
23
|
* Use rest arguments to merge arrays
|
|
22
24
|
*
|
|
23
25
|
* @category Array
|
|
24
26
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
28
|
+
type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
|
|
27
29
|
/**
|
|
28
30
|
* Divide an array into two parts by a filter function
|
|
29
31
|
*
|
|
30
32
|
* @category Array
|
|
31
33
|
* @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
|
|
32
34
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
|
|
36
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
|
|
37
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
|
|
38
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>): [T[], T[], T[], T[], T[]];
|
|
39
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[]];
|
|
40
|
+
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>, f6: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[], T[]];
|
|
39
41
|
/**
|
|
40
42
|
* Unique an Array
|
|
41
43
|
*
|
|
42
44
|
* @category Array
|
|
43
45
|
*/
|
|
44
|
-
|
|
46
|
+
declare function uniq<T>(array: readonly T[]): T[];
|
|
47
|
+
declare function unique<T>(array: readonly T[]): T[];
|
|
45
48
|
/**
|
|
46
49
|
* Unique an Array by a custom equality function
|
|
47
50
|
*
|
|
48
51
|
* @category Array
|
|
49
52
|
*/
|
|
50
|
-
|
|
53
|
+
declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
|
|
51
54
|
/**
|
|
52
55
|
* Get last item
|
|
53
56
|
*
|
|
54
57
|
* @category Array
|
|
55
58
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
declare function last(array: readonly []): undefined;
|
|
60
|
+
declare function last<T>(array: readonly T[]): T;
|
|
58
61
|
/**
|
|
59
62
|
* Remove an item from Array
|
|
60
63
|
*
|
|
61
64
|
* @category Array
|
|
62
65
|
*/
|
|
63
|
-
|
|
66
|
+
declare function remove<T>(array: T[], value: T): boolean;
|
|
64
67
|
/**
|
|
65
68
|
* Get nth item of Array. Negative for backward
|
|
66
69
|
*
|
|
67
70
|
* @category Array
|
|
68
71
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
declare function at(array: readonly [], index: number): undefined;
|
|
73
|
+
declare function at<T>(array: readonly T[], index: number): T;
|
|
71
74
|
/**
|
|
72
75
|
* Generate a range array of numbers. The `stop` is exclusive.
|
|
73
76
|
*
|
|
74
77
|
* @category Array
|
|
75
78
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
declare function range(stop: number): number[];
|
|
80
|
+
declare function range(start: number, stop: number, step?: number): number[];
|
|
78
81
|
/**
|
|
79
82
|
* Move element in an Array
|
|
80
83
|
*
|
|
@@ -83,22 +86,81 @@ export declare function range(start: number, stop: number, step?: number): numbe
|
|
|
83
86
|
* @param from
|
|
84
87
|
* @param to
|
|
85
88
|
*/
|
|
86
|
-
|
|
89
|
+
declare function move<T>(arr: T[], from: number, to: number): T[];
|
|
87
90
|
/**
|
|
88
91
|
* Clamp a number to the index range of an array.
|
|
89
92
|
*
|
|
90
93
|
* @category Array
|
|
91
94
|
*/
|
|
92
|
-
|
|
95
|
+
declare function clampArrayRange(arr: readonly unknown[], n: number): number;
|
|
93
96
|
/**
|
|
94
97
|
* Get random items from an array
|
|
95
98
|
*
|
|
96
99
|
* @category Array
|
|
97
100
|
*/
|
|
98
|
-
|
|
101
|
+
declare function sample<T>(arr: T[], count: number): T[];
|
|
99
102
|
/**
|
|
100
103
|
* Shuffle an array. This function mutates the array.
|
|
101
104
|
*
|
|
102
105
|
* @category Array
|
|
103
106
|
*/
|
|
104
|
-
|
|
107
|
+
declare function shuffle<T>(array: T[]): T[];
|
|
108
|
+
|
|
109
|
+
declare function contains(needle: string, haystack: string[]): boolean;
|
|
110
|
+
declare function containsAll(needles: string[], haystack: string[]): boolean;
|
|
111
|
+
declare function containsAny(needles: string[], haystack: string[]): boolean;
|
|
112
|
+
declare function containsNone(needles: string[], haystack: string[]): boolean;
|
|
113
|
+
declare function containsOnly(needles: string[], haystack: string[]): boolean;
|
|
114
|
+
declare function doesNotContain(needle: string, haystack: string[]): boolean;
|
|
115
|
+
|
|
116
|
+
declare class Arr extends Macroable {
|
|
117
|
+
contains(needle: string, haystack: string[]): boolean;
|
|
118
|
+
containsAll(needles: string[], haystack: string[]): boolean;
|
|
119
|
+
containsAny(needles: string[], haystack: string[]): boolean;
|
|
120
|
+
containsNone(needles: string[], haystack: string[]): boolean;
|
|
121
|
+
containsOnly(needles: string[], haystack: string[]): boolean;
|
|
122
|
+
doesNotContain(needle: string, haystack: string[]): boolean;
|
|
123
|
+
toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
|
|
124
|
+
flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
|
|
125
|
+
mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
126
|
+
partition<T>(array: readonly T[], filter: PartitionFilter<T>): [T[], T[]];
|
|
127
|
+
/**
|
|
128
|
+
* Returns a random item/s from the array
|
|
129
|
+
*/
|
|
130
|
+
random<T>(arr: T[], count?: number): T[];
|
|
131
|
+
/**
|
|
132
|
+
* Returns random item/s from the array
|
|
133
|
+
*/
|
|
134
|
+
sample<T>(arr: T[], count?: number): T[];
|
|
135
|
+
unique<T>(arr: T[]): T[];
|
|
136
|
+
uniqueBy<T>(arr: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
|
|
137
|
+
last<T>(arr: T[]): T | undefined;
|
|
138
|
+
remove<T>(arr: T[], value: T): boolean;
|
|
139
|
+
at(arr: any[], index: number): any;
|
|
140
|
+
range(start: number, end: number, step?: number): number[];
|
|
141
|
+
move<T>(arr: T[], from: number, to: number): T[];
|
|
142
|
+
clampArrayRange(arr: readonly unknown[], n: number): number;
|
|
143
|
+
shuffle<T>(arr: T[]): T[];
|
|
144
|
+
/**
|
|
145
|
+
* Returns the sum of all items in the array
|
|
146
|
+
*/
|
|
147
|
+
sum(arr: number[]): number;
|
|
148
|
+
/**
|
|
149
|
+
* Returns the average of all items in the array
|
|
150
|
+
*/
|
|
151
|
+
average(arr: number[]): number;
|
|
152
|
+
avg(arr: number[]): number;
|
|
153
|
+
/**
|
|
154
|
+
* Returns the median of all items in the array
|
|
155
|
+
*/
|
|
156
|
+
median(arr: number[]): number;
|
|
157
|
+
/**
|
|
158
|
+
* Returns the mode of all items in the array
|
|
159
|
+
*/
|
|
160
|
+
mode(arr: number[]): number;
|
|
161
|
+
static macros: {};
|
|
162
|
+
static getters: {};
|
|
163
|
+
}
|
|
164
|
+
declare const arr: Arr;
|
|
165
|
+
|
|
166
|
+
export { Arr, PartitionFilter, arr, at, average, avg, clampArrayRange, contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain, flatten, last, median, mergeArrayable, mode, move, partition, range, remove, sample, shuffle, sum, toArray, uniq, unique, uniqueBy };
|
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { clamp } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return needles.every((needle) => contains(needle, haystack));
|
|
1
|
+
import { clamp } from '@stacksjs/utils';
|
|
2
|
+
import { Macroable } from 'macroable';
|
|
3
|
+
|
|
4
|
+
function average(arr) {
|
|
5
|
+
return sum(arr) / arr.length;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
return
|
|
7
|
+
function avg(arr) {
|
|
8
|
+
return average(arr);
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
return
|
|
10
|
+
function median(arr) {
|
|
11
|
+
return arr[Math.floor(arr.length / 2)];
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
return
|
|
13
|
+
function mode(arr) {
|
|
14
|
+
return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop();
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
return
|
|
16
|
+
function sum(array) {
|
|
17
|
+
return array.reduce((acc, cur) => acc + cur, 0);
|
|
19
18
|
}
|
|
20
|
-
|
|
19
|
+
|
|
20
|
+
function toArray(array) {
|
|
21
21
|
array = array ?? [];
|
|
22
22
|
return Array.isArray(array) ? array : [array];
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
function flatten(array) {
|
|
25
25
|
return toArray(array).flat(1);
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
function mergeArrayable(...args) {
|
|
28
28
|
return args.flatMap((i) => toArray(i));
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
function partition(array, ...filters) {
|
|
31
31
|
const result = new Array(filters.length + 1).fill(null).map(() => []);
|
|
32
32
|
array.forEach((e, idx, arr) => {
|
|
33
33
|
let i = 0;
|
|
@@ -42,10 +42,13 @@ export function partition(array, ...filters) {
|
|
|
42
42
|
});
|
|
43
43
|
return result;
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
function uniq(array) {
|
|
46
46
|
return Array.from(new Set(array));
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
function unique(array) {
|
|
49
|
+
return uniq(array);
|
|
50
|
+
}
|
|
51
|
+
function uniqueBy(array, equalFn) {
|
|
49
52
|
return array.reduce((acc, cur) => {
|
|
50
53
|
const index = acc.findIndex((item) => equalFn(cur, item));
|
|
51
54
|
if (index === -1)
|
|
@@ -53,10 +56,10 @@ export function uniqueBy(array, equalFn) {
|
|
|
53
56
|
return acc;
|
|
54
57
|
}, []);
|
|
55
58
|
}
|
|
56
|
-
|
|
59
|
+
function last(array) {
|
|
57
60
|
return at(array, -1);
|
|
58
61
|
}
|
|
59
|
-
|
|
62
|
+
function remove(array, value) {
|
|
60
63
|
if (!array)
|
|
61
64
|
return false;
|
|
62
65
|
const index = array.indexOf(value);
|
|
@@ -66,7 +69,7 @@ export function remove(array, value) {
|
|
|
66
69
|
}
|
|
67
70
|
return false;
|
|
68
71
|
}
|
|
69
|
-
|
|
72
|
+
function at(array, index) {
|
|
70
73
|
const len = array.length;
|
|
71
74
|
if (!len)
|
|
72
75
|
return void 0;
|
|
@@ -74,7 +77,7 @@ export function at(array, index) {
|
|
|
74
77
|
index += len;
|
|
75
78
|
return array[index];
|
|
76
79
|
}
|
|
77
|
-
|
|
80
|
+
function range(...args) {
|
|
78
81
|
let start, stop, step;
|
|
79
82
|
if (args.length === 1) {
|
|
80
83
|
start = 0;
|
|
@@ -91,20 +94,143 @@ export function range(...args) {
|
|
|
91
94
|
}
|
|
92
95
|
return arr;
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
function move(arr, from, to) {
|
|
95
98
|
arr.splice(to, 0, arr.splice(from, 1)[0]);
|
|
96
99
|
return arr;
|
|
97
100
|
}
|
|
98
|
-
|
|
101
|
+
function clampArrayRange(arr, n) {
|
|
99
102
|
return clamp(n, 0, arr.length - 1);
|
|
100
103
|
}
|
|
101
|
-
|
|
104
|
+
function sample(arr, count) {
|
|
102
105
|
return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
|
|
103
106
|
}
|
|
104
|
-
|
|
107
|
+
function shuffle(array) {
|
|
105
108
|
for (let i = array.length - 1; i > 0; i--) {
|
|
106
109
|
const j = Math.floor(Math.random() * (i + 1));
|
|
107
110
|
[array[i], array[j]] = [array[j], array[i]];
|
|
108
111
|
}
|
|
109
112
|
return array;
|
|
110
113
|
}
|
|
114
|
+
|
|
115
|
+
function contains(needle, haystack) {
|
|
116
|
+
return haystack.some((hay) => needle.includes(hay));
|
|
117
|
+
}
|
|
118
|
+
function containsAll(needles, haystack) {
|
|
119
|
+
return needles.every((needle) => contains(needle, haystack));
|
|
120
|
+
}
|
|
121
|
+
function containsAny(needles, haystack) {
|
|
122
|
+
return needles.some((needle) => contains(needle, haystack));
|
|
123
|
+
}
|
|
124
|
+
function containsNone(needles, haystack) {
|
|
125
|
+
return !containsAny(needles, haystack);
|
|
126
|
+
}
|
|
127
|
+
function containsOnly(needles, haystack) {
|
|
128
|
+
return containsAll(haystack, needles);
|
|
129
|
+
}
|
|
130
|
+
function doesNotContain(needle, haystack) {
|
|
131
|
+
return !contains(needle, haystack);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
class Arr extends Macroable {
|
|
135
|
+
contains(needle, haystack) {
|
|
136
|
+
return contains(needle, haystack);
|
|
137
|
+
}
|
|
138
|
+
containsAll(needles, haystack) {
|
|
139
|
+
return containsAll(needles, haystack);
|
|
140
|
+
}
|
|
141
|
+
containsAny(needles, haystack) {
|
|
142
|
+
return containsAny(needles, haystack);
|
|
143
|
+
}
|
|
144
|
+
containsNone(needles, haystack) {
|
|
145
|
+
return containsNone(needles, haystack);
|
|
146
|
+
}
|
|
147
|
+
containsOnly(needles, haystack) {
|
|
148
|
+
return containsOnly(needles, haystack);
|
|
149
|
+
}
|
|
150
|
+
doesNotContain(needle, haystack) {
|
|
151
|
+
return doesNotContain(needle, haystack);
|
|
152
|
+
}
|
|
153
|
+
toArray(array) {
|
|
154
|
+
return toArray(array);
|
|
155
|
+
}
|
|
156
|
+
flatten(array) {
|
|
157
|
+
return flatten(array);
|
|
158
|
+
}
|
|
159
|
+
mergeArrayable(...args) {
|
|
160
|
+
return mergeArrayable(...args);
|
|
161
|
+
}
|
|
162
|
+
partition(array, filter) {
|
|
163
|
+
return partition(array, filter);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Returns a random item/s from the array
|
|
167
|
+
*/
|
|
168
|
+
random(arr2, count = 1) {
|
|
169
|
+
return sample(arr2, count);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns random item/s from the array
|
|
173
|
+
*/
|
|
174
|
+
sample(arr2, count = 1) {
|
|
175
|
+
return sample(arr2, count);
|
|
176
|
+
}
|
|
177
|
+
unique(arr2) {
|
|
178
|
+
return uniq(arr2);
|
|
179
|
+
}
|
|
180
|
+
uniqueBy(arr2, equalFn) {
|
|
181
|
+
return uniqueBy(arr2, equalFn);
|
|
182
|
+
}
|
|
183
|
+
last(arr2) {
|
|
184
|
+
return last(arr2);
|
|
185
|
+
}
|
|
186
|
+
remove(arr2, value) {
|
|
187
|
+
return remove(arr2, value);
|
|
188
|
+
}
|
|
189
|
+
at(arr2, index) {
|
|
190
|
+
return at(arr2, index);
|
|
191
|
+
}
|
|
192
|
+
range(start, end, step = 1) {
|
|
193
|
+
return range(start, end, step);
|
|
194
|
+
}
|
|
195
|
+
move(arr2, from, to) {
|
|
196
|
+
return move(arr2, from, to);
|
|
197
|
+
}
|
|
198
|
+
clampArrayRange(arr2, n) {
|
|
199
|
+
return clampArrayRange(arr2, n);
|
|
200
|
+
}
|
|
201
|
+
shuffle(arr2) {
|
|
202
|
+
return shuffle(arr2);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Returns the sum of all items in the array
|
|
206
|
+
*/
|
|
207
|
+
sum(arr2) {
|
|
208
|
+
return sum(arr2);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns the average of all items in the array
|
|
212
|
+
*/
|
|
213
|
+
average(arr2) {
|
|
214
|
+
return average(arr2);
|
|
215
|
+
}
|
|
216
|
+
avg(arr2) {
|
|
217
|
+
return average(arr2);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Returns the median of all items in the array
|
|
221
|
+
*/
|
|
222
|
+
median(arr2) {
|
|
223
|
+
return median(arr2);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Returns the mode of all items in the array
|
|
227
|
+
*/
|
|
228
|
+
mode(arr2) {
|
|
229
|
+
return mode(arr2);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
Arr.macros = {};
|
|
233
|
+
Arr.getters = {};
|
|
234
|
+
const arr = new Arr();
|
|
235
|
+
|
|
236
|
+
export { Arr, arr, at, average, avg, clampArrayRange, contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain, flatten, last, median, mergeArrayable, mode, move, partition, range, remove, sample, shuffle, sum, toArray, uniq, unique, uniqueBy };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/arrays",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.5
|
|
4
|
+
"version": "0.56.3",
|
|
5
|
+
"packageManager": "pnpm@8.6.5",
|
|
6
6
|
"description": "The Stacks array utilities.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"license": "MIT",
|
|
@@ -40,12 +40,16 @@
|
|
|
40
40
|
"README.md"
|
|
41
41
|
],
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"
|
|
44
|
-
"@stacksjs/
|
|
43
|
+
"macroable": "^7.0.2",
|
|
44
|
+
"@stacksjs/types": "0.56.3",
|
|
45
|
+
"@stacksjs/utils": "0.56.3"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"macroable": "^7.0.2"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
47
|
-
"@stacksjs/development": "0.
|
|
48
|
-
"@stacksjs/types": "0.
|
|
51
|
+
"@stacksjs/development": "0.56.3",
|
|
52
|
+
"@stacksjs/types": "0.56.3"
|
|
49
53
|
},
|
|
50
54
|
"scripts": {
|
|
51
55
|
"build": "unbuild",
|