-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression(?) in sample
behavior for strings
#2927
Comments
Thanks for reporting, @dylemma. I was able to trace this down to #2158 and confirm that this is indeed a regression, which was already introduced in 1.9.0 (ironically, right after the version you were using). The reason is that in 1.8.3, Sorry about the inconvenience. I will try to address this soon. In the meanwhile, you can patch Underscore as follows so you don't have to edit your code everywhere: var originalSample = _.sample;
function fixedSample(collection, n, guard) {
if (_.isString(collection)) collection = _.toArray(collection);
return originalSample(collection, n, guard);
}
_.mixin({sample: fixedSample}); |
Hi and thanks for the really handy library! I've had a dependency on underscore on a work project for many years now (we were using version 1.8.3) and I just merged a dependabot update which pulled in version 1.12.1 and noticed an unexpected change in the behavior of the
sample
function.Previously, you could pass a
string
as thelist
argument, and it would be treated as an array of characters, e.g.But now it just returns a substring from the beginning of the string, with no randomness
Other observations:
n
argument, it does return a random letter, e.g._.sample('abcdef')
returns"d"
or"a"
etc_.sample(['a', 'b', 'c', 'd', 'e', 'f'], 3)
The text was updated successfully, but these errors were encountered: