Skip to content

Commit

Permalink
Properly convert _.sample input collection _.toArray (fix #2927)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Dec 16, 2021
1 parent 354337a commit 72173ba
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions modules/sample.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import isArrayLike from './_isArrayLike.js';
import clone from './clone.js';
import values from './values.js';
import getLength from './_getLength.js';
import random from './random.js';
import toArray from './toArray.js';

// Sample **n** random values from a collection using the modern version of the
// [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
Expand All @@ -13,7 +13,7 @@ export default function sample(obj, n, guard) {
if (!isArrayLike(obj)) obj = values(obj);
return obj[random(obj.length - 1)];
}
var sample = isArrayLike(obj) ? clone(obj) : values(obj);
var sample = toArray(obj);
var length = getLength(sample);
n = Math.max(Math.min(n, length), 0);
var last = length - 1;
Expand Down
28 changes: 14 additions & 14 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions underscore-node-f.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,19 @@ function min(obj, iteratee, context) {
return result;
}

// Safely create a real, live array from anything iterable.
var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
function toArray(obj) {
if (!obj) return [];
if (isArray(obj)) return slice.call(obj);
if (isString(obj)) {
// Keep surrogate pair characters together.
return obj.match(reStrSymbol);
}
if (isArrayLike(obj)) return map(obj, identity);
return values(obj);
}

// Sample **n** random values from a collection using the modern version of the
// [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
// If **n** is not specified, returns a single random element.
Expand All @@ -1515,7 +1528,7 @@ function sample(obj, n, guard) {
if (!isArrayLike(obj)) obj = values(obj);
return obj[random(obj.length - 1)];
}
var sample = isArrayLike(obj) ? clone(obj) : values(obj);
var sample = toArray(obj);
var length = getLength(sample);
n = Math.max(Math.min(n, length), 0);
var last = length - 1;
Expand Down Expand Up @@ -1592,19 +1605,6 @@ var partition = group(function(result, value, pass) {
result[pass ? 0 : 1].push(value);
}, true);

// Safely create a real, live array from anything iterable.
var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
function toArray(obj) {
if (!obj) return [];
if (isArray(obj)) return slice.call(obj);
if (isString(obj)) {
// Keep surrogate pair characters together.
return obj.match(reStrSymbol);
}
if (isArrayLike(obj)) return map(obj, identity);
return values(obj);
}

// Return the number of elements in a collection.
function size(obj) {
if (obj == null) return 0;
Expand Down
2 changes: 1 addition & 1 deletion underscore-node-f.cjs.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions underscore-umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-umd.js.map

Large diffs are not rendered by default.

0 comments on commit 72173ba

Please sign in to comment.