@stacksjs/arrays 0.39.1 → 0.41.0

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,5 +1,97 @@
1
+ import type { Arrayable, Nullable } from '@stacksjs/types';
1
2
  export declare function contains(needle: string, haystack: string[]): boolean;
2
3
  export declare function containsAll(needles: string[], haystack: string[]): boolean;
3
4
  export declare function containsAny(needles: string[], haystack: string[]): boolean;
4
5
  export declare function containsNone(needles: string[], haystack: string[]): boolean;
5
6
  export declare function containsOnly(needles: string[], haystack: string[]): boolean;
7
+ /**
8
+ * Convert `Arrayable<T>` to `Array<T>`
9
+ *
10
+ * @category Array
11
+ */
12
+ export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
13
+ /**
14
+ * Convert `Arrayable<T>` to `Array<T>` and flatten it
15
+ *
16
+ * @category Array
17
+ */
18
+ export declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
19
+ /**
20
+ * Use rest arguments to merge arrays
21
+ *
22
+ * @category Array
23
+ */
24
+ export declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
25
+ export declare type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
26
+ /**
27
+ * Divide an array into two parts by a filter function
28
+ *
29
+ * @category Array
30
+ * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
31
+ */
32
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
33
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
34
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
35
+ 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[]];
36
+ 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[]];
37
+ 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[]];
38
+ /**
39
+ * Unique an Array
40
+ *
41
+ * @category Array
42
+ */
43
+ export declare function uniq<T>(array: readonly T[]): T[];
44
+ /**
45
+ * Get last item
46
+ *
47
+ * @category Array
48
+ */
49
+ export declare function last(array: readonly []): undefined;
50
+ export declare function last<T>(array: readonly T[]): T;
51
+ /**
52
+ * Remove an item from Array
53
+ *
54
+ * @category Array
55
+ */
56
+ export declare function remove<T>(array: T[], value: T): boolean;
57
+ /**
58
+ * Get nth item of Array. Negative for backward
59
+ *
60
+ * @category Array
61
+ */
62
+ export declare function at(array: readonly [], index: number): undefined;
63
+ export declare function at<T>(array: readonly T[], index: number): T;
64
+ /**
65
+ * Generate a range array of numbers. The `stop` is exclusive.
66
+ *
67
+ * @category Array
68
+ */
69
+ export declare function range(stop: number): number[];
70
+ export declare function range(start: number, stop: number, step?: number): number[];
71
+ /**
72
+ * Move element in an Array
73
+ *
74
+ * @category Array
75
+ * @param arr
76
+ * @param from
77
+ * @param to
78
+ */
79
+ export declare function move<T>(arr: T[], from: number, to: number): T[];
80
+ /**
81
+ * Clamp a number to the index range of an array.
82
+ *
83
+ * @category Array
84
+ */
85
+ export declare function clampArrayRange(n: number, arr: readonly unknown[]): any;
86
+ /**
87
+ * Get random items from an array
88
+ *
89
+ * @category Array
90
+ */
91
+ export declare function sample<T>(arr: T[], count: number): T[];
92
+ /**
93
+ * Shuffle an array. This function mutates the array.
94
+ *
95
+ * @category Array
96
+ */
97
+ export declare function shuffle<T>(array: T[]): T[];
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { clamp } from "@stacksjs/utils";
1
2
  export function contains(needle, haystack) {
2
3
  return haystack.some((hay) => needle.includes(hay));
3
4
  }
@@ -13,3 +14,86 @@ export function containsNone(needles, haystack) {
13
14
  export function containsOnly(needles, haystack) {
14
15
  return containsAll(haystack, needles);
15
16
  }
17
+ export function toArray(array) {
18
+ array = array ?? [];
19
+ return Array.isArray(array) ? array : [array];
20
+ }
21
+ export function flattenArrayable(array) {
22
+ return toArray(array).flat(1);
23
+ }
24
+ export function mergeArrayable(...args) {
25
+ return args.flatMap((i) => toArray(i));
26
+ }
27
+ export function partition(array, ...filters) {
28
+ const result = new Array(filters.length + 1).fill(null).map(() => []);
29
+ array.forEach((e, idx, arr) => {
30
+ let i = 0;
31
+ for (const filter of filters) {
32
+ if (filter(e, idx, arr)) {
33
+ result[i].push(e);
34
+ return;
35
+ }
36
+ i += 1;
37
+ }
38
+ result[i].push(e);
39
+ });
40
+ return result;
41
+ }
42
+ export function uniq(array) {
43
+ return Array.from(new Set(array));
44
+ }
45
+ export function last(array) {
46
+ return at(array, -1);
47
+ }
48
+ export function remove(array, value) {
49
+ if (!array)
50
+ return false;
51
+ const index = array.indexOf(value);
52
+ if (index >= 0) {
53
+ array.splice(index, 1);
54
+ return true;
55
+ }
56
+ return false;
57
+ }
58
+ export function at(array, index) {
59
+ const len = array.length;
60
+ if (!len)
61
+ return void 0;
62
+ if (index < 0)
63
+ index += len;
64
+ return array[index];
65
+ }
66
+ export function range(...args) {
67
+ let start, stop, step;
68
+ if (args.length === 1) {
69
+ start = 0;
70
+ step = 1;
71
+ [stop] = args;
72
+ } else {
73
+ [start, stop, step = 1] = args;
74
+ }
75
+ const arr = [];
76
+ let current = start;
77
+ while (current < stop) {
78
+ arr.push(current);
79
+ current += step || 1;
80
+ }
81
+ return arr;
82
+ }
83
+ export function move(arr, from, to) {
84
+ arr.splice(to, 0, arr.splice(from, 1)[0]);
85
+ return arr;
86
+ }
87
+ export function clampArrayRange(n, arr) {
88
+ return clamp(n, 0, arr.length - 1);
89
+ }
90
+ export function sample(arr, count) {
91
+ return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
92
+ }
93
+ export function shuffle(array) {
94
+ for (let i = array.length - 1; i > 0; i--) {
95
+ const j = Math.floor(Math.random() * (i + 1));
96
+ [array[i], array[j]] = [array[j], array[i]];
97
+ }
98
+ return array;
99
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.39.1",
5
- "packageManager": "pnpm@7.15.0",
4
+ "version": "0.41.0",
5
+ "packageManager": "pnpm@7.16.0",
6
6
  "description": "The Stacks array utilities.",
7
7
  "author": "Chris Breuer",
8
8
  "license": "MIT",
@@ -36,10 +36,10 @@
36
36
  ],
37
37
  "engines": {
38
38
  "node": ">=v18.12.1",
39
- "pnpm": ">=7.15.0"
39
+ "pnpm": ">=7.16.0"
40
40
  },
41
41
  "devDependencies": {
42
- "mkdist": "^0.4.0",
42
+ "mkdist": "^1.0.0",
43
43
  "typescript": "^4.8.4"
44
44
  },
45
45
  "scripts": {