@stacksjs/arrays 0.57.4 โ†’ 0.58.20

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/README.md CHANGED
@@ -15,7 +15,7 @@ wip
15
15
  wip
16
16
 
17
17
  ```bash
18
- pnpm i -D @stacksjs/arrays
18
+ bun install -d @stacksjs/arrays
19
19
  ```
20
20
 
21
21
  Now, you can use it in your project:
@@ -31,7 +31,7 @@ Learn more in the docs.
31
31
  ## ๐Ÿงช Testing
32
32
 
33
33
  ```bash
34
- pnpm test
34
+ bun test
35
35
  ```
36
36
 
37
37
  ## ๐Ÿ“ˆ Changelog
@@ -50,10 +50,10 @@ For help, discussion about best practices, or any other conversation that would
50
50
 
51
51
  For casual chit-chat with others using this package:
52
52
 
53
- [Join the Stacks Discord Server](https://discord.ow3.org)
53
+ [Join the Stacks Discord Server](https://discord.gg/stacksjs)
54
54
 
55
55
  ## ๐Ÿ“„ License
56
56
 
57
57
  The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
58
58
 
59
- Made with โค๏ธ
59
+ Made with ๐Ÿ’™
package/dist/index.js ADDED
@@ -0,0 +1,469 @@
1
+ // @bun
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true,
8
+ configurable: true,
9
+ set: (newValue) => all[name] = () => newValue
10
+ });
11
+ };
12
+
13
+ // src/arr.ts
14
+ var exports_arr = {};
15
+ __export(exports_arr, {
16
+ zScore: () => {
17
+ {
18
+ return zScore;
19
+ }
20
+ },
21
+ variance: () => {
22
+ {
23
+ return variance;
24
+ }
25
+ },
26
+ uniqueBy: () => {
27
+ {
28
+ return uniqueBy;
29
+ }
30
+ },
31
+ unique: () => {
32
+ {
33
+ return unique;
34
+ }
35
+ },
36
+ uniq: () => {
37
+ {
38
+ return uniq;
39
+ }
40
+ },
41
+ toArray: () => {
42
+ {
43
+ return toArray;
44
+ }
45
+ },
46
+ sum: () => {
47
+ {
48
+ return sum;
49
+ }
50
+ },
51
+ standardDeviation: () => {
52
+ {
53
+ return standardDeviation;
54
+ }
55
+ },
56
+ shuffle: () => {
57
+ {
58
+ return shuffle;
59
+ }
60
+ },
61
+ sample: () => {
62
+ {
63
+ return sample;
64
+ }
65
+ },
66
+ remove: () => {
67
+ {
68
+ return remove;
69
+ }
70
+ },
71
+ range: () => {
72
+ {
73
+ return range;
74
+ }
75
+ },
76
+ product: () => {
77
+ {
78
+ return product;
79
+ }
80
+ },
81
+ percentile: () => {
82
+ {
83
+ return percentile;
84
+ }
85
+ },
86
+ partition: () => {
87
+ {
88
+ return partition;
89
+ }
90
+ },
91
+ move: () => {
92
+ {
93
+ return move;
94
+ }
95
+ },
96
+ mode: () => {
97
+ {
98
+ return mode;
99
+ }
100
+ },
101
+ min: () => {
102
+ {
103
+ return min;
104
+ }
105
+ },
106
+ mergeArrayable: () => {
107
+ {
108
+ return mergeArrayable;
109
+ }
110
+ },
111
+ median: () => {
112
+ {
113
+ return median;
114
+ }
115
+ },
116
+ max: () => {
117
+ {
118
+ return max;
119
+ }
120
+ },
121
+ last: () => {
122
+ {
123
+ return last;
124
+ }
125
+ },
126
+ interquartileRange: () => {
127
+ {
128
+ return interquartileRange;
129
+ }
130
+ },
131
+ flatten: () => {
132
+ {
133
+ return flatten;
134
+ }
135
+ },
136
+ doesNotContain: () => {
137
+ {
138
+ return doesNotContain;
139
+ }
140
+ },
141
+ covariance: () => {
142
+ {
143
+ return covariance;
144
+ }
145
+ },
146
+ containsOnly: () => {
147
+ {
148
+ return containsOnly;
149
+ }
150
+ },
151
+ containsNone: () => {
152
+ {
153
+ return containsNone;
154
+ }
155
+ },
156
+ containsAny: () => {
157
+ {
158
+ return containsAny;
159
+ }
160
+ },
161
+ containsAll: () => {
162
+ {
163
+ return containsAll;
164
+ }
165
+ },
166
+ contains: () => {
167
+ {
168
+ return contains;
169
+ }
170
+ },
171
+ clampArrayRange: () => {
172
+ {
173
+ return clampArrayRange;
174
+ }
175
+ },
176
+ avg: () => {
177
+ {
178
+ return avg;
179
+ }
180
+ },
181
+ average: () => {
182
+ {
183
+ return average;
184
+ }
185
+ },
186
+ at: () => {
187
+ {
188
+ return at;
189
+ }
190
+ }
191
+ });
192
+
193
+ // src/helpers.ts
194
+ import {clamp} from "@stacksjs/utils";
195
+ function toArray(array) {
196
+ array = array ?? [];
197
+ return Array.isArray(array) ? array : [array];
198
+ }
199
+ function flatten(array) {
200
+ return toArray(array).flat(1);
201
+ }
202
+ function mergeArrayable(...args) {
203
+ return args.flatMap((i) => toArray(i));
204
+ }
205
+ function partition(array, ...filters) {
206
+ const result = Array.from({ length: filters.length + 1 }).fill(null).map(() => []);
207
+ array.forEach((e, idx, arr) => {
208
+ let i = 0;
209
+ for (const filter of filters) {
210
+ if (filter(e, idx, arr)) {
211
+ result[i].push(e);
212
+ return;
213
+ }
214
+ i += 1;
215
+ }
216
+ result[i].push(e);
217
+ });
218
+ return result;
219
+ }
220
+ function uniq(array) {
221
+ return Array.from(new Set(array));
222
+ }
223
+ function unique(array) {
224
+ return uniq(array);
225
+ }
226
+ function uniqueBy(array, equalFn) {
227
+ return array.reduce((acc, cur) => {
228
+ const index = acc.findIndex((item) => equalFn(cur, item));
229
+ if (index === -1)
230
+ acc.push(cur);
231
+ return acc;
232
+ }, []);
233
+ }
234
+ function last(array) {
235
+ return at(array, -1);
236
+ }
237
+ function remove(array, value) {
238
+ if (!array)
239
+ return false;
240
+ const index = array.indexOf(value);
241
+ if (index >= 0) {
242
+ array.splice(index, 1);
243
+ return true;
244
+ }
245
+ return false;
246
+ }
247
+ function at(array, index) {
248
+ const len = array.length;
249
+ if (!len)
250
+ return;
251
+ if (index < 0)
252
+ index += len;
253
+ return array[index];
254
+ }
255
+ function move(array, from, to) {
256
+ const len = array.length;
257
+ if (!len)
258
+ return [];
259
+ if (from < 0)
260
+ from += len;
261
+ if (to < 0)
262
+ to += len;
263
+ const item = array.splice(from, 1)[0];
264
+ array.splice(to, 0, item);
265
+ return array;
266
+ }
267
+ function clampArrayRange(arr, n) {
268
+ return clamp(n, 0, arr.length - 1);
269
+ }
270
+ function sample(arr, count) {
271
+ return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
272
+ }
273
+ function shuffle(array) {
274
+ for (let i = array.length - 1;i > 0; i--) {
275
+ const j = Math.floor(Math.random() * (i + 1));
276
+ [array[i], array[j]] = [array[j], array[i]];
277
+ }
278
+ return array;
279
+ }
280
+ // src/math.ts
281
+ function average(arr) {
282
+ return sum(arr) / arr.length;
283
+ }
284
+ function avg(arr) {
285
+ return average(arr);
286
+ }
287
+ function median(arr) {
288
+ return arr[Math.floor(arr.length / 2)];
289
+ }
290
+ function mode(arr) {
291
+ return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop();
292
+ }
293
+ function sum(array) {
294
+ return array.reduce((acc, cur) => acc + cur, 0);
295
+ }
296
+ function product(array) {
297
+ return array.reduce((acc, cur) => acc * cur, 1);
298
+ }
299
+ function min(array) {
300
+ return Math.min(...array);
301
+ }
302
+ function max(array) {
303
+ return Math.max(...array);
304
+ }
305
+ function range(array) {
306
+ return max(array) - min(array);
307
+ }
308
+ function variance(array) {
309
+ const mean = average(array);
310
+ return average(array.map((num) => (num - mean) ** 2));
311
+ }
312
+ function standardDeviation(array) {
313
+ return Math.sqrt(variance(array));
314
+ }
315
+ function zScore(array, num) {
316
+ return (num - average(array)) / standardDeviation(array);
317
+ }
318
+ function percentile(array, num) {
319
+ return array.filter((n) => n < num).length / array.length;
320
+ }
321
+ function interquartileRange(array) {
322
+ const q1 = median(array.slice(0, Math.floor(array.length / 2)));
323
+ const q3 = median(array.slice(Math.ceil(array.length / 2)));
324
+ return q3 - q1;
325
+ }
326
+ function covariance(array1, array2) {
327
+ const mean1 = average(array1);
328
+ const mean2 = average(array2);
329
+ return average(array1.map((num1, i) => (num1 - mean1) * (array2[i] - mean2)));
330
+ }
331
+ // src/contains.ts
332
+ function contains(needle, haystack) {
333
+ return haystack.some((hay) => needle.includes(hay));
334
+ }
335
+ function containsAll(needles, haystack) {
336
+ return needles.every((needle) => contains(needle, haystack));
337
+ }
338
+ function containsAny(needles, haystack) {
339
+ return needles.some((needle) => contains(needle, haystack));
340
+ }
341
+ function containsNone(needles, haystack) {
342
+ return !containsAny(needles, haystack);
343
+ }
344
+ function containsOnly(needles, haystack) {
345
+ return containsAll(haystack, needles);
346
+ }
347
+ function doesNotContain(needle, haystack) {
348
+ return !contains(needle, haystack);
349
+ }
350
+ // src/macro.ts
351
+ var Arr = {
352
+ contains(needle, haystack) {
353
+ return contains(needle, haystack);
354
+ },
355
+ containsAll(needles, haystack) {
356
+ return containsAll(needles, haystack);
357
+ },
358
+ containsAny(needles, haystack) {
359
+ return containsAny(needles, haystack);
360
+ },
361
+ containsNone(needles, haystack) {
362
+ return containsNone(needles, haystack);
363
+ },
364
+ containsOnly(needles, haystack) {
365
+ return containsOnly(needles, haystack);
366
+ },
367
+ doesNotContain(needle, haystack) {
368
+ return doesNotContain(needle, haystack);
369
+ },
370
+ toArray(array) {
371
+ return toArray(array);
372
+ },
373
+ flatten(array) {
374
+ return flatten(array);
375
+ },
376
+ mergeArrayable(...args) {
377
+ return mergeArrayable(...args);
378
+ },
379
+ partition(array, filter) {
380
+ return partition(array, filter);
381
+ },
382
+ random(arr, count = 1) {
383
+ return sample(arr, count);
384
+ },
385
+ sample(arr, count = 1) {
386
+ return sample(arr, count);
387
+ },
388
+ unique(arr) {
389
+ return uniq(arr);
390
+ },
391
+ uniqueBy(arr, equalFn) {
392
+ return uniqueBy(arr, equalFn);
393
+ },
394
+ last(arr) {
395
+ return last(arr);
396
+ },
397
+ remove(arr, value) {
398
+ return remove(arr, value);
399
+ },
400
+ at(arr, index) {
401
+ return at(arr, index);
402
+ },
403
+ range(arr) {
404
+ return range(arr);
405
+ },
406
+ move(arr, from, to) {
407
+ return move(arr, from, to);
408
+ },
409
+ clampArrayRange(arr, n) {
410
+ return clampArrayRange(arr, n);
411
+ },
412
+ shuffle(arr) {
413
+ return shuffle(arr);
414
+ },
415
+ sum(arr) {
416
+ return sum(arr);
417
+ },
418
+ average(arr) {
419
+ return average(arr);
420
+ },
421
+ avg(arr) {
422
+ return average(arr);
423
+ },
424
+ median(arr) {
425
+ return median(arr);
426
+ },
427
+ mode(arr) {
428
+ return mode(arr);
429
+ }
430
+ };
431
+ export {
432
+ zScore,
433
+ variance,
434
+ uniqueBy,
435
+ unique,
436
+ uniq,
437
+ toArray,
438
+ sum,
439
+ standardDeviation,
440
+ shuffle,
441
+ sample,
442
+ remove,
443
+ range,
444
+ product,
445
+ percentile,
446
+ partition,
447
+ move,
448
+ mode,
449
+ min,
450
+ mergeArrayable,
451
+ median,
452
+ max,
453
+ last,
454
+ interquartileRange,
455
+ flatten,
456
+ doesNotContain,
457
+ covariance,
458
+ containsOnly,
459
+ containsNone,
460
+ containsAny,
461
+ containsAll,
462
+ contains,
463
+ clampArrayRange,
464
+ avg,
465
+ average,
466
+ at,
467
+ exports_arr as arr,
468
+ Arr
469
+ };
package/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.57.4",
5
- "packageManager": "pnpm@8.6.6",
4
+ "version": "0.58.20",
6
5
  "description": "The Stacks array utilities.",
7
6
  "author": "Chris Breuer",
8
7
  "license": "MIT",
9
8
  "funding": "https://github.com/sponsors/chrisbbreuer",
10
- "homepage": "https://github.com/stacksjs/stacks/tree/main/.stacks/core/arrays#readme",
9
+ "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/src/arrays#readme",
11
10
  "repository": {
12
11
  "type": "git",
13
12
  "url": "git+https://github.com/stacksjs/stacks.git",
14
- "directory": "./.stacks/core/arrays"
13
+ "directory": "./storage/framework/core/src/arrays"
15
14
  },
16
15
  "bugs": {
17
16
  "url": "https://github.com/stacksjs/stacks/issues"
@@ -26,34 +25,35 @@
26
25
  ],
27
26
  "exports": {
28
27
  ".": {
29
- "types": "./dist/index.d.ts",
30
- "import": "./dist/index.mjs"
28
+ "bun": "./src/index.ts",
29
+ "import": "./dist/index.js"
30
+ },
31
+ "./*": {
32
+ "bun": "./src/*",
33
+ "import": "./dist/*"
31
34
  }
32
35
  },
33
- "module": "dist/index.mjs",
36
+ "module": "dist/index.js",
34
37
  "types": "dist/index.d.ts",
35
38
  "contributors": [
36
- "Chris Breuer <chris@ow3.org>"
39
+ "Chris Breuer <chris@stacksjs.org>"
37
40
  ],
38
41
  "files": [
39
- "dist",
40
- "README.md"
42
+ "README.md",
43
+ "dist"
41
44
  ],
45
+ "scripts": {
46
+ "build": "bun --bun build.ts",
47
+ "typecheck": "bun --bun tsc --noEmit",
48
+ "prepublishOnly": "bun --bun run build"
49
+ },
42
50
  "peerDependencies": {
43
- "macroable": "^7.0.2",
44
- "@stacksjs/types": "0.57.4",
45
- "@stacksjs/utils": "0.57.4"
51
+ "@stacksjs/utils": "workspace:*"
46
52
  },
47
53
  "dependencies": {
48
- "macroable": "^7.0.2"
54
+ "@stacksjs/utils": "workspace:*"
49
55
  },
50
56
  "devDependencies": {
51
- "@stacksjs/development": "0.57.4",
52
- "@stacksjs/types": "0.57.4"
53
- },
54
- "scripts": {
55
- "build": "unbuild",
56
- "dev": "unbuild --stub",
57
- "typecheck": "tsc --noEmit"
57
+ "@stacksjs/development": "workspace:*"
58
58
  }
59
- }
59
+ }
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- # MIT License
2
-
3
- Copyright (c) 2022 Open Web Foundation
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,6 +0,0 @@
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;
package/dist/contains.mjs DELETED
@@ -1,18 +0,0 @@
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
- }
package/dist/helpers.d.ts DELETED
@@ -1,99 +0,0 @@
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[];
package/dist/helpers.mjs DELETED
@@ -1,95 +0,0 @@
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 DELETED
@@ -1,4 +0,0 @@
1
- export * from './math';
2
- export * from './helpers';
3
- export * from './contains';
4
- export * from './macro';
package/dist/index.mjs DELETED
@@ -1,4 +0,0 @@
1
- export * from "./math.mjs";
2
- export * from "./helpers.mjs";
3
- export * from "./contains.mjs";
4
- export * from "./macro.mjs";
package/dist/macro.d.ts DELETED
@@ -1,52 +0,0 @@
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 DELETED
@@ -1,105 +0,0 @@
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 DELETED
@@ -1,5 +0,0 @@
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 DELETED
@@ -1,15 +0,0 @@
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
- }