UNPKG

@pacote/shuffle

Version:

An implementation of the Durstenfeld algorithm for shuffling collections.

28 lines 921 B
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.shuffle = shuffle; function random(min, max) { return min + Math.floor(Math.random() * (max - min)); } function swap(i, j, items) { var swapped = items[i]; items[i] = items[j]; items[j] = swapped; } function shuffle(items) { var shuffled = __spreadArray([], items, true); for (var i = 0; i < items.length - 1; i++) { swap(i, random(i, items.length), shuffled); } return shuffled; } //# sourceMappingURL=index.js.map