X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=node_modules%2Fgrunt-legacy-util%2Fnode_modules%2Flodash%2FsampleSize.js;fp=node_modules%2Fgrunt-legacy-util%2Fnode_modules%2Flodash%2FsampleSize.js;h=47e7b116e394efa2dcbedbea0e9d11c78f2618f3;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/node_modules/grunt-legacy-util/node_modules/lodash/sampleSize.js b/node_modules/grunt-legacy-util/node_modules/lodash/sampleSize.js new file mode 100644 index 000000000..47e7b116e --- /dev/null +++ b/node_modules/grunt-legacy-util/node_modules/lodash/sampleSize.js @@ -0,0 +1,42 @@ +var baseClamp = require('./_baseClamp'), + baseRandom = require('./_baseRandom'), + toArray = require('./toArray'), + toInteger = require('./toInteger'); + +/** + * Gets `n` random elements at unique keys from `collection` up to the + * size of `collection`. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object} collection The collection to sample. + * @param {number} [n=0] The number of elements to sample. + * @returns {Array} Returns the random elements. + * @example + * + * _.sampleSize([1, 2, 3], 2); + * // => [3, 1] + * + * _.sampleSize([1, 2, 3], 4); + * // => [2, 3, 1] + */ +function sampleSize(collection, n) { + var index = -1, + result = toArray(collection), + length = result.length, + lastIndex = length - 1; + + n = baseClamp(toInteger(n), 0, length); + while (++index < n) { + var rand = baseRandom(index, lastIndex), + value = result[rand]; + + result[rand] = result[index]; + result[index] = value; + } + result.length = n; + return result; +} + +module.exports = sampleSize;