mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Ensure min and max of _.random are swapped if min > max.
This commit is contained in:
40
lodash.js
40
lodash.js
@@ -11651,29 +11651,33 @@
|
|||||||
if (floating && typeof floating != 'boolean' && isIterateeCall(min, max, floating)) {
|
if (floating && typeof floating != 'boolean' && isIterateeCall(min, max, floating)) {
|
||||||
max = floating = undefined;
|
max = floating = undefined;
|
||||||
}
|
}
|
||||||
var noMin = min === undefined,
|
|
||||||
noMax = max === undefined;
|
|
||||||
|
|
||||||
if (floating === undefined) {
|
if (floating === undefined) {
|
||||||
if (noMax && typeof min == 'boolean') {
|
if (typeof max == 'boolean') {
|
||||||
floating = min;
|
|
||||||
min = 1;
|
|
||||||
}
|
|
||||||
else if (typeof max == 'boolean') {
|
|
||||||
floating = max;
|
floating = max;
|
||||||
noMax = true;
|
max = undefined;
|
||||||
|
}
|
||||||
|
else if (typeof min == 'boolean') {
|
||||||
|
floating = min;
|
||||||
|
min = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (noMin && noMax) {
|
if (min === undefined && max === undefined) {
|
||||||
max = 1;
|
|
||||||
noMax = false;
|
|
||||||
}
|
|
||||||
min = toNumber(min) || 0;
|
|
||||||
if (noMax) {
|
|
||||||
max = min;
|
|
||||||
min = 0;
|
min = 0;
|
||||||
} else {
|
max = 1;
|
||||||
max = toNumber(max) || 0;
|
}
|
||||||
|
else {
|
||||||
|
min = toNumber(min) || 0;
|
||||||
|
if (max === undefined) {
|
||||||
|
max = min;
|
||||||
|
min = 0;
|
||||||
|
} else {
|
||||||
|
max = toNumber(max) || 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (min > max) {
|
||||||
|
var temp = min;
|
||||||
|
min = max;
|
||||||
|
max = temp;
|
||||||
}
|
}
|
||||||
if (floating || min % 1 || max % 1) {
|
if (floating || min % 1 || max % 1) {
|
||||||
var rand = nativeRandom();
|
var rand = nativeRandom();
|
||||||
|
|||||||
14
test/test.js
14
test/test.js
@@ -15407,6 +15407,20 @@
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should swap `min` and `max` when `min` > `max`', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var min = 4,
|
||||||
|
max = 2,
|
||||||
|
expected = [2, 3, 4];
|
||||||
|
|
||||||
|
var actual = lodashStable.uniq(lodashStable.map(array, function() {
|
||||||
|
return _.random(min, max);
|
||||||
|
})).sort();
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should support large integer values', function(assert) {
|
QUnit.test('should support large integer values', function(assert) {
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user