You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
519 B
18 lines
519 B
var isArrLike = require('./isArrLike'); |
|
var clone = require('./clone'); |
|
var values = require('./values'); |
|
var random = require('./random'); |
|
var swap = require('./swap'); |
|
exports = function(obj, n) { |
|
var sample = isArrLike(obj) ? clone(obj) : values(obj); |
|
var len = sample.length; |
|
n = Math.max(Math.min(n, len), 0); |
|
var last = len - 1; |
|
for (var i = 0; i < n; i++) { |
|
var rand = random(i, last); |
|
swap(sample, i, rand); |
|
} |
|
return sample.slice(0, n); |
|
}; |
|
|
|
module.exports = exports;
|
|
|