UNPKG

diginext-utils

Version:
32 lines 922 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shuffle = shuffle; /** * Creates a new array with elements shuffled in random order. * Uses the Fisher-Yates shuffle algorithm for uniform randomness. * * @template T - The type of elements in the array * @param array - The array to shuffle * @returns A new shuffled array * * @example * ```ts * shuffle([1, 2, 3, 4, 5]); // e.g., [3, 1, 5, 2, 4] * shuffle(['a', 'b', 'c']); // e.g., ['c', 'a', 'b'] * shuffle([]); // [] * ``` */ function shuffle(array) { if (!Array.isArray(array) || array.length === 0) { return []; } const result = [...array]; for (let i = result.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const temp = result[i]; result[i] = result[j]; result[j] = temp; } return result; } //# sourceMappingURL=shuffle.js.map