graycoreio/daffodil

View on GitHub
libs/core/src/utils/shuffle.ts

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Immutable Fisher-Yates Shuffle
 * https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
 */
export const shuffle = <T>(array: T[]): T[] => {
  const result = [];
  array.forEach((el, i) => {
    const s = Math.floor(Math.random() * (i + 1));
    if( s !== i ){
      result[i] = result[s];
    }
    result[s] = el;
  });
  return result;
};