@stacksjs/arrays 0.59.11 → 0.61.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.js +15 -3
- package/package.json +1 -1
- package/src/contains.ts +3 -3
- package/src/helpers.ts +47 -26
- package/src/macro.ts +22 -4
- package/src/math.ts +19 -5
- package/dist/arr.d.ts +0 -3
- package/dist/contains.d.ts +0 -70
- package/dist/helpers.d.ts +0 -174
- package/dist/index.d.ts +0 -3
- package/dist/macro.d.ts +0 -94
- package/dist/math.d.ts +0 -171
package/dist/index.js
CHANGED
|
@@ -285,7 +285,16 @@ function avg(arr) {
|
|
|
285
285
|
return average(arr);
|
|
286
286
|
}
|
|
287
287
|
function median(arr) {
|
|
288
|
-
|
|
288
|
+
if (arr.length === 0)
|
|
289
|
+
throw new Error("Cannot compute median of an empty array");
|
|
290
|
+
const sorted = [...arr].sort((a, b) => a - b);
|
|
291
|
+
const mid = Math.floor(sorted.length / 2);
|
|
292
|
+
let medianValue;
|
|
293
|
+
if (sorted.length % 2 !== 0)
|
|
294
|
+
medianValue = sorted[mid];
|
|
295
|
+
else
|
|
296
|
+
medianValue = (sorted[mid] + sorted[mid - 1]) / 2;
|
|
297
|
+
return medianValue;
|
|
289
298
|
}
|
|
290
299
|
function mode(arr) {
|
|
291
300
|
return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop();
|
|
@@ -324,6 +333,8 @@ function interquartileRange(array) {
|
|
|
324
333
|
return q3 - q1;
|
|
325
334
|
}
|
|
326
335
|
function covariance(array1, array2) {
|
|
336
|
+
if (array1.length !== array2.length)
|
|
337
|
+
throw new Error("Arrays must have the same length");
|
|
327
338
|
const mean1 = average(array1);
|
|
328
339
|
const mean2 = average(array2);
|
|
329
340
|
return average(array1.map((num1, i) => (num1 - mean1) * (array2[i] - mean2)));
|
|
@@ -380,10 +391,11 @@ var Arr = {
|
|
|
380
391
|
return partition(array, filter);
|
|
381
392
|
},
|
|
382
393
|
random(arr, count = 1) {
|
|
383
|
-
return sample(arr, count);
|
|
394
|
+
return sample(arr, count).filter((item) => item != null);
|
|
384
395
|
},
|
|
385
396
|
sample(arr, count = 1) {
|
|
386
|
-
|
|
397
|
+
const sampleArr = sample(arr, count);
|
|
398
|
+
return sampleArr.filter((item) => item !== undefined);
|
|
387
399
|
},
|
|
388
400
|
unique(arr) {
|
|
389
401
|
return uniq(arr);
|
package/package.json
CHANGED
package/src/contains.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
12
|
export function contains(needle: string, haystack: string[]) {
|
|
13
|
-
return haystack.some(hay => needle.includes(hay))
|
|
13
|
+
return haystack.some((hay) => needle.includes(hay))
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -24,7 +24,7 @@ export function contains(needle: string, haystack: string[]) {
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
export function containsAll(needles: string[], haystack: string[]) {
|
|
27
|
-
return needles.every(needle => contains(needle, haystack))
|
|
27
|
+
return needles.every((needle) => contains(needle, haystack))
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -39,7 +39,7 @@ export function containsAll(needles: string[], haystack: string[]) {
|
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
41
|
export function containsAny(needles: string[], haystack: string[]) {
|
|
42
|
-
return needles.some(needle => contains(needle, haystack))
|
|
42
|
+
return needles.some((needle) => contains(needle, haystack))
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
package/src/helpers.ts
CHANGED
|
@@ -45,7 +45,7 @@ export function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>
|
|
|
45
45
|
* ```
|
|
46
46
|
*/
|
|
47
47
|
export function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
|
|
48
|
-
return args.flatMap(i => toArray(i))
|
|
48
|
+
return args.flatMap((i) => toArray(i))
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any
|
|
@@ -63,23 +63,51 @@ export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any
|
|
|
63
63
|
*/
|
|
64
64
|
export function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]]
|
|
65
65
|
export function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]]
|
|
66
|
-
export function partition<T>(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
export function partition<T>(
|
|
67
|
+
array: readonly T[],
|
|
68
|
+
f1: PartitionFilter<T>,
|
|
69
|
+
f2: PartitionFilter<T>,
|
|
70
|
+
f3: PartitionFilter<T>,
|
|
71
|
+
): [T[], T[], T[], T[]]
|
|
72
|
+
export function partition<T>(
|
|
73
|
+
array: readonly T[],
|
|
74
|
+
f1: PartitionFilter<T>,
|
|
75
|
+
f2: PartitionFilter<T>,
|
|
76
|
+
f3: PartitionFilter<T>,
|
|
77
|
+
f4: PartitionFilter<T>,
|
|
78
|
+
): [T[], T[], T[], T[], T[]]
|
|
79
|
+
export function partition<T>(
|
|
80
|
+
array: readonly T[],
|
|
81
|
+
f1: PartitionFilter<T>,
|
|
82
|
+
f2: PartitionFilter<T>,
|
|
83
|
+
f3: PartitionFilter<T>,
|
|
84
|
+
f4: PartitionFilter<T>,
|
|
85
|
+
f5: PartitionFilter<T>,
|
|
86
|
+
): [T[], T[], T[], T[], T[], T[]]
|
|
87
|
+
export function partition<T>(
|
|
88
|
+
array: readonly T[],
|
|
89
|
+
f1: PartitionFilter<T>,
|
|
90
|
+
f2: PartitionFilter<T>,
|
|
91
|
+
f3: PartitionFilter<T>,
|
|
92
|
+
f4: PartitionFilter<T>,
|
|
93
|
+
f5: PartitionFilter<T>,
|
|
94
|
+
f6: PartitionFilter<T>,
|
|
95
|
+
): [T[], T[], T[], T[], T[], T[], T[]]
|
|
70
96
|
export function partition<T>(array: readonly T[], ...filters: PartitionFilter<T>[]): any {
|
|
71
|
-
const result: T[][] = Array.from({ length: filters.length + 1 })
|
|
97
|
+
const result: T[][] = Array.from({ length: filters.length + 1 })
|
|
98
|
+
.fill(null)
|
|
99
|
+
.map(() => [])
|
|
72
100
|
|
|
73
101
|
array.forEach((e, idx, arr) => {
|
|
74
102
|
let i = 0
|
|
75
103
|
for (const filter of filters) {
|
|
76
104
|
if (filter(e, idx, arr)) {
|
|
77
|
-
result[i]
|
|
105
|
+
;(result[i] as T[]).push(e)
|
|
78
106
|
return
|
|
79
107
|
}
|
|
80
108
|
i += 1
|
|
81
109
|
}
|
|
82
|
-
result[i]
|
|
110
|
+
;(result[i] as T[]).push(e)
|
|
83
111
|
})
|
|
84
112
|
return result
|
|
85
113
|
}
|
|
@@ -122,8 +150,7 @@ export function unique<T>(array: readonly T[]): T[] {
|
|
|
122
150
|
export function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[] {
|
|
123
151
|
return array.reduce((acc: T[], cur: any) => {
|
|
124
152
|
const index = acc.findIndex((item: any) => equalFn(cur, item))
|
|
125
|
-
if (index === -1)
|
|
126
|
-
acc.push(cur)
|
|
153
|
+
if (index === -1) acc.push(cur)
|
|
127
154
|
return acc
|
|
128
155
|
}, [])
|
|
129
156
|
}
|
|
@@ -155,8 +182,7 @@ export function last<T>(array: readonly T[]): T | undefined {
|
|
|
155
182
|
* console.log(arr) // [1, 3]
|
|
156
183
|
*/
|
|
157
184
|
export function remove<T>(array: T[], value: T) {
|
|
158
|
-
if (!array)
|
|
159
|
-
return false
|
|
185
|
+
if (!array) return false
|
|
160
186
|
|
|
161
187
|
const index = array.indexOf(value)
|
|
162
188
|
if (index >= 0) {
|
|
@@ -183,11 +209,9 @@ export function at(array: readonly [], index: number): undefined
|
|
|
183
209
|
export function at<T>(array: readonly T[], index: number): T
|
|
184
210
|
export function at<T>(array: readonly T[] | [], index: number): T | undefined {
|
|
185
211
|
const len = array.length
|
|
186
|
-
if (!len)
|
|
187
|
-
return undefined
|
|
212
|
+
if (!len) return undefined
|
|
188
213
|
|
|
189
|
-
if (index < 0)
|
|
190
|
-
index += len
|
|
214
|
+
if (index < 0) index += len
|
|
191
215
|
|
|
192
216
|
return array[index]
|
|
193
217
|
}
|
|
@@ -210,17 +234,14 @@ export function at<T>(array: readonly T[] | [], index: number): T | undefined {
|
|
|
210
234
|
*/
|
|
211
235
|
export function move<T>(array: T[], from: number, to: number): T[] {
|
|
212
236
|
const len = array.length
|
|
213
|
-
if (!len)
|
|
214
|
-
return []
|
|
237
|
+
if (!len) return []
|
|
215
238
|
|
|
216
|
-
if (from < 0)
|
|
217
|
-
from += len
|
|
239
|
+
if (from < 0) from += len
|
|
218
240
|
|
|
219
|
-
if (to < 0)
|
|
220
|
-
to += len
|
|
241
|
+
if (to < 0) to += len
|
|
221
242
|
|
|
222
243
|
const item = array.splice(from, 1)[0]
|
|
223
|
-
array.splice(to, 0, item
|
|
244
|
+
array.splice(to, 0, item as T)
|
|
224
245
|
return array
|
|
225
246
|
}
|
|
226
247
|
|
|
@@ -251,7 +272,7 @@ export function clampArrayRange(arr: readonly unknown[], n: number) {
|
|
|
251
272
|
* ```
|
|
252
273
|
*/
|
|
253
274
|
export function sample<T>(arr: T[], count: number) {
|
|
254
|
-
return Array.from({ length: count }, _ => arr[Math.round(Math.random() * (arr.length - 1))])
|
|
275
|
+
return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))])
|
|
255
276
|
}
|
|
256
277
|
|
|
257
278
|
/**
|
|
@@ -265,8 +286,8 @@ export function sample<T>(arr: T[], count: number) {
|
|
|
265
286
|
*/
|
|
266
287
|
export function shuffle<T>(array: T[]): T[] {
|
|
267
288
|
for (let i = array.length - 1; i > 0; i--) {
|
|
268
|
-
const j = Math.floor(Math.random() * (i + 1))
|
|
269
|
-
[array[i]
|
|
289
|
+
const j = Math.floor(Math.random() * (i + 1))
|
|
290
|
+
;[array[i] as T, array[j] as T] = [array[j] as T, array[i] as T]
|
|
270
291
|
}
|
|
271
292
|
return array
|
|
272
293
|
}
|
package/src/macro.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
import type { Arrayable, Nullable } from '@stacksjs/types'
|
|
2
|
+
import { contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain } from './contains'
|
|
2
3
|
import type { PartitionFilter } from './helpers'
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
at,
|
|
6
|
+
clampArrayRange,
|
|
7
|
+
flatten,
|
|
8
|
+
last,
|
|
9
|
+
mergeArrayable,
|
|
10
|
+
move,
|
|
11
|
+
partition,
|
|
12
|
+
remove,
|
|
13
|
+
sample,
|
|
14
|
+
shuffle,
|
|
15
|
+
toArray,
|
|
16
|
+
uniq,
|
|
17
|
+
uniqueBy,
|
|
18
|
+
} from './helpers'
|
|
4
19
|
import { average, median, mode, range, sum } from './math'
|
|
5
|
-
import { contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain } from './contains'
|
|
6
20
|
|
|
7
21
|
export const Arr = {
|
|
8
22
|
contains(needle: string, haystack: string[]) {
|
|
@@ -49,14 +63,18 @@ export const Arr = {
|
|
|
49
63
|
* Returns a random item/s from the array
|
|
50
64
|
*/
|
|
51
65
|
random<T>(arr: T[], count = 1): T[] {
|
|
52
|
-
return sample(arr, count)
|
|
66
|
+
return sample(arr, count).filter((item): item is T => item != null)
|
|
53
67
|
},
|
|
54
68
|
|
|
55
69
|
/**
|
|
56
70
|
* Returns random item/s from the array
|
|
57
71
|
*/
|
|
58
72
|
sample<T>(arr: T[], count = 1): T[] {
|
|
59
|
-
|
|
73
|
+
// Get the sample array (may contain undefined values)
|
|
74
|
+
const sampleArr = sample(arr, count)
|
|
75
|
+
|
|
76
|
+
// Filter out any undefined values
|
|
77
|
+
return sampleArr.filter((item): item is T => item !== undefined)
|
|
60
78
|
},
|
|
61
79
|
|
|
62
80
|
unique<T>(arr: T[]): T[] {
|
package/src/math.ts
CHANGED
|
@@ -34,7 +34,18 @@ export function avg(arr: number[]): number {
|
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
36
|
export function median(arr: number[]): number {
|
|
37
|
-
|
|
37
|
+
if (arr.length === 0) throw new Error('Cannot compute median of an empty array')
|
|
38
|
+
|
|
39
|
+
const sorted = [...arr].sort((a, b) => a - b)
|
|
40
|
+
|
|
41
|
+
const mid = Math.floor(sorted.length / 2)
|
|
42
|
+
|
|
43
|
+
let medianValue: number
|
|
44
|
+
|
|
45
|
+
if (sorted.length % 2 !== 0) medianValue = sorted[mid] as number
|
|
46
|
+
else medianValue = ((sorted[mid] as number) + (sorted[mid - 1] as number)) / 2 // or (sorted[mid - 1] + sorted[mid]) / 2
|
|
47
|
+
|
|
48
|
+
return medianValue
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
/**
|
|
@@ -51,7 +62,7 @@ export function median(arr: number[]): number {
|
|
|
51
62
|
* ```
|
|
52
63
|
*/
|
|
53
64
|
export function mode(arr: number[]): number {
|
|
54
|
-
return arr.sort((a, b) => arr.filter(v => v === a).length - arr.filter(v => v === b).length).pop()
|
|
65
|
+
return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop() as number
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
/**
|
|
@@ -135,7 +146,7 @@ export function range(array: readonly number[]): number {
|
|
|
135
146
|
*/
|
|
136
147
|
export function variance(array: number[]): number {
|
|
137
148
|
const mean = average(array)
|
|
138
|
-
return average(array.map(num => (num - mean) ** 2))
|
|
149
|
+
return average(array.map((num) => (num - mean) ** 2))
|
|
139
150
|
}
|
|
140
151
|
|
|
141
152
|
/**
|
|
@@ -182,7 +193,7 @@ export function zScore(array: number[], num: number): number {
|
|
|
182
193
|
* @see https://en.wikipedia.org/wiki/Percentile
|
|
183
194
|
*/
|
|
184
195
|
export function percentile(array: number[], num: number): number {
|
|
185
|
-
return array.filter(n => n < num).length / array.length
|
|
196
|
+
return array.filter((n) => n < num).length / array.length
|
|
186
197
|
}
|
|
187
198
|
|
|
188
199
|
/**
|
|
@@ -214,7 +225,10 @@ export function interquartileRange(array: number[]): number {
|
|
|
214
225
|
* ```
|
|
215
226
|
*/
|
|
216
227
|
export function covariance(array1: number[], array2: number[]): number {
|
|
228
|
+
if (array1.length !== array2.length) throw new Error('Arrays must have the same length')
|
|
229
|
+
|
|
217
230
|
const mean1 = average(array1)
|
|
218
231
|
const mean2 = average(array2)
|
|
219
|
-
|
|
232
|
+
|
|
233
|
+
return average(array1.map((num1, i) => (num1 - mean1) * ((array2[i] as number) - mean2)))
|
|
220
234
|
}
|
package/dist/arr.d.ts
DELETED
package/dist/contains.d.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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;
|
package/dist/helpers.d.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
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[];
|
package/dist/index.d.ts
DELETED
package/dist/macro.d.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
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?: any): Array<T>;
|
|
11
|
-
flatten<T_1>(array?: any): 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?: any): Array<T>;
|
|
57
|
-
flatten<T_1>(array?: any): 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
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
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;
|