UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

48 lines (47 loc) 1.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.shuffle = void 0; const copyArray_1 = __importDefault(require("./.internal/copyArray")); const shuffleSelf_1 = require("./.internal/shuffleSelf"); const isArray_1 = __importDefault(require("./isArray")); const values_1 = __importDefault(require("./values")); /** * @private * @ignore * @param array */ function arrayShuffle(array) { return (0, shuffleSelf_1.shuffleSelf)((0, copyArray_1.default)(array)); } /** * @ignore * @private * @param collection */ function baseShuffle(collection) { return (0, shuffleSelf_1.shuffleSelf)((0, values_1.default)(collection)); } /** * Creates an array of shuffled values, using a version of the * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle). * * @since 5.11.0 * @category Array * @param collection The array to shuffle. * @returns Returns the new shuffled array. * @example * * ```js * shuffle([1, 2, 3, 4]) * // => [4, 1, 3, 2] * ``` */ function shuffle(collection) { const func = (0, isArray_1.default)(collection) ? arrayShuffle : baseShuffle; return func(collection); } exports.shuffle = shuffle; exports.default = shuffle;