@stacksjs/arrays 0.70.23 → 0.70.25

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 CHANGED
@@ -1,2 +1,2 @@
1
1
  // @bun
2
- var v=Object.defineProperty;var h=(z,E)=>{for(var G in E)v(z,G,{get:E[G],enumerable:!0,configurable:!0,set:(H)=>E[G]=()=>H})};var f={};h(f,{zScore:()=>i,variance:()=>x,uniqueBy:()=>C,unique:()=>u,uniq:()=>Y,toArray:()=>P,sum:()=>$,standardDeviation:()=>g,shuffle:()=>w,sample:()=>_,remove:()=>F,range:()=>b,product:()=>d,percentile:()=>c,partition:()=>j,move:()=>N,mode:()=>q,min:()=>p,mergeArrayable:()=>T,median:()=>Q,max:()=>A,last:()=>V,interquartileRange:()=>l,flatten:()=>X,doesNotContain:()=>S,covariance:()=>s,containsOnly:()=>O,containsNone:()=>D,containsAny:()=>W,containsAll:()=>U,contains:()=>M,clampArrayRange:()=>R,avg:()=>o,average:()=>J,at:()=>Z});function M(z,E){return E.some((G)=>z.includes(G))}function U(z,E){return z.every((G)=>M(G,E))}function W(z,E){return z.some((G)=>M(G,E))}function D(z,E){return!W(z,E)}function O(z,E){return U(E,z)}function S(z,E){return!M(z,E)}import{clamp as m}from"@stacksjs/utils";function P(z){return z=z??[],Array.isArray(z)?z:[z]}function X(z){return P(z).reduce((E,G)=>E.concat(Array.isArray(G)?X(G):G),[])}function T(...z){return z.flatMap((E)=>P(E))}function j(z,...E){let G=Array.from({length:E.length+1}).fill(null).map(()=>[]);return z.forEach((H,I,K)=>{let L=0;for(let B of E){if(B(H,I,K)){G[L].push(H);return}L+=1}G[L].push(H)}),G}function Y(z){return Array.from(new Set(z))}function u(z){return Y(z)}function C(z,E){return z.reduce((G,H)=>{if(G.findIndex((K)=>E(H,K))===-1)G.push(H);return G},[])}function V(z){return Z(z,-1)}function F(z,E){if(!z)return!1;let G=z.indexOf(E);if(G>=0)return z.splice(G,1),!0;return!1}function Z(z,E){let G=z.length;if(!G)return;if(E<0)E+=G;return z[E]}function N(z,E,G){let H=z.length;if(!H)return[];if(E<0)E+=H;if(G<0)G+=H;let I=z.splice(E,1)[0];return z.splice(G,0,I),z}function R(z,E){return m(E,0,z.length-1)}function _(z,E){return Array.from({length:E},()=>z[Math.floor(Math.random()*z.length)])}function w(z){for(let E=z.length-1;E>0;E--){let G=Math.floor(Math.random()*(E+1));[z[E],z[G]]=[z[G],z[E]]}return z}function J(z){return $(z)/z.length}function o(z){return J(z)}function Q(z){if(z.length===0)throw new Error("Cannot compute median of an empty array");let E=[...z].sort((I,K)=>I-K),G=Math.floor(E.length/2),H;if(E.length%2!==0)H=E[G];else H=(E[G]+E[G-1])/2;return H}function q(z){return z.sort((E,G)=>z.filter((H)=>H===E).length-z.filter((H)=>H===G).length).pop()}function $(z){return z.reduce((E,G)=>E+G,0)}function d(z){return z.reduce((E,G)=>E*G,1)}function p(z){return Math.min(...z)}function A(z){return Math.max(...z)}function b(z){return A(z)-p(z)}function x(z){let E=J(z);return J(z.map((G)=>(G-E)**2))}function g(z){return Math.sqrt(x(z))}function i(z,E){return(E-J(z))/g(z)}function c(z,E){let G=[...z].sort((B,k)=>B-k),H=E/100*(G.length-1),I=Math.floor(H),K=Math.ceil(H),L=H-I;if(K===I)return G[H]??0;return(1-L)*(G[I]??0)+L*(G[K]??0)}function l(z){let E=Q(z.slice(0,Math.floor(z.length/2)));return Q(z.slice(Math.ceil(z.length/2)))-E}function s(z,E){if(z.length!==E.length)throw new Error("Arrays must have the same length");let G=J(z),H=J(E);return J(z.map((I,K)=>(I-G)*(E[K]-H)))}var Jz={contains(z,E){return M(z,E)},containsAll(z,E){return U(z,E)},containsAny(z,E){return W(z,E)},containsNone(z,E){return D(z,E)},containsOnly(z,E){return O(z,E)},doesNotContain(z,E){return S(z,E)},toArray(z){return P(z)},flatten(z){return X(z)},mergeArrayable(...z){return T(...z)},partition(z,E){return j(z,E)},random(z,E=1){return _(z,E).filter((G)=>G!=null)},sample(z,E=1){return _(z,E)},unique(z){return Y(z)},uniqueBy(z,E){return C(z,E)},last(z){return V(z)},remove(z,E){return F(z,E)},at(z,E){return Z(z,E)},range(z){return b(z)},move(z,E,G){return N(z,E,G)},clampArrayRange(z,E){return R(z,E)},shuffle(z){return w(z)},sum(z){return $(z)},average(z){return J(z)},avg(z){return J(z)},median(z){return Q(z)},mode(z){return q(z)}};export{i as zScore,x as variance,C as uniqueBy,u as unique,Y as uniq,P as toArray,$ as sum,g as standardDeviation,w as shuffle,_ as sample,F as remove,b as range,d as product,c as percentile,j as partition,N as move,q as mode,p as min,T as mergeArrayable,Q as median,A as max,V as last,l as interquartileRange,X as flatten,S as doesNotContain,s as covariance,O as containsOnly,D as containsNone,W as containsAny,U as containsAll,M as contains,R as clampArrayRange,o as avg,J as average,Z as at,f as arr,Jz as Arr};
2
+ function M(z,E){return E.some((G)=>z.includes(G))}function W(z,E){return z.every((G)=>M(G,E))}function X(z,E){return z.some((G)=>M(G,E))}function D(z,E){return!X(z,E)}function O(z,E){return W(E,z)}function S(z,E){return!M(z,E)}import{clamp as p}from"@stacksjs/utils";function P(z){return z=z??[],Array.isArray(z)?z:[z]}function Y(z){return P(z).reduce((E,G)=>E.concat(Array.isArray(G)?Y(G):G),[])}function T(...z){return z.flatMap((E)=>P(E))}function j(z,...E){let G=Array.from({length:E.length+1}).fill(null).map(()=>[]);return z.forEach((H,I,J)=>{let L=0;for(let U of E){if(U(H,I,J)){G[L].push(H);return}L+=1}G[L].push(H)}),G}function Z(z){return Array.from(new Set(z))}function m(z){return Z(z)}function V(z,E){return z.reduce((G,H)=>{if(G.findIndex((J)=>E(H,J))===-1)G.push(H);return G},[])}function F(z){return _(z,-1)}function N(z,E){if(!z)return!1;let G=z.indexOf(E);if(G>=0)return z.splice(G,1),!0;return!1}function _(z,E){let G=z.length;if(!G)return;if(E<0)E+=G;return z[E]}function R(z,E,G){let H=z.length;if(!H)return[];if(E<0)E+=H;if(G<0)G+=H;let I=z.splice(E,1)[0];return z.splice(G,0,I),z}function C(z,E){return p(E,0,z.length-1)}function $(z,E){return Array.from({length:E},()=>z[Math.floor(Math.random()*z.length)])}function w(z){for(let E=z.length-1;E>0;E--){let G=Math.floor(Math.random()*(E+1));[z[E],z[G]]=[z[G],z[E]]}return z}function K(z){if(z.length===0)throw Error("Cannot compute average of an empty array");return B(z)/z.length}function d(z){return K(z)}function Q(z){if(z.length===0)throw Error("Cannot compute median of an empty array");let E=[...z].sort((I,J)=>I-J),G=Math.floor(E.length/2),H;if(E.length%2!==0)H=E[G];else H=(E[G]+E[G-1])/2;return H}function b(z){if(z.length===0)throw Error("Cannot compute mode of an empty array");let E=new Map;for(let I of z)E.set(I,(E.get(I)||0)+1);let G=0,H=z[0];for(let[I,J]of E)if(J>G)G=J,H=I;return H}function B(z){return z.reduce((E,G)=>E+G,0)}function o(z){let E=1;for(let G of z)if(E*=G,Number.isFinite(E)&&Math.abs(E)>Number.MAX_SAFE_INTEGER)throw RangeError(`[arrays.product] Result exceeds Number.MAX_SAFE_INTEGER (${Number.MAX_SAFE_INTEGER}); use BigInt for large products.`);return E}function x(z){if(z.length===0)throw Error("Cannot compute min of an empty array");return Math.min(...z)}function g(z){if(z.length===0)throw Error("Cannot compute max of an empty array");return Math.max(...z)}function q(z){return g(z)-x(z)}function k(z){let E=K(z);return K(z.map((G)=>(G-E)**2))}function f(z){return Math.sqrt(k(z))}function i(z,E){return(E-K(z))/f(z)}function l(z,E){let G=[...z].sort((U,A)=>U-A),H=E/100*(G.length-1),I=Math.floor(H),J=Math.ceil(H),L=H-I;if(J===I)return G[H]??0;return(1-L)*(G[I]??0)+L*(G[J]??0)}function c(z){let E=Q(z.slice(0,Math.floor(z.length/2)));return Q(z.slice(Math.ceil(z.length/2)))-E}function s(z,E){if(z.length!==E.length)throw Error("Arrays must have the same length");let G=K(z),H=K(E);return K(z.map((I,J)=>(I-G)*(E[J]-H)))}var r={contains(z,E){return M(z,E)},containsAll(z,E){return W(z,E)},containsAny(z,E){return X(z,E)},containsNone(z,E){return D(z,E)},containsOnly(z,E){return O(z,E)},doesNotContain(z,E){return S(z,E)},toArray(z){return P(z)},flatten(z){return Y(z)},mergeArrayable(...z){return T(...z)},partition(z,E){return j(z,E)},random(z,E=1){return $(z,E).filter((G)=>G!=null)},sample(z,E=1){return $(z,E)},unique(z){return Z(z)},uniqueBy(z,E){return V(z,E)},last(z){return F(z)},remove(z,E){return N(z,E)},at(z,E){return _(z,E)},range(z){return q(z)},move(z,E,G){return R(z,E,G)},clampArrayRange(z,E){return C(z,E)},shuffle(z){return w(z)},sum(z){return B(z)},average(z){return K(z)},avg(z){return K(z)},median(z){return Q(z)},mode(z){return b(z)}};export{i as zScore,k as variance,V as uniqueBy,m as unique,Z as uniq,P as toArray,B as sum,f as standardDeviation,w as shuffle,$ as sample,N as remove,q as range,o as product,l as percentile,j as partition,R as move,b as mode,x as min,T as mergeArrayable,Q as median,g as max,F as last,c as interquartileRange,Y as flatten,S as doesNotContain,s as covariance,O as containsOnly,D as containsNone,X as containsAny,W as containsAll,M as contains,C as clampArrayRange,d as avg,K as average,_ as at,Gz as arr,r as Arr};
@@ -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,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,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;
@@ -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/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@stacksjs/arrays",
3
3
  "type": "module",
4
- "version": "0.70.23",
4
+ "version": "0.70.25",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
- "contributors": ["Chris Breuer <chris@stacksjs.org>"],
7
+ "contributors": [
8
+ "Chris Breuer <chris@stacksjs.com>"
9
+ ],
8
10
  "license": "MIT",
9
11
  "funding": "https://github.com/sponsors/chrisbbreuer",
10
12
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/arrays#readme",
@@ -26,15 +28,21 @@
26
28
  ],
27
29
  "exports": {
28
30
  ".": {
31
+ "bun": "./src/index.ts",
32
+ "types": "./dist/index.d.ts",
29
33
  "import": "./dist/index.js"
30
34
  },
31
35
  "./*": {
36
+ "bun": "./src/*",
32
37
  "import": "./dist/*"
33
38
  }
34
39
  },
35
40
  "module": "dist/index.js",
36
41
  "types": "dist/index.d.ts",
37
- "files": ["README.md", "dist"],
42
+ "files": [
43
+ "README.md",
44
+ "dist"
45
+ ],
38
46
  "scripts": {
39
47
  "build": "bun build.ts",
40
48
  "typecheck": "bun tsc --noEmit",
@@ -42,7 +50,8 @@
42
50
  "prepublishOnly": "bun run build"
43
51
  },
44
52
  "devDependencies": {
45
- "@stacksjs/development": "0.70.22",
46
- "@stacksjs/utils": "0.70.22"
47
- }
53
+ "better-dx": "^0.2.12",
54
+ "@stacksjs/utils": "0.70.23"
55
+ },
56
+ "sideEffects": false
48
57
  }
package/dist/arr.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './contains'
2
- export * from './helpers'
3
- export * from './math'
@@ -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/helpers.d.ts DELETED
@@ -1,27 +0,0 @@
1
- import type { Nullable } from '@stacksjs/types';
2
-
3
- export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
4
- export declare function flatten<T>(array?: Nullable<Arrayable<T | T[]>>): T[];
5
- export declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
6
- export declare type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any
7
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
8
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
9
- export declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
10
- 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[]];
11
- 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[]];
12
- 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[]];
13
- export declare function partition<T>(array: readonly T[], ...filters: PartitionFilter<T>[]): any;
14
- export declare function uniq<T>(array: readonly T[]): T[];
15
- export declare function unique<T>(array: readonly T[]): T[];
16
- export declare function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any)): T[];
17
- export declare function last(array: readonly []): undefined;
18
- export declare function last<T>(array: readonly T[]): T;
19
- export declare function last<T>(array: readonly T[]): T | undefined;
20
- export declare function remove<T>(array: T[], value: T): boolean;
21
- export declare function at(array: readonly [], index: number): undefined;
22
- export declare function at<T>(array: readonly T[], index: number): T;
23
- export declare function at<T>(array: readonly T[] | [], index: number): T | undefined;
24
- export declare function move<T>(array: T[], from: number, to: number): T[];
25
- export declare function clampArrayRange(arr: readonly unknown[], n: number): number;
26
- export declare function sample<T>(arr: T[], count: number): T[];
27
- export declare function shuffle<T>(array: T[]): T[];
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './arr'
2
- export * as arr from './arr'
3
- export * from './macro'
package/dist/macro.d.ts DELETED
@@ -1,44 +0,0 @@
1
- import type { Nullable } from '@stacksjs/types';
2
- import type { PartitionFilter } from './helpers';
3
-
4
- export declare const Arr: {
5
- contains: (needle: string, haystack: string[]) => void;
6
- containsAll: (needles: string[], haystack: string[]) => void;
7
- containsAny: (needles: string[], haystack: string[]) => void;
8
- containsNone: (needles: string[], haystack: string[]) => void;
9
- containsOnly: (needles: string[], haystack: string[]) => void;
10
- doesNotContain: (needle: string, haystack: string[]) => void;
11
- toArray: <T> (array?: Nullable<Arrayable<T>>) => void;
12
- flatten: <T> (array?: Nullable<Arrayable<T | T[]>>) => void;
13
- mergeArrayable: <T> (...args: Nullable<Arrayable<T>>[]) => void;
14
- partition: <T> (array: readonly T[], filter: PartitionFilter<T>) => void;
15
- * Returns a random item/s from the array
16
- */
17
- random<T>: () => unknown;
18
- * Returns random item/s from the array
19
- */
20
- sample<T>: () => unknown;
21
- unique: <T> (arr: T[]) => void;
22
- uniqueBy: <T> (arr: readonly T[], equalFn: (a: any, b: any) => void;
23
- last: <T> (arr: T[]) => void;
24
- remove: <T> (arr: T[], value: T) => void;
25
- at: (arr: any[], index: number) => void;
26
- range: (arr: readonly number[]) => void;
27
- move: <T> (arr: T[], from: number, to: number) => void;
28
- clampArrayRange: (arr: readonly unknown[], n: number) => void;
29
- shuffle: <T> (arr: T[]) => void;
30
- * Returns the sum of all items in the array
31
- */
32
- sum: () => unknown;
33
- * Returns the average of all items in the array
34
- */
35
- average: () => unknown;
36
- avg: (arr: number[]) => void;
37
- * Returns the median of all items in the array
38
- */
39
- median: () => unknown;
40
- * Returns the mode of all items in the array
41
- */
42
- mode: () => unknown
43
- };
44
- export declare const arr: typeof Arr;
package/dist/math.d.ts DELETED
@@ -1,15 +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;
6
- export declare function product(array: readonly number[]): number;
7
- export declare function min(array: readonly number[]): number;
8
- export declare function max(array: readonly number[]): number;
9
- export declare function range(array: readonly number[]): number;
10
- export declare function variance(array: number[]): number;
11
- export declare function standardDeviation(array: number[]): number;
12
- export declare function zScore(array: number[], num: number): number;
13
- export declare function percentile(array: number[], num: number): number;
14
- export declare function interquartileRange(array: number[]): number;
15
- export declare function covariance(array1: number[], array2: number[]): number;