@stacksjs/arrays 0.53.1 → 0.56.23

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 CHANGED
@@ -1,80 +1,83 @@
1
- import type { Arrayable, Nullable } from '@stacksjs/types';
2
- export declare function contains(needle: string, haystack: string[]): boolean;
3
- export declare function containsAll(needles: string[], haystack: string[]): boolean;
4
- export declare function containsAny(needles: string[], haystack: string[]): boolean;
5
- export declare function containsNone(needles: string[], haystack: string[]): boolean;
6
- export declare function containsOnly(needles: string[], haystack: string[]): boolean;
7
- export declare function doesNotContain(needle: string, haystack: string[]): boolean;
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
- export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
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
- export declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
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
- export declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
26
- export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
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
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
34
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
35
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
36
- 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[]];
37
- 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[]];
38
- 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[]];
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
- export declare function uniq<T>(array: readonly T[]): T[];
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
- export declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
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
- export declare function last(array: readonly []): undefined;
57
- export declare function last<T>(array: readonly T[]): T;
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
- export declare function remove<T>(array: T[], value: T): boolean;
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
- export declare function at(array: readonly [], index: number): undefined;
70
- export declare function at<T>(array: readonly T[], index: number): T;
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
- export declare function range(stop: number): number[];
77
- export declare function range(start: number, stop: number, step?: number): number[];
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
- export declare function move<T>(arr: T[], from: number, to: number): T[];
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
- export declare function clampArrayRange(n: number, arr: readonly unknown[]): any;
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
- export declare function sample<T>(arr: T[], count: number): T[];
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
- export declare function shuffle<T>(array: T[]): T[];
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 "@stacksjs/utils";
2
- export function contains(needle, haystack) {
3
- return haystack.some((hay) => needle.includes(hay));
4
- }
5
- export function containsAll(needles, haystack) {
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
- export function containsAny(needles, haystack) {
9
- return needles.some((needle) => contains(needle, haystack));
7
+ function avg(arr) {
8
+ return average(arr);
10
9
  }
11
- export function containsNone(needles, haystack) {
12
- return !containsAny(needles, haystack);
10
+ function median(arr) {
11
+ return arr[Math.floor(arr.length / 2)];
13
12
  }
14
- export function containsOnly(needles, haystack) {
15
- return containsAll(haystack, needles);
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
- export function doesNotContain(needle, haystack) {
18
- return !contains(needle, haystack);
16
+ function sum(array) {
17
+ return array.reduce((acc, cur) => acc + cur, 0);
19
18
  }
20
- export function toArray(array) {
19
+
20
+ function toArray(array) {
21
21
  array = array ?? [];
22
22
  return Array.isArray(array) ? array : [array];
23
23
  }
24
- export function flattenArrayable(array) {
24
+ function flatten(array) {
25
25
  return toArray(array).flat(1);
26
26
  }
27
- export function mergeArrayable(...args) {
27
+ function mergeArrayable(...args) {
28
28
  return args.flatMap((i) => toArray(i));
29
29
  }
30
- export function partition(array, ...filters) {
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
- export function uniq(array) {
45
+ function uniq(array) {
46
46
  return Array.from(new Set(array));
47
47
  }
48
- export function uniqueBy(array, equalFn) {
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
- export function last(array) {
59
+ function last(array) {
57
60
  return at(array, -1);
58
61
  }
59
- export function remove(array, value) {
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
- export function at(array, index) {
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
- export function range(...args) {
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
- export function move(arr, from, to) {
97
+ function move(arr, from, to) {
95
98
  arr.splice(to, 0, arr.splice(from, 1)[0]);
96
99
  return arr;
97
100
  }
98
- export function clampArrayRange(n, arr) {
101
+ function clampArrayRange(arr, n) {
99
102
  return clamp(n, 0, arr.length - 1);
100
103
  }
101
- export function sample(arr, count) {
104
+ function sample(arr, count) {
102
105
  return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
103
106
  }
104
- export function shuffle(array) {
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.53.1",
5
- "packageManager": "pnpm@8.5.1",
4
+ "version": "0.56.23",
5
+ "packageManager": "pnpm@8.6.5",
6
6
  "description": "The Stacks array utilities.",
7
7
  "author": "Chris Breuer",
8
8
  "license": "MIT",
@@ -30,6 +30,7 @@
30
30
  "import": "./dist/index.mjs"
31
31
  }
32
32
  },
33
+ "main": "dist/index.mjs",
33
34
  "module": "dist/index.mjs",
34
35
  "types": "dist/index.d.ts",
35
36
  "contributors": [
@@ -40,12 +41,16 @@
40
41
  "README.md"
41
42
  ],
42
43
  "peerDependencies": {
43
- "@stacksjs/types": "0.53.1",
44
- "@stacksjs/utils": "0.53.1"
44
+ "macroable": "^7.0.2",
45
+ "@stacksjs/types": "0.56.23",
46
+ "@stacksjs/utils": "0.56.23"
47
+ },
48
+ "dependencies": {
49
+ "macroable": "^7.0.2"
45
50
  },
46
51
  "devDependencies": {
47
- "@stacksjs/development": "0.53.1",
48
- "@stacksjs/types": "0.53.1"
52
+ "@stacksjs/development": "0.56.23",
53
+ "@stacksjs/types": "0.56.23"
49
54
  },
50
55
  "scripts": {
51
56
  "build": "unbuild",