UNPKG

@grandom/shuffle

Version:

A configurable, flexible, seedable, and overall great random shuffler.

51 lines (47 loc) 1.63 kB
'use strict'; var core = require('@grandom/core'); var index = require('../utils/index.js'); // TODO: handle typed arrays class RandomArrayShuffle extends core.RandomGenerator { constructor(engine) { super(engine); this.shuffle = this.shuffle.bind(this); } // ----------------------------------------------------------------------------------------------- canBeParsed(arg1) { return Array.isArray(arg1); } parse(arg1, arg2, arg3) { if (arg1.length < 1) { return []; } const { count, options } = index.getRest(arg1, arg2, arg3); const filter = typeof options.filter === 'function' ? options.filter : undefined; const length = count === -1 ? arg1.length : count; const pool = []; for (let i = 0; i < length; i++) { const element = arg1[i]; if (filter?.(element) === false) { continue; } pool.push(element); } this._engine.shuffleArray(pool); return pool; } // ----------------------------------------------------------------------------------------------- shuffle(arg1, arg2, arg3) { if (this.canBeParsed(arg1)) { return this.parse(arg1, arg2, arg3); } throw new TypeError( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Must be called with an array, got: ${arg1} (typeof === '${typeof arg1}').`); } } module.exports = RandomArrayShuffle; //# sourceMappingURL=index.js.map