@stacksjs/arrays 0.70.88 → 0.70.91

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 './contains';
2
+ export * from './helpers';
3
+ export * from './math';
package/dist/arr.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./contains";
2
+ export * from "./helpers";
3
+ export * from "./math";
@@ -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,18 @@
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
+ }
@@ -0,0 +1,178 @@
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 | T[]>>): 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
+ /**
40
+ * Divide an array into two parts by a filter function
41
+ *
42
+ * @category Array
43
+ * @example
44
+ * ```ts
45
+ * const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
46
+ * console.log(odd) // [1, 3]
47
+ * console.log(even) // [2, 4]
48
+ * ```
49
+ */
50
+ export declare function partition<T>(array: readonly T[], _f1: PartitionFilter<T>): [T[], T[]];
51
+ export declare function partition<T>(array: readonly T[], _f1: PartitionFilter<T>, _f2: PartitionFilter<T>): [T[], T[], T[]];
52
+ export declare function partition<T>(array: readonly T[], _f1: PartitionFilter<T>, _f2: PartitionFilter<T>, _f3: PartitionFilter<T>): [T[], T[], T[], T[]];
53
+ 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[]];
54
+ 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[]];
55
+ 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[]];
56
+ /**
57
+ * Unique an Array
58
+ *
59
+ * @category Array
60
+ * @example
61
+ * ```ts
62
+ * uniq([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
63
+ * ```
64
+ */
65
+ export declare function uniq<T>(array: readonly T[]): T[];
66
+ /**
67
+ * Unique an Array
68
+ *
69
+ * @param array
70
+ * @example
71
+ * ```ts
72
+ * unique([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
73
+ * ```
74
+ */
75
+ export declare function unique<T>(array: readonly T[]): T[];
76
+ /**
77
+ * Unique an Array by a custom equality function
78
+ *
79
+ * @category Array
80
+ * @example
81
+ * ```ts
82
+ * uniqueBy([1, 2, 3, 3, 2, 1], (a, b) => a === b) // [1, 2, 3]
83
+ * ```
84
+ */
85
+ export declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[];
86
+ /**
87
+ * Get last item
88
+ *
89
+ * @category Array
90
+ * @example
91
+ * ```ts
92
+ * last([1, 2, 3]) // 3
93
+ * ```
94
+ */
95
+ export declare function last(array: readonly []): undefined;
96
+ export declare function last<T>(array: readonly T[]): T;
97
+ /**
98
+ * Remove an item from Array.
99
+ *
100
+ * **Mutates the input array** via splice — the original reference shrinks
101
+ * by one element. If you need a non-destructive version, use
102
+ * `array.filter(x => x !== value)` instead.
103
+ *
104
+ * @category Array
105
+ * @example
106
+ * ```ts
107
+ * const arr = [1, 2, 3]
108
+ * remove(arr, 2) // true
109
+ * Arr.remove(arr, 4) // false
110
+ * console.log(arr) // [1, 3]
111
+ */
112
+ export declare function remove<T>(array: T[], value: T): boolean;
113
+ /**
114
+ * Get nth item of Array. Negative for backward
115
+ *
116
+ * @category Array
117
+ * @example
118
+ * ```ts
119
+ * at([1, 2, 3], 1) // 2
120
+ * at([1, 2, 3], -1) // 3
121
+ * at([1, 2, 3], 3) // undefined
122
+ * at([1, 2, 3], -4) // undefined
123
+ * ```
124
+ */
125
+ export declare function at(array: readonly [], index: number): undefined;
126
+ export declare function at<T>(array: readonly T[], index: number): T;
127
+ /**
128
+ * Move an item from one index to another
129
+ * @param array
130
+ * @param from
131
+ * @param to
132
+ *
133
+ * @category Array
134
+ * @example
135
+ * ```ts
136
+ * move([1, 2, 3, 4], 0, 2) // [2, 3, 1, 4]
137
+ * move([1, 2, 3, 4], 0, -1) // [2, 3, 4, 1]
138
+ * move([1, 2, 3, 4], -1, 0) // [4, 1, 2, 3]
139
+ * move([1, 2, 3, 4], -1, -2) // [1, 4, 2, 3]
140
+ * move([1, 2, 3, 4], 1, 1) // [1, 2, 3, 4]
141
+ * ```
142
+ */
143
+ export declare function move<T>(array: T[], from: number, to: number): T[];
144
+ /**
145
+ * Clamp a number to the index range of an array.
146
+ *
147
+ * @category Array
148
+ * @example
149
+ * ```ts
150
+ * clampArrayRange([1, 2, 3], 0) // 0
151
+ * clampArrayRange([1, 2, 3], 1) // 1
152
+ * clampArrayRange([1, 2, 3], 2) // 2
153
+ * clampArrayRange([1, 2, 3], 3) // 2
154
+ * clampArrayRange([1, 2, 3], 4) // 2
155
+ * clampArrayRange([1, 2, 3], -1) // 0
156
+ */
157
+ export declare function clampArrayRange(arr: readonly unknown[], n: number): number;
158
+ /**
159
+ * Get random items from an array
160
+ *
161
+ * @category Array
162
+ * @example
163
+ * ```ts
164
+ * sample([1, 2, 3, 4], 2) // [2, 3]
165
+ * ```
166
+ */
167
+ export declare function sample<T>(arr: T[], count: number): T[];
168
+ /**
169
+ * Shuffle an array. This function mutates the array.
170
+ *
171
+ * @category Array
172
+ * @example
173
+ * ```ts
174
+ * shuffle([1, 2, 3, 4]) // [2, 4, 1, 3]
175
+ * ```
176
+ */
177
+ export declare function shuffle<T>(array: T[]): T[];
178
+ export type PartitionFilter<T> = (_i: T, _idx: number, _arr: readonly T[]) => any;
@@ -0,0 +1,85 @@
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).reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []);
8
+ }
9
+ export function mergeArrayable(...args) {
10
+ return args.flatMap((i) => toArray(i));
11
+ }
12
+ export function partition(array, ...filters) {
13
+ const result = Array.from({ length: 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
+ if (acc.findIndex((item) => equalFn(cur, item)) === -1)
36
+ acc.push(cur);
37
+ return acc;
38
+ }, []);
39
+ }
40
+ export function last(array) {
41
+ return at(array, -1);
42
+ }
43
+ export function remove(array, value) {
44
+ if (!array)
45
+ return !1;
46
+ const index = array.indexOf(value);
47
+ if (index >= 0) {
48
+ array.splice(index, 1);
49
+ return !0;
50
+ }
51
+ return !1;
52
+ }
53
+ export function at(array, index) {
54
+ const len = array.length;
55
+ if (!len)
56
+ return;
57
+ if (index < 0)
58
+ index += len;
59
+ return array[index];
60
+ }
61
+ export function move(array, from, to) {
62
+ const len = array.length;
63
+ if (!len)
64
+ return [];
65
+ if (from < 0)
66
+ from += len;
67
+ if (to < 0)
68
+ to += len;
69
+ const item = array.splice(from, 1)[0];
70
+ array.splice(to, 0, item);
71
+ return array;
72
+ }
73
+ export function clampArrayRange(arr, n) {
74
+ return clamp(n, 0, arr.length - 1);
75
+ }
76
+ export function sample(arr, count) {
77
+ return Array.from({ length: count }, () => arr[Math.floor(Math.random() * arr.length)]);
78
+ }
79
+ export function shuffle(array) {
80
+ for (let i = array.length - 1;i > 0; i--) {
81
+ const j = Math.floor(Math.random() * (i + 1));
82
+ [array[i], array[j]] = [array[j], array[i]];
83
+ }
84
+ return array;
85
+ }
@@ -0,0 +1,3 @@
1
+ export * from './arr';
2
+ export * as arr from './arr';
3
+ export * from './macro';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./arr";
2
+ export * as arr from "./arr";
3
+ export * from "./macro";
@@ -0,0 +1,48 @@
1
+ /** @defaultValue `{ random<T>: () => unknown, uniqueBy<T>: () => unknown }` */
2
+ export declare const Arr: {
3
+ contains: (needle: string, haystack: string[]) => boolean;
4
+ containsAll: (needles: string[], haystack: string[]) => boolean;
5
+ containsAny: (needles: string[], haystack: string[]) => boolean;
6
+ containsNone: (needles: string[], haystack: string[]) => boolean;
7
+ containsOnly: (needles: string[], haystack: string[]) => boolean;
8
+ doesNotContain: (needle: string, haystack: string[]) => boolean;
9
+ toArray<T>: (array?: Nullable<Arrayable<T>>) => Array<T>;
10
+ flatten<T>: (array?: Nullable<Arrayable<T | T[]>>) => T[];
11
+ mergeArrayable<T>: (...args: Nullable<Arrayable<T>>[]) => Array<T>;
12
+ partition<T>: (array: readonly T[], filter: PartitionFilter<T>) => [T[], T[]];
13
+ /**
14
+ * Returns a random item/s from the array
15
+ */
16
+ random<T>: (arr: T[], count?: number) => T[];
17
+ /**
18
+ * Returns random item/s from the array
19
+ */
20
+ sample<T>: (arr: T[], count?: number) => T[];
21
+ unique<T>: (arr: T[]) => T[];
22
+ uniqueBy<T>: (arr: readonly T[], equalFn: (a: any, b: any) => boolean) => T[];
23
+ last<T>: (arr: T[]) => T | undefined;
24
+ remove<T>: (arr: T[], value: T) => boolean;
25
+ at: (arr: any[], index: number) => any;
26
+ range: (arr: readonly number[]) => number;
27
+ move<T>: (arr: T[], from: number, to: number) => T[];
28
+ clampArrayRange: (arr: readonly unknown[], n: number) => number;
29
+ shuffle<T>: (arr: T[]) => T[];
30
+ /**
31
+ * Returns the sum of all items in the array
32
+ */
33
+ sum: (arr: number[]) => number;
34
+ /**
35
+ * Returns the average of all items in the array
36
+ */
37
+ average: (arr: number[]) => number;
38
+ avg: (arr: number[]) => number;
39
+ /**
40
+ * Returns the median of all items in the array
41
+ */
42
+ median: (arr: number[]) => number;
43
+ /**
44
+ * Returns the mode of all items in the array
45
+ */
46
+ mode: (arr: number[]) => number
47
+ };
48
+ export declare const arr: typeof Arr;
package/dist/macro.js ADDED
@@ -0,0 +1,97 @@
1
+ import { contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain } from "./contains";
2
+ import {
3
+ at,
4
+ clampArrayRange,
5
+ flatten,
6
+ last,
7
+ mergeArrayable,
8
+ move,
9
+ partition,
10
+ remove,
11
+ sample,
12
+ shuffle,
13
+ toArray,
14
+ uniq,
15
+ uniqueBy
16
+ } from "./helpers";
17
+ import { average, median, mode, range, sum } from "./math";
18
+ export const Arr = {
19
+ contains(needle, haystack) {
20
+ return contains(needle, haystack);
21
+ },
22
+ containsAll(needles, haystack) {
23
+ return containsAll(needles, haystack);
24
+ },
25
+ containsAny(needles, haystack) {
26
+ return containsAny(needles, haystack);
27
+ },
28
+ containsNone(needles, haystack) {
29
+ return containsNone(needles, haystack);
30
+ },
31
+ containsOnly(needles, haystack) {
32
+ return containsOnly(needles, haystack);
33
+ },
34
+ doesNotContain(needle, haystack) {
35
+ return doesNotContain(needle, haystack);
36
+ },
37
+ toArray(array) {
38
+ return toArray(array);
39
+ },
40
+ flatten(array) {
41
+ return flatten(array);
42
+ },
43
+ mergeArrayable(...args) {
44
+ return mergeArrayable(...args);
45
+ },
46
+ partition(array, filter) {
47
+ return partition(array, filter);
48
+ },
49
+ random(arr, count = 1) {
50
+ return sample(arr, count).filter((item) => item != null);
51
+ },
52
+ sample(arr, count = 1) {
53
+ return sample(arr, count);
54
+ },
55
+ unique(arr) {
56
+ return uniq(arr);
57
+ },
58
+ uniqueBy(arr, equalFn) {
59
+ return uniqueBy(arr, equalFn);
60
+ },
61
+ last(arr) {
62
+ return last(arr);
63
+ },
64
+ remove(arr, value) {
65
+ return remove(arr, value);
66
+ },
67
+ at(arr, index) {
68
+ return at(arr, index);
69
+ },
70
+ range(arr) {
71
+ return range(arr);
72
+ },
73
+ move(arr, from, to) {
74
+ return move(arr, from, to);
75
+ },
76
+ clampArrayRange(arr, n) {
77
+ return clampArrayRange(arr, n);
78
+ },
79
+ shuffle(arr) {
80
+ return shuffle(arr);
81
+ },
82
+ sum(arr) {
83
+ return sum(arr);
84
+ },
85
+ average(arr) {
86
+ return average(arr);
87
+ },
88
+ avg(arr) {
89
+ return average(arr);
90
+ },
91
+ median(arr) {
92
+ return median(arr);
93
+ },
94
+ mode(arr) {
95
+ return mode(arr);
96
+ }
97
+ }, arr = Arr;
package/dist/math.d.ts ADDED
@@ -0,0 +1,177 @@
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
+ *
58
+ * Throws when an intermediate product exceeds `Number.MAX_SAFE_INTEGER` —
59
+ * silently returning a precision-lossy float made downstream comparisons
60
+ * (e.g. inventory totals, distance calcs) lie without any signal that
61
+ * the result was unreliable.
62
+ *
63
+ * @param array
64
+ * @category Array
65
+ * @example
66
+ * ```ts
67
+ * product([1, 2, 3, 4]) // 24
68
+ * ```
69
+ */
70
+ export declare function product(array: readonly number[]): number;
71
+ /**
72
+ * Returns the minimum value of an array of numbers
73
+ * @param array
74
+ * @category Array
75
+ * @example
76
+ * ```ts
77
+ * min([1, 2, 3, 4]) // 1
78
+ * min([1, 2, 3, 4, -1]) // -1
79
+ * ```
80
+ */
81
+ export declare function min(array: readonly number[]): number;
82
+ /**
83
+ * Returns the maximum value of an array of numbers
84
+ * @param array
85
+ * @category Array
86
+ * @example
87
+ * ```ts
88
+ * max([1, 2, 3, 4]) // 4
89
+ * max([1, 2, 3, 4, -1]) // 4
90
+ * ```
91
+ */
92
+ export declare function max(array: readonly number[]): number;
93
+ /**
94
+ * Returns the range of an array of numbers
95
+ * @param array
96
+ * @category Array
97
+ * @example
98
+ * ```ts
99
+ * range([1, 2, 3, 4]) // 3
100
+ * range([1, 2, 3, 4, -1]) // 5
101
+ * range([1, 2, 3, 4, -1, 10]) // 11
102
+ * ```
103
+ */
104
+ export declare function range(array: readonly number[]): number;
105
+ /**
106
+ * Returns the variance of an array of numbers
107
+ * @param array
108
+ * @category Array
109
+ * @example
110
+ * ```ts
111
+ * variance([1, 2, 3, 4]) // 1.25
112
+ * ```
113
+ * @see https://en.wikipedia.org/wiki/Variance
114
+ */
115
+ export declare function variance(array: number[]): number;
116
+ /**
117
+ * Returns the standard deviation of an array of numbers
118
+ * @param array
119
+ * @category Array
120
+ * @example
121
+ * ```ts
122
+ * standardDeviation([1, 2, 3, 4]) // 1.118033988749895
123
+ * ```
124
+ * @see https://en.wikipedia.org/wiki/Standard_deviation
125
+ * @see https://stackoverflow.com/questions/7343890/standard-deviation-javascript
126
+ */
127
+ export declare function standardDeviation(array: number[]): number;
128
+ /**
129
+ * Returns the z-score of a number in an array of numbers
130
+ * @param array
131
+ * @param num
132
+ * @category Array
133
+ * @example
134
+ * ```ts
135
+ * zScore([1, 2, 3, 4], 2) // 0
136
+ * zScore([1, 2, 3, 4], 3) // 0.7071067811865475
137
+ * ```
138
+ * @see https://en.wikipedia.org/wiki/Standard_score
139
+ */
140
+ export declare function zScore(array: number[], num: number): number;
141
+ /**
142
+ * Returns the percentile of a number in an array of numbers
143
+ * @param array
144
+ * @param num
145
+ * @category Array
146
+ * @example
147
+ * ```ts
148
+ * percentile([1, 2, 3, 4], 2) // 0.25
149
+ * percentile([1, 2, 3, 4], 3) // 0.75
150
+ * ```
151
+ * @see https://en.wikipedia.org/wiki/Percentile
152
+ */
153
+ export declare function percentile(array: number[], num: number): number;
154
+ /**
155
+ * Returns the interquartile range of an array of numbers
156
+ * @param array
157
+ * @category Array
158
+ * @example
159
+ * ```ts
160
+ * interquartileRange([1, 2, 3, 4]) // 1.5
161
+ * ```
162
+ * @see https://en.wikipedia.org/wiki/Interquartile_range
163
+ * @see https://stackoverflow.com/questions/48719873/how-to-calculate-interquartile-range-in-javascript
164
+ */
165
+ export declare function interquartileRange(array: number[]): number;
166
+ /**
167
+ * Returns the covariance of two arrays of numbers
168
+ * @param array1
169
+ * @param array2
170
+ * @category Array
171
+ * @example
172
+ * ```ts
173
+ * covariance([1, 2, 3, 4], [1, 2, 3, 4]) // 1.25
174
+ * covariance([1, 2, 3, 4], [4, 3, 2, 1]) // -1.25
175
+ * ```
176
+ */
177
+ export declare function covariance(array1: number[], array2: number[]): number;
package/dist/math.js ADDED
@@ -0,0 +1,84 @@
1
+ export function average(arr) {
2
+ if (arr.length === 0)
3
+ throw Error("Cannot compute average of an empty array");
4
+ return sum(arr) / arr.length;
5
+ }
6
+ export function avg(arr) {
7
+ return average(arr);
8
+ }
9
+ export function median(arr) {
10
+ if (arr.length === 0)
11
+ throw Error("Cannot compute median of an empty array");
12
+ const sorted = [...arr].sort((a, b) => a - b), mid = Math.floor(sorted.length / 2);
13
+ let medianValue;
14
+ if (sorted.length % 2 !== 0)
15
+ medianValue = sorted[mid];
16
+ else
17
+ medianValue = (sorted[mid] + sorted[mid - 1]) / 2;
18
+ return medianValue;
19
+ }
20
+ export function mode(arr) {
21
+ if (arr.length === 0)
22
+ throw Error("Cannot compute mode of an empty array");
23
+ const counts = new Map;
24
+ for (const v of arr)
25
+ counts.set(v, (counts.get(v) || 0) + 1);
26
+ let maxCount = 0, result = arr[0];
27
+ for (const [value, count] of counts)
28
+ if (count > maxCount) {
29
+ maxCount = count;
30
+ result = value;
31
+ }
32
+ return result;
33
+ }
34
+ export function sum(array) {
35
+ return array.reduce((acc, cur) => acc + cur, 0);
36
+ }
37
+ export function product(array) {
38
+ let acc = 1;
39
+ for (const cur of array) {
40
+ acc *= cur;
41
+ if (Number.isFinite(acc) && Math.abs(acc) > Number.MAX_SAFE_INTEGER)
42
+ throw RangeError(`[arrays.product] Result exceeds Number.MAX_SAFE_INTEGER (${Number.MAX_SAFE_INTEGER}); use BigInt for large products.`);
43
+ }
44
+ return acc;
45
+ }
46
+ export function min(array) {
47
+ if (array.length === 0)
48
+ throw Error("Cannot compute min of an empty array");
49
+ return Math.min(...array);
50
+ }
51
+ export function max(array) {
52
+ if (array.length === 0)
53
+ throw Error("Cannot compute max of an empty array");
54
+ return Math.max(...array);
55
+ }
56
+ export function range(array) {
57
+ return max(array) - min(array);
58
+ }
59
+ export function variance(array) {
60
+ const mean = average(array);
61
+ return average(array.map((num) => (num - mean) ** 2));
62
+ }
63
+ export function standardDeviation(array) {
64
+ return Math.sqrt(variance(array));
65
+ }
66
+ export function zScore(array, num) {
67
+ return (num - average(array)) / standardDeviation(array);
68
+ }
69
+ export function percentile(array, num) {
70
+ const sorted = [...array].sort((a, b) => a - b), index = num / 100 * (sorted.length - 1), lower = Math.floor(index), upper = Math.ceil(index), weight = index - lower;
71
+ if (upper === lower)
72
+ return sorted[index] ?? 0;
73
+ return (1 - weight) * (sorted[lower] ?? 0) + weight * (sorted[upper] ?? 0);
74
+ }
75
+ export function interquartileRange(array) {
76
+ const q1 = median(array.slice(0, Math.floor(array.length / 2)));
77
+ return median(array.slice(Math.ceil(array.length / 2))) - q1;
78
+ }
79
+ export function covariance(array1, array2) {
80
+ if (array1.length !== array2.length)
81
+ throw Error("Arrays must have the same length");
82
+ const mean1 = average(array1), mean2 = average(array2);
83
+ return average(array1.map((num1, i) => (num1 - mean1) * (array2[i] - mean2)));
84
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.70.88",
4
+ "version": "0.70.91",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": [
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "better-dx": "^0.2.16",
58
- "@stacksjs/utils": "0.70.88"
58
+ "@stacksjs/utils": "0.70.91"
59
59
  },
60
60
  "sideEffects": false
61
61
  }