@adriansteffan/reactive 0.0.27 → 0.0.28
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/{mod-CbGhKi2f.js → mod-CjZm1Ta9.js} +392 -376
- package/dist/mod.d.ts +20 -8
- package/dist/reactive.es.js +18 -16
- package/dist/reactive.umd.js +24 -24
- package/dist/{web-DOFokKz7.js → web-B_I1xy51.js} +1 -1
- package/dist/{web-BFGLx41c.js → web-DfpITFBR.js} +1 -1
- package/package.json +1 -1
- package/src/utils/array.ts +49 -15
package/package.json
CHANGED
package/src/utils/array.ts
CHANGED
|
@@ -3,46 +3,52 @@ declare global {
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns random elements from the array
|
|
5
5
|
* @param n Number of random elements to return (defaults to 1)
|
|
6
|
-
* @returns Array of randomly selected elements
|
|
7
6
|
*/
|
|
8
7
|
sample(n?: number): Array<T>;
|
|
9
|
-
|
|
8
|
+
|
|
10
9
|
/**
|
|
11
10
|
* Shuffles array elements using Fisher-Yates algorithm
|
|
12
|
-
* @returns A new shuffled array
|
|
13
11
|
*/
|
|
14
12
|
shuffle(): Array<T>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Applies a function to the array
|
|
16
|
+
* @param fn Function to apply to the array
|
|
17
|
+
*/
|
|
18
|
+
pipe<U>(fn: (arr: Array<T>) => U): U;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Splits array into chunks
|
|
22
|
+
* @param n Number of chunks to create
|
|
23
|
+
*/
|
|
24
|
+
chunk(n: number): Array<Array<T>>;
|
|
15
25
|
}
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Returns random elements from an array
|
|
20
|
-
* @param array The source array
|
|
21
|
-
* @param n Number of random elements to return (defaults to 1)
|
|
22
|
-
* @returns Array of randomly selected elements
|
|
23
30
|
*/
|
|
24
31
|
export function sample<T>(array: T[], n: number = 1): T[] {
|
|
25
32
|
const result: T[] = [];
|
|
26
|
-
|
|
33
|
+
|
|
27
34
|
if (array.length === 0) {
|
|
28
35
|
return result;
|
|
29
36
|
}
|
|
30
|
-
|
|
37
|
+
|
|
31
38
|
for (let i = 0; i < n; i++) {
|
|
32
39
|
const randomIndex = Math.floor(Math.random() * array.length);
|
|
33
40
|
result.push(array[randomIndex]);
|
|
34
41
|
}
|
|
35
|
-
|
|
42
|
+
|
|
36
43
|
return result;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
/**
|
|
40
47
|
* Shuffles array elements using Fisher-Yates algorithm
|
|
41
|
-
* @param array The source array
|
|
42
|
-
* @returns A new shuffled array
|
|
43
48
|
*/
|
|
44
49
|
export function shuffle<T>(array: T[]): T[] {
|
|
45
|
-
const result = [...array];
|
|
50
|
+
const result = [...array];
|
|
51
|
+
|
|
46
52
|
for (let i = result.length - 1; i >= 0; i--) {
|
|
47
53
|
const j = Math.floor(Math.random() * (i + 1));
|
|
48
54
|
[result[i], result[j]] = [result[j], result[i]];
|
|
@@ -50,6 +56,23 @@ export function shuffle<T>(array: T[]): T[] {
|
|
|
50
56
|
return result;
|
|
51
57
|
}
|
|
52
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Applies a function to an array
|
|
61
|
+
*/
|
|
62
|
+
export function pipe<T, U>(array: T[], fn: (arr: T[]) => U): U {
|
|
63
|
+
return fn(array);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Splits array into chunks
|
|
68
|
+
*/
|
|
69
|
+
export function chunk<T>(array: T[], n: number): T[][] {
|
|
70
|
+
const size = Math.ceil(array.length / n);
|
|
71
|
+
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) =>
|
|
72
|
+
array.slice(i * size, i * size + size),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
53
76
|
/**
|
|
54
77
|
* Registers array methods on the Array prototype.
|
|
55
78
|
* Call this function once to make array methods available globally.
|
|
@@ -59,7 +82,6 @@ export function shuffle<T>(array: T[]): T[] {
|
|
|
59
82
|
* import { registerArrayExtensions } from '@adriansteffan/reactive/array';
|
|
60
83
|
* registerArrayExtensions();
|
|
61
84
|
*
|
|
62
|
-
* // Now you can use the methods
|
|
63
85
|
* const myArray = [1, 2, 3, 4, 5];
|
|
64
86
|
* myArray.shuffle();
|
|
65
87
|
* ```
|
|
@@ -70,10 +92,22 @@ export function registerArrayExtensions(): void {
|
|
|
70
92
|
return sample(this, n);
|
|
71
93
|
};
|
|
72
94
|
}
|
|
73
|
-
|
|
95
|
+
|
|
74
96
|
if (typeof Array.prototype.shuffle !== 'function') {
|
|
75
97
|
Array.prototype.shuffle = function <T>(this: T[]): T[] {
|
|
76
|
-
return shuffle(this);
|
|
98
|
+
return shuffle(this);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof Array.prototype.pipe !== 'function') {
|
|
103
|
+
Array.prototype.pipe = function <T, U>(this: T[], fn: (arr: T[]) => U): U {
|
|
104
|
+
return pipe(this, fn);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (typeof Array.prototype.chunk !== 'function') {
|
|
109
|
+
Array.prototype.chunk = function <T>(this: T[], n: number): T[][] {
|
|
110
|
+
return chunk(this, n);
|
|
77
111
|
};
|
|
78
112
|
}
|
|
79
113
|
}
|