@stacksjs/arrays 0.58.73 → 0.59.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.
package/dist/arr.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './helpers';
2
+ export * from './math';
3
+ export * from './contains';
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Returns true if the needle is contained in the haystack.
3
+ *
4
+ * @param needle
5
+ * @param haystack
6
+ * @example
7
+ * ```ts
8
+ * contains('foo', ['foo', 'bar']) // true
9
+ * contains('foo', ['bar']) // false
10
+ * ```
11
+ */
12
+ export declare function contains(needle: string, haystack: string[]): boolean;
13
+ /**
14
+ * Returns true if all needles are contained in the haystack.
15
+ * @param needles
16
+ * @param haystack
17
+ * @example
18
+ * ```ts
19
+ * containsAll(['foo', 'bar'], ['foo', 'bar', 'baz']) // true
20
+ * containsAll(['foo', 'bar'], ['foo', 'baz']) // false
21
+ * ```
22
+ */
23
+ export declare function containsAll(needles: string[], haystack: string[]): boolean;
24
+ /**
25
+ * Returns true if any needle is contained in the haystack.
26
+ * @param needles
27
+ * @param haystack
28
+ * @example
29
+ * ```ts
30
+ * containsAny(['foo', 'bar'], ['foo', 'bar', 'baz']) // true
31
+ * containsAny(['foo', 'bar'], ['foo', 'baz']) // true
32
+ * containsAny(['foo', 'bar'], ['baz']) // false
33
+ * ```
34
+ */
35
+ export declare function containsAny(needles: string[], haystack: string[]): boolean;
36
+ /**
37
+ * Returns true if none of the needles are contained in the haystack.
38
+ * @param needles
39
+ * @param haystack
40
+ * @example
41
+ * ```ts
42
+ * containsNone(['foo', 'bar'], ['foo', 'bar', 'baz']) // false
43
+ * containsNone(['foo', 'bar'], ['foo', 'baz']) // false
44
+ * containsNone(['foo', 'bar'], ['baz']) // true
45
+ * ```
46
+ */
47
+ export declare function containsNone(needles: string[], haystack: string[]): boolean;
48
+ /**
49
+ * Returns true if all needles are contained in the haystack.
50
+ * @param needles
51
+ * @param haystack
52
+ * @example
53
+ * ```ts
54
+ * containsOnly(['foo', 'bar'], ['foo', 'bar', 'baz']) // false
55
+ * containsOnly(['foo', 'bar'], ['foo', 'baz']) // false
56
+ * containsOnly(['foo', 'bar'], ['foo', 'bar']) // true
57
+ * ```
58
+ */
59
+ export declare function containsOnly(needles: string[], haystack: string[]): boolean;
60
+ /**
61
+ * Returns true if the needle is not contained in the haystack.
62
+ * @param needle
63
+ * @param haystack
64
+ * @example
65
+ * ```ts
66
+ * doesNotContain('foo', ['foo', 'bar']) // false
67
+ * doesNotContain('foo', ['bar']) // true
68
+ * ```
69
+ */
70
+ export declare function doesNotContain(needle: string, haystack: string[]): boolean;
@@ -0,0 +1,174 @@
1
+ import type { Arrayable, Nullable } from '@stacksjs/types';
2
+ /**
3
+ * Convert `Arrayable<T>` to `Array<T>`
4
+ *
5
+ * @category Array
6
+ * @example
7
+ * ```ts
8
+ * toArray('foo') // ['foo']
9
+ * toArray(['foo']) // ['foo']
10
+ * toArray(null) // []
11
+ * toArray(undefined) // []
12
+ * toArray(1) // [1]
13
+ * toArray([1]) // [1]
14
+ * toArray({ foo: 'bar' }) // [{ foo: 'bar' }]
15
+ * toArray([{ foo: 'bar' }]) // [{ foo: 'bar' }]
16
+ * ```
17
+ */
18
+ export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
19
+ /**
20
+ * Flatten `Arrayable<T>` to `Array<T>`
21
+ *
22
+ * @category Array
23
+ * @example
24
+ * ```ts
25
+ * flatten([1, [2, [3, [4, [5]]]]]) // [1, 2, 3, 4, 5]
26
+ * ```
27
+ */
28
+ export declare function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
29
+ /**
30
+ * Use rest arguments to merge arrays
31
+ *
32
+ * @category Array
33
+ * @example
34
+ * ```ts
35
+ * mergeArrayable([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]
36
+ * ```
37
+ */
38
+ export declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
39
+ export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
40
+ /**
41
+ * Divide an array into two parts by a filter function
42
+ *
43
+ * @category Array
44
+ * @example
45
+ * ```ts
46
+ * const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
47
+ * console.log(odd) // [1, 3]
48
+ * console.log(even) // [2, 4]
49
+ * ```
50
+ */
51
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
52
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
53
+ export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
54
+ 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[]];
55
+ 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[]];
56
+ 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[]];
57
+ /**
58
+ * Unique an Array
59
+ *
60
+ * @category Array
61
+ * @example
62
+ * ```ts
63
+ * uniq([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
64
+ * ```
65
+ */
66
+ export declare function uniq<T>(array: readonly T[]): T[];
67
+ /**
68
+ * Unique an Array
69
+ *
70
+ * @param array
71
+ * @example
72
+ * ```ts
73
+ * unique([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
74
+ * ```
75
+ */
76
+ export declare function unique<T>(array: readonly T[]): T[];
77
+ /**
78
+ * Unique an Array by a custom equality function
79
+ *
80
+ * @category Array
81
+ * @example
82
+ * ```ts
83
+ * uniqueBy([1, 2, 3, 3, 2, 1], (a, b) => a === b) // [1, 2, 3]
84
+ * ```
85
+ */
86
+ export declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
87
+ /**
88
+ * Get last item
89
+ *
90
+ * @category Array
91
+ * @example
92
+ * ```ts
93
+ * last([1, 2, 3]) // 3
94
+ * ```
95
+ */
96
+ export declare function last(array: readonly []): undefined;
97
+ export declare function last<T>(array: readonly T[]): T;
98
+ /**
99
+ * Remove an item from Array
100
+ *
101
+ * @category Array
102
+ * @example
103
+ * ```ts
104
+ * const arr = [1, 2, 3]
105
+ * remove(arr, 2) // true
106
+ * Arr.remove(arr, 4) // false
107
+ * console.log(arr) // [1, 3]
108
+ */
109
+ export declare function remove<T>(array: T[], value: T): boolean;
110
+ /**
111
+ * Get nth item of Array. Negative for backward
112
+ *
113
+ * @category Array
114
+ * @example
115
+ * ```ts
116
+ * at([1, 2, 3], 1) // 2
117
+ * at([1, 2, 3], -1) // 3
118
+ * at([1, 2, 3], 3) // undefined
119
+ * at([1, 2, 3], -4) // undefined
120
+ * ```
121
+ */
122
+ export declare function at(array: readonly [], index: number): undefined;
123
+ export declare function at<T>(array: readonly T[], index: number): T;
124
+ /**
125
+ * Move an item from one index to another
126
+ * @param array
127
+ * @param from
128
+ * @param to
129
+ *
130
+ * @category Array
131
+ * @example
132
+ * ```ts
133
+ * move([1, 2, 3, 4], 0, 2) // [2, 3, 1, 4]
134
+ * move([1, 2, 3, 4], 0, -1) // [2, 3, 4, 1]
135
+ * move([1, 2, 3, 4], -1, 0) // [4, 1, 2, 3]
136
+ * move([1, 2, 3, 4], -1, -2) // [1, 4, 2, 3]
137
+ * move([1, 2, 3, 4], 1, 1) // [1, 2, 3, 4]
138
+ * ```
139
+ */
140
+ export declare function move<T>(array: T[], from: number, to: number): T[];
141
+ /**
142
+ * Clamp a number to the index range of an array.
143
+ *
144
+ * @category Array
145
+ * @example
146
+ * ```ts
147
+ * clampArrayRange([1, 2, 3], 0) // 0
148
+ * clampArrayRange([1, 2, 3], 1) // 1
149
+ * clampArrayRange([1, 2, 3], 2) // 2
150
+ * clampArrayRange([1, 2, 3], 3) // 2
151
+ * clampArrayRange([1, 2, 3], 4) // 2
152
+ * clampArrayRange([1, 2, 3], -1) // 0
153
+ */
154
+ export declare function clampArrayRange(arr: readonly unknown[], n: number): number;
155
+ /**
156
+ * Get random items from an array
157
+ *
158
+ * @category Array
159
+ * @example
160
+ * ```ts
161
+ * sample([1, 2, 3, 4], 2) // [2, 3]
162
+ * ```
163
+ */
164
+ export declare function sample<T>(arr: T[], count: number): (T | undefined)[];
165
+ /**
166
+ * Shuffle an array. This function mutates the array.
167
+ *
168
+ * @category Array
169
+ * @example
170
+ * ```ts
171
+ * shuffle([1, 2, 3, 4]) // [2, 4, 1, 3]
172
+ * ```
173
+ */
174
+ export declare function shuffle<T>(array: T[]): T[];
@@ -0,0 +1,3 @@
1
+ export * from './arr';
2
+ export * as arr from './arr';
3
+ export * from './macro';
@@ -0,0 +1,94 @@
1
+ import type { Arrayable, Nullable } from '@stacksjs/types';
2
+ import type { PartitionFilter } from './helpers';
3
+ export declare const Arr: {
4
+ contains(needle: string, haystack: string[]): boolean;
5
+ containsAll(needles: string[], haystack: string[]): boolean;
6
+ containsAny(needles: string[], haystack: string[]): boolean;
7
+ containsNone(needles: string[], haystack: string[]): boolean;
8
+ containsOnly(needles: string[], haystack: string[]): boolean;
9
+ doesNotContain(needle: string, haystack: string[]): boolean;
10
+ toArray<T>(array?: Nullable<Arrayable<T>>): T[];
11
+ flatten<T_1>(array?: Nullable<Arrayable<T_1 | T_1[]>>): T_1[];
12
+ mergeArrayable<T_2>(...args: Nullable<Arrayable<T_2>>[]): T_2[];
13
+ partition<T_3>(array: readonly T_3[], filter: PartitionFilter<T_3>): [T_3[], T_3[]];
14
+ /**
15
+ * Returns a random item/s from the array
16
+ */
17
+ random<T_4>(arr: T_4[], count?: number): T_4[];
18
+ /**
19
+ * Returns random item/s from the array
20
+ */
21
+ sample<T_5>(arr: T_5[], count?: number): T_5[];
22
+ unique<T_6>(arr: T_6[]): T_6[];
23
+ uniqueBy<T_7>(arr: readonly T_7[], equalFn: (a: any, b: any) => boolean): T_7[];
24
+ last<T_8>(arr: T_8[]): T_8 | undefined;
25
+ remove<T_9>(arr: T_9[], value: T_9): boolean;
26
+ at(arr: any[], index: number): any;
27
+ range(arr: readonly number[]): number;
28
+ move<T_10>(arr: T_10[], from: number, to: number): T_10[];
29
+ clampArrayRange(arr: readonly unknown[], n: number): number;
30
+ shuffle<T_11>(arr: T_11[]): T_11[];
31
+ /**
32
+ * Returns the sum of all items in the array
33
+ */
34
+ sum(arr: number[]): number;
35
+ /**
36
+ * Returns the average of all items in the array
37
+ */
38
+ average(arr: number[]): number;
39
+ avg(arr: number[]): number;
40
+ /**
41
+ * Returns the median of all items in the array
42
+ */
43
+ median(arr: number[]): number;
44
+ /**
45
+ * Returns the mode of all items in the array
46
+ */
47
+ mode(arr: number[]): number;
48
+ };
49
+ export declare const arr: {
50
+ contains(needle: string, haystack: string[]): boolean;
51
+ containsAll(needles: string[], haystack: string[]): boolean;
52
+ containsAny(needles: string[], haystack: string[]): boolean;
53
+ containsNone(needles: string[], haystack: string[]): boolean;
54
+ containsOnly(needles: string[], haystack: string[]): boolean;
55
+ doesNotContain(needle: string, haystack: string[]): boolean;
56
+ toArray<T>(array?: Nullable<Arrayable<T>>): T[];
57
+ flatten<T_1>(array?: Nullable<Arrayable<T_1 | T_1[]>>): T_1[];
58
+ mergeArrayable<T_2>(...args: Nullable<Arrayable<T_2>>[]): T_2[];
59
+ partition<T_3>(array: readonly T_3[], filter: PartitionFilter<T_3>): [T_3[], T_3[]];
60
+ /**
61
+ * Returns a random item/s from the array
62
+ */
63
+ random<T_4>(arr: T_4[], count?: number): T_4[];
64
+ /**
65
+ * Returns random item/s from the array
66
+ */
67
+ sample<T_5>(arr: T_5[], count?: number): T_5[];
68
+ unique<T_6>(arr: T_6[]): T_6[];
69
+ uniqueBy<T_7>(arr: readonly T_7[], equalFn: (a: any, b: any) => boolean): T_7[];
70
+ last<T_8>(arr: T_8[]): T_8 | undefined;
71
+ remove<T_9>(arr: T_9[], value: T_9): boolean;
72
+ at(arr: any[], index: number): any;
73
+ range(arr: readonly number[]): number;
74
+ move<T_10>(arr: T_10[], from: number, to: number): T_10[];
75
+ clampArrayRange(arr: readonly unknown[], n: number): number;
76
+ shuffle<T_11>(arr: T_11[]): T_11[];
77
+ /**
78
+ * Returns the sum of all items in the array
79
+ */
80
+ sum(arr: number[]): number;
81
+ /**
82
+ * Returns the average of all items in the array
83
+ */
84
+ average(arr: number[]): number;
85
+ avg(arr: number[]): number;
86
+ /**
87
+ * Returns the median of all items in the array
88
+ */
89
+ median(arr: number[]): number;
90
+ /**
91
+ * Returns the mode of all items in the array
92
+ */
93
+ mode(arr: number[]): number;
94
+ };
package/dist/math.d.ts ADDED
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Returns the average of an array of numbers
3
+ * @param arr
4
+ * @category Array
5
+ * @example
6
+ * ```ts
7
+ * average([1, 2, 3, 4]) // 2.5
8
+ * ```
9
+ */
10
+ export declare function average(arr: number[]): number;
11
+ /**
12
+ * Returns the average of an array of numbers
13
+ * @param arr
14
+ * @category Array
15
+ * @example
16
+ * ```ts
17
+ * avg([1, 2, 3, 4]) // 2.5
18
+ * ```
19
+ */
20
+ export declare function avg(arr: number[]): number;
21
+ /**
22
+ * Returns the median of an array of numbers
23
+ * @param arr
24
+ * @category Array
25
+ * @example
26
+ * ```ts
27
+ * median([1, 2, 3, 4]) // 2.5
28
+ * ```
29
+ */
30
+ export declare function median(arr: number[]): number;
31
+ /**
32
+ * Returns the mode of an array of numbers
33
+ * @param arr
34
+ * @category Array
35
+ * @example
36
+ * ```ts
37
+ * mode([1, 2, 3, 4]) // 1
38
+ * mode([1, 2, 2, 3, 4]) // 2
39
+ * mode([1, 2, 2, 3, 3, 4]) // 2
40
+ * mode([1, 2, 2, 3, 3, 4, 4]) // 2
41
+ * mode([1, 2, 2, 3, 3, 4, 4, 4]) // 4
42
+ * ```
43
+ */
44
+ export declare function mode(arr: number[]): number;
45
+ /**
46
+ * Returns the sum of an array of numbers
47
+ * @param array
48
+ * @category Array
49
+ * @example
50
+ * ```ts
51
+ * sum([1, 2, 3, 4]) // 10
52
+ * ```
53
+ */
54
+ export declare function sum(array: readonly number[]): number;
55
+ /**
56
+ * Returns the product of an array of numbers
57
+ * @param array
58
+ * @category Array
59
+ * @example
60
+ * ```ts
61
+ * product([1, 2, 3, 4]) // 24
62
+ * ```
63
+ */
64
+ export declare function product(array: readonly number[]): number;
65
+ /**
66
+ * Returns the minimum value of an array of numbers
67
+ * @param array
68
+ * @category Array
69
+ * @example
70
+ * ```ts
71
+ * min([1, 2, 3, 4]) // 1
72
+ * min([1, 2, 3, 4, -1]) // -1
73
+ * ```
74
+ */
75
+ export declare function min(array: readonly number[]): number;
76
+ /**
77
+ * Returns the maximum value of an array of numbers
78
+ * @param array
79
+ * @category Array
80
+ * @example
81
+ * ```ts
82
+ * max([1, 2, 3, 4]) // 4
83
+ * max([1, 2, 3, 4, -1]) // 4
84
+ * ```
85
+ */
86
+ export declare function max(array: readonly number[]): number;
87
+ /**
88
+ * Returns the range of an array of numbers
89
+ * @param array
90
+ * @category Array
91
+ * @example
92
+ * ```ts
93
+ * range([1, 2, 3, 4]) // 3
94
+ * range([1, 2, 3, 4, -1]) // 5
95
+ * range([1, 2, 3, 4, -1, 10]) // 11
96
+ * ```
97
+ */
98
+ export declare function range(array: readonly number[]): number;
99
+ /**
100
+ * Returns the variance of an array of numbers
101
+ * @param array
102
+ * @category Array
103
+ * @example
104
+ * ```ts
105
+ * variance([1, 2, 3, 4]) // 1.25
106
+ * ```
107
+ * @see https://en.wikipedia.org/wiki/Variance
108
+ */
109
+ export declare function variance(array: number[]): number;
110
+ /**
111
+ * Returns the standard deviation of an array of numbers
112
+ * @param array
113
+ * @category Array
114
+ * @example
115
+ * ```ts
116
+ * standardDeviation([1, 2, 3, 4]) // 1.118033988749895
117
+ * ```
118
+ * @see https://en.wikipedia.org/wiki/Standard_deviation
119
+ * @see https://stackoverflow.com/questions/7343890/standard-deviation-javascript
120
+ */
121
+ export declare function standardDeviation(array: number[]): number;
122
+ /**
123
+ * Returns the z-score of a number in an array of numbers
124
+ * @param array
125
+ * @param num
126
+ * @category Array
127
+ * @example
128
+ * ```ts
129
+ * zScore([1, 2, 3, 4], 2) // 0
130
+ * zScore([1, 2, 3, 4], 3) // 0.7071067811865475
131
+ * ```
132
+ * @see https://en.wikipedia.org/wiki/Standard_score
133
+ */
134
+ export declare function zScore(array: number[], num: number): number;
135
+ /**
136
+ * Returns the percentile of a number in an array of numbers
137
+ * @param array
138
+ * @param num
139
+ * @category Array
140
+ * @example
141
+ * ```ts
142
+ * percentile([1, 2, 3, 4], 2) // 0.25
143
+ * percentile([1, 2, 3, 4], 3) // 0.75
144
+ * ```
145
+ * @see https://en.wikipedia.org/wiki/Percentile
146
+ */
147
+ export declare function percentile(array: number[], num: number): number;
148
+ /**
149
+ * Returns the interquartile range of an array of numbers
150
+ * @param array
151
+ * @category Array
152
+ * @example
153
+ * ```ts
154
+ * interquartileRange([1, 2, 3, 4]) // 1.5
155
+ * ```
156
+ * @see https://en.wikipedia.org/wiki/Interquartile_range
157
+ * @see https://stackoverflow.com/questions/48719873/how-to-calculate-interquartile-range-in-javascript
158
+ */
159
+ export declare function interquartileRange(array: number[]): number;
160
+ /**
161
+ * Returns the covariance of two arrays of numbers
162
+ * @param array1
163
+ * @param array2
164
+ * @category Array
165
+ * @example
166
+ * ```ts
167
+ * covariance([1, 2, 3, 4], [1, 2, 3, 4]) // 1.25
168
+ * covariance([1, 2, 3, 4], [4, 3, 2, 1]) // -1.25
169
+ * ```
170
+ */
171
+ export declare function covariance(array1: number[], array2: number[]): number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.58.73",
4
+ "version": "0.59.2",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",