all files / shuffle-array/ index.js

100% Statements 30/30
100% Branches 22/22
100% Functions 2/2
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                          10× 10× 10× 10× 10×                                                                
'use strict';
 
/**
 * Randomize the order of the elements in a given array.
 * @param {Array} arr - The given array.
 * @param {Object} [options] - Optional configuration options.
 * @param {Boolean} [options.copy] - Sets if should return a shuffled copy of the given array. By default it's a falsy value.
 * @param {Function} [options.rng] - Specifies a custom random number generator.
 * @returns {Array}
 */
function shuffle(arr, options) {
 
  if (!Array.isArray(arr)) {
    throw new Error('shuffle expect an array as parameter.');
  }
 
  options = options || {};
 
  var collection = arr,
      len = arr.length,
      rng = options.rng || Math.random,
      random,
      temp;
 
  if (options.copy === true) {
    collection = arr.slice();
  }
 
  while (len) {
    random = Math.floor(rng() * len);
    len -= 1;
    temp = collection[len];
    collection[len] = collection[random];
    collection[random] = temp;
  }
 
  return collection;
};
 
/**
 * Pick one or more random elements from the given array.
 * @param {Array} arr - The given array.
 * @param {Object} [options] - Optional configuration options.
 * @param {Number} [options.picks] - Specifies how many random elements you want to pick. By default it picks 1.
 * @param {Function} [options.rng] - Specifies a custom random number generator.
 * @returns {Object}
 */
shuffle.pick = function(arr, options) {
 
  if (!Array.isArray(arr)) {
    throw new Error('shuffle.pick() expect an array as parameter.');
  }
 
  options = options || {};
 
  var rng = options.rng || Math.random,
      picks = options.picks || 1;
 
  if (typeof picks === 'number' && picks !== 1) {
    var len = arr.length,
        collection = arr.slice(),
        random = [],
        index;
 
    while (picks && len) {
      index = Math.floor(rng() * len);
      random.push(collection[index]);
      collection.splice(index, 1);
      len -= 1;
      picks -= 1;
    }
 
    return random;
  }
 
  return arr[Math.floor(rng() * arr.length)];
};
 
/**
 * Expose
 */
module.exports = shuffle;