UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

15 lines 538 B
import toArray from './toArray'; /** * Collects all values from the input iterator, then shuffles the order of it's values. * @param seed A seed between 0 and 1. */ export function shuffle(arg, seed = Math.random()) { const values = toArray(arg); for (let i = values.length - 1; i > 0; i--) { const j = Math.min(i, Math.max(0, Math.floor(seed * (i + 1)))); [values[i], values[j]] = [values[j], values[i]]; } return values[Symbol.iterator](); } export default shuffle; //# sourceMappingURL=shuffle.js.map