mentie 0.3.19 → 0.3.21
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/index.js +2 -1
- package/modules/manipulations.js +23 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -7,4 +7,5 @@ export * from './modules/numbers.js'
|
|
|
7
7
|
export * from './modules/promises.js'
|
|
8
8
|
export * from './modules/cache.js'
|
|
9
9
|
export * from './modules/crypto.js'
|
|
10
|
-
export * from './modules/network.js'
|
|
10
|
+
export * from './modules/network.js'
|
|
11
|
+
export * from './modules/manipulations.js'
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shuffle an array using the Fisher-Yates algorithm.
|
|
3
|
+
* @param {Array} array
|
|
4
|
+
* @returns {Array} The shuffled array.
|
|
5
|
+
*/
|
|
6
|
+
export const shuffle_array = ( array, in_place=true ) => {
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// Validate input
|
|
10
|
+
if( Array.isArray( array ) === false ) throw new Error( 'Input must be an array' )
|
|
11
|
+
|
|
12
|
+
// Handle in-place shuffling
|
|
13
|
+
const to_shuffle = in_place ? array : [ ...array ]
|
|
14
|
+
|
|
15
|
+
// Fisher-Yates shuffle
|
|
16
|
+
for( let i = to_shuffle.length - 1; i > 0; i-- ) {
|
|
17
|
+
const j = Math.floor( Math.random() * ( i + 1 ) );
|
|
18
|
+
[ to_shuffle[i], to_shuffle[j] ] = [ to_shuffle[j], to_shuffle[i] ]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return to_shuffle
|
|
22
|
+
|
|
23
|
+
}
|