froebel
Version:
TypeScript utility library
36 lines (29 loc) • 811 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shuffleInPlace = exports.default = void 0;
/**
* Shuffles `list` using the Fisher-Yates shuffle algorithm.
* The original `list` is not modified and the shuffled list is returned.
*/
const shuffle = list => {
const shuffled = [...list];
shuffleInPlace(shuffled);
return shuffled;
};
var _default = shuffle;
/**
* Same as {@link shuffle} but shuffles `list` in place.
*/
exports.default = _default;
const shuffleInPlace = list => {
for (let i = list.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const tmp = list[j];
list[j] = list[i];
list[i] = tmp;
}
};
exports.shuffleInPlace = shuffleInPlace;
module.exports = Object.assign(exports.default || {}, exports);