mirror of
https://github.com/alexbosworth/balanceofsatoshis.git
synced 2026-08-13 12:33:37 +02:00
34 lines
572 B
JavaScript
34 lines
572 B
JavaScript
const {floor} = Math;
|
|
const {isArray} = Array;
|
|
const {random} = Math;
|
|
|
|
/** Shuffle array
|
|
|
|
{
|
|
array: [<Element Object>]
|
|
}
|
|
|
|
@returns
|
|
{
|
|
shuffled: [<Shuffled Element Object>]
|
|
}
|
|
*/
|
|
module.exports = ({array}) => {
|
|
if (!isArray(array)) {
|
|
throw new Error('ExpectedArrayToShuffle');
|
|
}
|
|
|
|
if (!array.length) {
|
|
return {shuffled: []};
|
|
}
|
|
|
|
const shuffled = array.slice();
|
|
|
|
for (let i = shuffled.length - 1; !!i; i--) {
|
|
const j = floor(random() * (i + 1));
|
|
|
|
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
}
|
|
|
|
return {shuffled};
|
|
};
|