@stacksjs/arrays 0.56.35 → 0.57.2

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.
@@ -0,0 +1,6 @@
1
+ export declare function contains(needle: string, haystack: string[]): boolean;
2
+ export declare function containsAll(needles: string[], haystack: string[]): boolean;
3
+ export declare function containsAny(needles: string[], haystack: string[]): boolean;
4
+ export declare function containsNone(needles: string[], haystack: string[]): boolean;
5
+ export declare function containsOnly(needles: string[], haystack: string[]): boolean;
6
+ export declare function doesNotContain(needle: string, haystack: string[]): boolean;
@@ -0,0 +1,18 @@
1
+ export function contains(needle, haystack) {
2
+ return haystack.some((hay) => needle.includes(hay));
3
+ }
4
+ export function containsAll(needles, haystack) {
5
+ return needles.every((needle) => contains(needle, haystack));
6
+ }
7
+ export function containsAny(needles, haystack) {
8
+ return needles.some((needle) => contains(needle, haystack));
9
+ }
10
+ export function containsNone(needles, haystack) {
11
+ return !containsAny(needles, haystack);
12
+ }
13
+ export function containsOnly(needles, haystack) {
14
+ return containsAll(haystack, needles);
15
+ }
16
+ export function doesNotContain(needle, haystack) {
17
+ return !contains(needle, haystack);
18
+ }
@@ -0,0 +1,99 @@
1
+ import type { Arrayable, Nullable } from '@stacksjs/types';
2
+ /**
3
+ * Convert `Arrayable<T>` to `Array<T>`
4
+ *
5
+ * @category Array
6
+ */
7
+ export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
8
+ /**
9
+ * Convert `Arrayable<T>` to `Array<T>` and flatten it
10
+ *
11
+ * @category Array
12
+ */
13
+ export declare function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
14
+ /**
15
+ * Use rest arguments to merge arrays
16
+ *
17
+ * @category Array
18
+ */
19
+ export declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
20
+ export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
21
+ /**
22
+ * Divide an array into two parts by a filter function
23
+ *
24
+ * @category Array
25
+ * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
26
+ */
27
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
28
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
29
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
30
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>): [T[], T[], T[], T[], T[]];
31
+ export 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[]];
32
+ export 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[]];
33
+ /**
34
+ * Unique an Array
35
+ *
36
+ * @category Array
37
+ */
38
+ export declare function uniq<T>(array: readonly T[]): T[];
39
+ export declare function unique<T>(array: readonly T[]): T[];
40
+ /**
41
+ * Unique an Array by a custom equality function
42
+ *
43
+ * @category Array
44
+ */
45
+ export declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
46
+ /**
47
+ * Get last item
48
+ *
49
+ * @category Array
50
+ */
51
+ export declare function last(array: readonly []): undefined;
52
+ export declare function last<T>(array: readonly T[]): T;
53
+ /**
54
+ * Remove an item from Array
55
+ *
56
+ * @category Array
57
+ */
58
+ export declare function remove<T>(array: T[], value: T): boolean;
59
+ /**
60
+ * Get nth item of Array. Negative for backward
61
+ *
62
+ * @category Array
63
+ */
64
+ export declare function at(array: readonly [], index: number): undefined;
65
+ export declare function at<T>(array: readonly T[], index: number): T;
66
+ /**
67
+ * Generate a range array of numbers. The `stop` is exclusive.
68
+ *
69
+ * @category Array
70
+ */
71
+ export declare function range(stop: number): number[];
72
+ export declare function range(start: number, stop: number, step?: number): number[];
73
+ /**
74
+ * Move element in an Array
75
+ *
76
+ * @category Array
77
+ * @param arr
78
+ * @param from
79
+ * @param to
80
+ */
81
+ export declare function move<T>(arr: T[], from: number, to: number): T[];
82
+ /**
83
+ * Clamp a number to the index range of an array.
84
+ *
85
+ * @category Array
86
+ */
87
+ export declare function clampArrayRange(arr: readonly unknown[], n: number): number;
88
+ /**
89
+ * Get random items from an array
90
+ *
91
+ * @category Array
92
+ */
93
+ export declare function sample<T>(arr: T[], count: number): T[];
94
+ /**
95
+ * Shuffle an array. This function mutates the array.
96
+ *
97
+ * @category Array
98
+ */
99
+ export declare function shuffle<T>(array: T[]): T[];
@@ -0,0 +1,95 @@
1
+ import { clamp } from "@stacksjs/utils";
2
+ export function toArray(array) {
3
+ array = array ?? [];
4
+ return Array.isArray(array) ? array : [array];
5
+ }
6
+ export function flatten(array) {
7
+ return toArray(array).flat(1);
8
+ }
9
+ export function mergeArrayable(...args) {
10
+ return args.flatMap((i) => toArray(i));
11
+ }
12
+ export function partition(array, ...filters) {
13
+ const result = new Array(filters.length + 1).fill(null).map(() => []);
14
+ array.forEach((e, idx, arr) => {
15
+ let i = 0;
16
+ for (const filter of filters) {
17
+ if (filter(e, idx, arr)) {
18
+ result[i].push(e);
19
+ return;
20
+ }
21
+ i += 1;
22
+ }
23
+ result[i].push(e);
24
+ });
25
+ return result;
26
+ }
27
+ export function uniq(array) {
28
+ return Array.from(new Set(array));
29
+ }
30
+ export function unique(array) {
31
+ return uniq(array);
32
+ }
33
+ export function uniqueBy(array, equalFn) {
34
+ return array.reduce((acc, cur) => {
35
+ const index = acc.findIndex((item) => equalFn(cur, item));
36
+ if (index === -1)
37
+ acc.push(cur);
38
+ return acc;
39
+ }, []);
40
+ }
41
+ export function last(array) {
42
+ return at(array, -1);
43
+ }
44
+ export function remove(array, value) {
45
+ if (!array)
46
+ return false;
47
+ const index = array.indexOf(value);
48
+ if (index >= 0) {
49
+ array.splice(index, 1);
50
+ return true;
51
+ }
52
+ return false;
53
+ }
54
+ export function at(array, index) {
55
+ const len = array.length;
56
+ if (!len)
57
+ return void 0;
58
+ if (index < 0)
59
+ index += len;
60
+ return array[index];
61
+ }
62
+ export function range(...args) {
63
+ let start, stop, step;
64
+ if (args.length === 1) {
65
+ start = 0;
66
+ step = 1;
67
+ [stop] = args;
68
+ } else {
69
+ [start, stop, step = 1] = args;
70
+ }
71
+ const arr = [];
72
+ let current = start;
73
+ while (current < stop) {
74
+ arr.push(current);
75
+ current += step || 1;
76
+ }
77
+ return arr;
78
+ }
79
+ export function move(arr, from, to) {
80
+ arr.splice(to, 0, arr.splice(from, 1)[0]);
81
+ return arr;
82
+ }
83
+ export function clampArrayRange(arr, n) {
84
+ return clamp(n, 0, arr.length - 1);
85
+ }
86
+ export function sample(arr, count) {
87
+ return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
88
+ }
89
+ export function shuffle(array) {
90
+ for (let i = array.length - 1; i > 0; i--) {
91
+ const j = Math.floor(Math.random() * (i + 1));
92
+ [array[i], array[j]] = [array[j], array[i]];
93
+ }
94
+ return array;
95
+ }
package/dist/index.d.ts CHANGED
@@ -1,166 +1,4 @@
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
-
10
- /**
11
- * Convert `Arrayable<T>` to `Array<T>`
12
- *
13
- * @category Array
14
- */
15
- declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
16
- /**
17
- * Convert `Arrayable<T>` to `Array<T>` and flatten it
18
- *
19
- * @category Array
20
- */
21
- declare function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
22
- /**
23
- * Use rest arguments to merge arrays
24
- *
25
- * @category Array
26
- */
27
- declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
28
- type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
29
- /**
30
- * Divide an array into two parts by a filter function
31
- *
32
- * @category Array
33
- * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
34
- */
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[]];
41
- /**
42
- * Unique an Array
43
- *
44
- * @category Array
45
- */
46
- declare function uniq<T>(array: readonly T[]): T[];
47
- declare function unique<T>(array: readonly T[]): T[];
48
- /**
49
- * Unique an Array by a custom equality function
50
- *
51
- * @category Array
52
- */
53
- declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
54
- /**
55
- * Get last item
56
- *
57
- * @category Array
58
- */
59
- declare function last(array: readonly []): undefined;
60
- declare function last<T>(array: readonly T[]): T;
61
- /**
62
- * Remove an item from Array
63
- *
64
- * @category Array
65
- */
66
- declare function remove<T>(array: T[], value: T): boolean;
67
- /**
68
- * Get nth item of Array. Negative for backward
69
- *
70
- * @category Array
71
- */
72
- declare function at(array: readonly [], index: number): undefined;
73
- declare function at<T>(array: readonly T[], index: number): T;
74
- /**
75
- * Generate a range array of numbers. The `stop` is exclusive.
76
- *
77
- * @category Array
78
- */
79
- declare function range(stop: number): number[];
80
- declare function range(start: number, stop: number, step?: number): number[];
81
- /**
82
- * Move element in an Array
83
- *
84
- * @category Array
85
- * @param arr
86
- * @param from
87
- * @param to
88
- */
89
- declare function move<T>(arr: T[], from: number, to: number): T[];
90
- /**
91
- * Clamp a number to the index range of an array.
92
- *
93
- * @category Array
94
- */
95
- declare function clampArrayRange(arr: readonly unknown[], n: number): number;
96
- /**
97
- * Get random items from an array
98
- *
99
- * @category Array
100
- */
101
- declare function sample<T>(arr: T[], count: number): T[];
102
- /**
103
- * Shuffle an array. This function mutates the array.
104
- *
105
- * @category Array
106
- */
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 };
1
+ export * from './math';
2
+ export * from './helpers';
3
+ export * from './contains';
4
+ export * from './macro';
package/dist/index.mjs CHANGED
@@ -1,236 +1,4 @@
1
- import { clamp } from '@stacksjs/utils';
2
- import { Macroable } from 'macroable';
3
-
4
- function average(arr) {
5
- return sum(arr) / arr.length;
6
- }
7
- function avg(arr) {
8
- return average(arr);
9
- }
10
- function median(arr) {
11
- return arr[Math.floor(arr.length / 2)];
12
- }
13
- function mode(arr) {
14
- return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop();
15
- }
16
- function sum(array) {
17
- return array.reduce((acc, cur) => acc + cur, 0);
18
- }
19
-
20
- function toArray(array) {
21
- array = array ?? [];
22
- return Array.isArray(array) ? array : [array];
23
- }
24
- function flatten(array) {
25
- return toArray(array).flat(1);
26
- }
27
- function mergeArrayable(...args) {
28
- return args.flatMap((i) => toArray(i));
29
- }
30
- function partition(array, ...filters) {
31
- const result = new Array(filters.length + 1).fill(null).map(() => []);
32
- array.forEach((e, idx, arr) => {
33
- let i = 0;
34
- for (const filter of filters) {
35
- if (filter(e, idx, arr)) {
36
- result[i].push(e);
37
- return;
38
- }
39
- i += 1;
40
- }
41
- result[i].push(e);
42
- });
43
- return result;
44
- }
45
- function uniq(array) {
46
- return Array.from(new Set(array));
47
- }
48
- function unique(array) {
49
- return uniq(array);
50
- }
51
- function uniqueBy(array, equalFn) {
52
- return array.reduce((acc, cur) => {
53
- const index = acc.findIndex((item) => equalFn(cur, item));
54
- if (index === -1)
55
- acc.push(cur);
56
- return acc;
57
- }, []);
58
- }
59
- function last(array) {
60
- return at(array, -1);
61
- }
62
- function remove(array, value) {
63
- if (!array)
64
- return false;
65
- const index = array.indexOf(value);
66
- if (index >= 0) {
67
- array.splice(index, 1);
68
- return true;
69
- }
70
- return false;
71
- }
72
- function at(array, index) {
73
- const len = array.length;
74
- if (!len)
75
- return void 0;
76
- if (index < 0)
77
- index += len;
78
- return array[index];
79
- }
80
- function range(...args) {
81
- let start, stop, step;
82
- if (args.length === 1) {
83
- start = 0;
84
- step = 1;
85
- [stop] = args;
86
- } else {
87
- [start, stop, step = 1] = args;
88
- }
89
- const arr = [];
90
- let current = start;
91
- while (current < stop) {
92
- arr.push(current);
93
- current += step || 1;
94
- }
95
- return arr;
96
- }
97
- function move(arr, from, to) {
98
- arr.splice(to, 0, arr.splice(from, 1)[0]);
99
- return arr;
100
- }
101
- function clampArrayRange(arr, n) {
102
- return clamp(n, 0, arr.length - 1);
103
- }
104
- function sample(arr, count) {
105
- return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
106
- }
107
- function shuffle(array) {
108
- for (let i = array.length - 1; i > 0; i--) {
109
- const j = Math.floor(Math.random() * (i + 1));
110
- [array[i], array[j]] = [array[j], array[i]];
111
- }
112
- return array;
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 };
1
+ export * from "./math.mjs";
2
+ export * from "./helpers.mjs";
3
+ export * from "./contains.mjs";
4
+ export * from "./macro.mjs";
@@ -0,0 +1,52 @@
1
+ import { Macroable } from 'macroable';
2
+ import type { Arrayable, Nullable } from '@stacksjs/types';
3
+ import type { PartitionFilter } from './helpers';
4
+ export declare class Arr extends Macroable {
5
+ contains(needle: string, haystack: string[]): boolean;
6
+ containsAll(needles: string[], haystack: string[]): boolean;
7
+ containsAny(needles: string[], haystack: string[]): boolean;
8
+ containsNone(needles: string[], haystack: string[]): boolean;
9
+ containsOnly(needles: string[], haystack: string[]): boolean;
10
+ doesNotContain(needle: string, haystack: string[]): boolean;
11
+ toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
12
+ flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
13
+ mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
14
+ partition<T>(array: readonly T[], filter: PartitionFilter<T>): [T[], T[]];
15
+ /**
16
+ * Returns a random item/s from the array
17
+ */
18
+ random<T>(arr: T[], count?: number): T[];
19
+ /**
20
+ * Returns random item/s from the array
21
+ */
22
+ sample<T>(arr: T[], count?: number): T[];
23
+ unique<T>(arr: T[]): T[];
24
+ uniqueBy<T>(arr: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
25
+ last<T>(arr: T[]): T | undefined;
26
+ remove<T>(arr: T[], value: T): boolean;
27
+ at(arr: any[], index: number): any;
28
+ range(start: number, end: number, step?: number): number[];
29
+ move<T>(arr: T[], from: number, to: number): T[];
30
+ clampArrayRange(arr: readonly unknown[], n: number): number;
31
+ shuffle<T>(arr: T[]): T[];
32
+ /**
33
+ * Returns the sum of all items in the array
34
+ */
35
+ sum(arr: number[]): number;
36
+ /**
37
+ * Returns the average of all items in the array
38
+ */
39
+ average(arr: number[]): number;
40
+ avg(arr: number[]): number;
41
+ /**
42
+ * Returns the median of all items in the array
43
+ */
44
+ median(arr: number[]): number;
45
+ /**
46
+ * Returns the mode of all items in the array
47
+ */
48
+ mode(arr: number[]): number;
49
+ static macros: {};
50
+ static getters: {};
51
+ }
52
+ export declare const arr: Arr;
package/dist/macro.mjs ADDED
@@ -0,0 +1,105 @@
1
+ import { Macroable } from "macroable";
2
+ import { at, clampArrayRange, flatten, last, mergeArrayable, move, partition, range, remove, sample, shuffle, toArray, uniq, uniqueBy } from "./helpers.mjs";
3
+ import { contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain } from "./contains.mjs";
4
+ import { average, median, mode, sum } from "./math.mjs";
5
+ export class Arr extends Macroable {
6
+ contains(needle, haystack) {
7
+ return contains(needle, haystack);
8
+ }
9
+ containsAll(needles, haystack) {
10
+ return containsAll(needles, haystack);
11
+ }
12
+ containsAny(needles, haystack) {
13
+ return containsAny(needles, haystack);
14
+ }
15
+ containsNone(needles, haystack) {
16
+ return containsNone(needles, haystack);
17
+ }
18
+ containsOnly(needles, haystack) {
19
+ return containsOnly(needles, haystack);
20
+ }
21
+ doesNotContain(needle, haystack) {
22
+ return doesNotContain(needle, haystack);
23
+ }
24
+ toArray(array) {
25
+ return toArray(array);
26
+ }
27
+ flatten(array) {
28
+ return flatten(array);
29
+ }
30
+ mergeArrayable(...args) {
31
+ return mergeArrayable(...args);
32
+ }
33
+ partition(array, filter) {
34
+ return partition(array, filter);
35
+ }
36
+ /**
37
+ * Returns a random item/s from the array
38
+ */
39
+ random(arr2, count = 1) {
40
+ return sample(arr2, count);
41
+ }
42
+ /**
43
+ * Returns random item/s from the array
44
+ */
45
+ sample(arr2, count = 1) {
46
+ return sample(arr2, count);
47
+ }
48
+ unique(arr2) {
49
+ return uniq(arr2);
50
+ }
51
+ uniqueBy(arr2, equalFn) {
52
+ return uniqueBy(arr2, equalFn);
53
+ }
54
+ last(arr2) {
55
+ return last(arr2);
56
+ }
57
+ remove(arr2, value) {
58
+ return remove(arr2, value);
59
+ }
60
+ at(arr2, index) {
61
+ return at(arr2, index);
62
+ }
63
+ range(start, end, step = 1) {
64
+ return range(start, end, step);
65
+ }
66
+ move(arr2, from, to) {
67
+ return move(arr2, from, to);
68
+ }
69
+ clampArrayRange(arr2, n) {
70
+ return clampArrayRange(arr2, n);
71
+ }
72
+ shuffle(arr2) {
73
+ return shuffle(arr2);
74
+ }
75
+ /**
76
+ * Returns the sum of all items in the array
77
+ */
78
+ sum(arr2) {
79
+ return sum(arr2);
80
+ }
81
+ /**
82
+ * Returns the average of all items in the array
83
+ */
84
+ average(arr2) {
85
+ return average(arr2);
86
+ }
87
+ avg(arr2) {
88
+ return average(arr2);
89
+ }
90
+ /**
91
+ * Returns the median of all items in the array
92
+ */
93
+ median(arr2) {
94
+ return median(arr2);
95
+ }
96
+ /**
97
+ * Returns the mode of all items in the array
98
+ */
99
+ mode(arr2) {
100
+ return mode(arr2);
101
+ }
102
+ }
103
+ Arr.macros = {};
104
+ Arr.getters = {};
105
+ export const arr = new Arr();
package/dist/math.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare function average(arr: number[]): number;
2
+ export declare function avg(arr: number[]): number;
3
+ export declare function median(arr: number[]): number;
4
+ export declare function mode(arr: number[]): number;
5
+ export declare function sum(array: readonly number[]): number;
package/dist/math.mjs ADDED
@@ -0,0 +1,15 @@
1
+ export function average(arr) {
2
+ return sum(arr) / arr.length;
3
+ }
4
+ export function avg(arr) {
5
+ return average(arr);
6
+ }
7
+ export function median(arr) {
8
+ return arr[Math.floor(arr.length / 2)];
9
+ }
10
+ export function mode(arr) {
11
+ return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop();
12
+ }
13
+ export function sum(array) {
14
+ return array.reduce((acc, cur) => acc + cur, 0);
15
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.56.35",
4
+ "version": "0.57.2",
5
5
  "packageManager": "pnpm@8.6.5",
6
6
  "description": "The Stacks array utilities.",
7
7
  "author": "Chris Breuer",
@@ -41,15 +41,15 @@
41
41
  ],
42
42
  "peerDependencies": {
43
43
  "macroable": "^7.0.2",
44
- "@stacksjs/types": "0.56.35",
45
- "@stacksjs/utils": "0.56.35"
44
+ "@stacksjs/types": "0.57.2",
45
+ "@stacksjs/utils": "0.57.2"
46
46
  },
47
47
  "dependencies": {
48
48
  "macroable": "^7.0.2"
49
49
  },
50
50
  "devDependencies": {
51
- "@stacksjs/development": "0.56.35",
52
- "@stacksjs/types": "0.56.35"
51
+ "@stacksjs/development": "0.57.2",
52
+ "@stacksjs/types": "0.57.2"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "unbuild",