mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Use _.toFinite in random and range methods.
This commit is contained in:
15
lodash.js
15
lodash.js
@@ -5009,15 +5009,14 @@
|
|||||||
end = step = undefined;
|
end = step = undefined;
|
||||||
}
|
}
|
||||||
// Ensure the sign of `-0` is preserved.
|
// Ensure the sign of `-0` is preserved.
|
||||||
start = toNumber(start);
|
start = toFinite(start);
|
||||||
start = start === start ? start : 0;
|
|
||||||
if (end === undefined) {
|
if (end === undefined) {
|
||||||
end = start;
|
end = start;
|
||||||
start = 0;
|
start = 0;
|
||||||
} else {
|
} else {
|
||||||
end = toNumber(end) || 0;
|
end = toFinite(end);
|
||||||
}
|
}
|
||||||
step = step === undefined ? (start < end ? 1 : -1) : (toNumber(step) || 0);
|
step = step === undefined ? (start < end ? 1 : -1) : toFinite(step);
|
||||||
return baseRange(start, end, step, fromRight);
|
return baseRange(start, end, step, fromRight);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -13533,12 +13532,12 @@
|
|||||||
* // => true
|
* // => true
|
||||||
*/
|
*/
|
||||||
function inRange(number, start, end) {
|
function inRange(number, start, end) {
|
||||||
start = toNumber(start) || 0;
|
start = toFinite(start);
|
||||||
if (end === undefined) {
|
if (end === undefined) {
|
||||||
end = start;
|
end = start;
|
||||||
start = 0;
|
start = 0;
|
||||||
} else {
|
} else {
|
||||||
end = toNumber(end) || 0;
|
end = toFinite(end);
|
||||||
}
|
}
|
||||||
number = toNumber(number);
|
number = toNumber(number);
|
||||||
return baseInRange(number, start, end);
|
return baseInRange(number, start, end);
|
||||||
@@ -13594,12 +13593,12 @@
|
|||||||
upper = 1;
|
upper = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lower = toNumber(lower) || 0;
|
lower = toFinite(lower);
|
||||||
if (upper === undefined) {
|
if (upper === undefined) {
|
||||||
upper = lower;
|
upper = lower;
|
||||||
lower = 0;
|
lower = 0;
|
||||||
} else {
|
} else {
|
||||||
upper = toNumber(upper) || 0;
|
upper = toFinite(upper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lower > upper) {
|
if (lower > upper) {
|
||||||
|
|||||||
56
test/test.js
56
test/test.js
@@ -8290,10 +8290,15 @@
|
|||||||
QUnit.test('should coerce arguments to finite numbers', function(assert) {
|
QUnit.test('should coerce arguments to finite numbers', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var actual = [_.inRange(0, '0', 1), _.inRange(0, '1'), _.inRange(0, 0, '1'), _.inRange(0, NaN, 1), _.inRange(-1, -1, NaN)],
|
var actual = [
|
||||||
expected = lodashStable.map(actual, stubTrue);
|
_.inRange(0, '1'),
|
||||||
|
_.inRange(0, '0', 1),
|
||||||
|
_.inRange(0, 0, '1'),
|
||||||
|
_.inRange(0, NaN, 1),
|
||||||
|
_.inRange(-1, -1, NaN)
|
||||||
|
];
|
||||||
|
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, lodashStable.map(actual, stubTrue));
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -18211,10 +18216,15 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should coerce arguments to finite numbers', function(assert) {
|
QUnit.test('should coerce arguments to finite numbers', function(assert) {
|
||||||
assert.expect(2);
|
assert.expect(1);
|
||||||
|
|
||||||
assert.strictEqual(_.random('1', '1'), 1);
|
var actual = [
|
||||||
assert.strictEqual(_.random(NaN, NaN), 0);
|
_.random(NaN, NaN),
|
||||||
|
_.random('1', '1'),
|
||||||
|
_.random(Infinity, Infinity)
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(actual, [0, 1, MAX_INTEGER]);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should support floats', function(assert) {
|
QUnit.test('should support floats', function(assert) {
|
||||||
@@ -18332,7 +18342,14 @@
|
|||||||
QUnit.test('`_.' + methodName + '` should coerce arguments to finite numbers', function(assert) {
|
QUnit.test('`_.' + methodName + '` should coerce arguments to finite numbers', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var actual = [func('0', 1), func('1'), func(0, 1, '1'), func(NaN), func(NaN, NaN)];
|
var actual = [
|
||||||
|
func('1'),
|
||||||
|
func('0', 1),
|
||||||
|
func(0, 1, '1'),
|
||||||
|
func(NaN),
|
||||||
|
func(NaN, NaN)
|
||||||
|
];
|
||||||
|
|
||||||
assert.deepEqual(actual, [[0], [0], [0], [], []]);
|
assert.deepEqual(actual, [[0], [0], [0], [], []]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -18731,15 +18748,15 @@
|
|||||||
|
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
||||||
predicate = function(value) { return isFilter ? isEven(value) : !isEven(value); },
|
predicate = function(value) { return isFilter ? isEven(value) : !isEven(value); };
|
||||||
actual = _(array).slice(1).map(square)[methodName](predicate).value();
|
|
||||||
|
|
||||||
assert.deepEqual(actual, _[methodName](lodashStable.map(array.slice(1), square), predicate));
|
|
||||||
|
|
||||||
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
||||||
return ['key' + index, index];
|
return ['key' + index, index];
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
var actual = _(array).slice(1).map(square)[methodName](predicate).value();
|
||||||
|
assert.deepEqual(actual, _[methodName](lodashStable.map(array.slice(1), square), predicate));
|
||||||
|
|
||||||
actual = _(object).mapValues(square)[methodName](predicate).value();
|
actual = _(object).mapValues(square)[methodName](predicate).value();
|
||||||
assert.deepEqual(actual, _[methodName](lodashStable.mapValues(object, square), predicate));
|
assert.deepEqual(actual, _[methodName](lodashStable.mapValues(object, square), predicate));
|
||||||
}
|
}
|
||||||
@@ -21483,8 +21500,13 @@
|
|||||||
|
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var args,
|
var args,
|
||||||
array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
array = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
||||||
expected = [square(LARGE_ARRAY_SIZE), LARGE_ARRAY_SIZE - 1, lodashStable.map(array.slice(1), square)];
|
|
||||||
|
var expected = [
|
||||||
|
square(LARGE_ARRAY_SIZE),
|
||||||
|
LARGE_ARRAY_SIZE - 1,
|
||||||
|
lodashStable.map(array.slice(1), square)
|
||||||
|
];
|
||||||
|
|
||||||
_(array).slice(1).takeRightWhile(function(value, index, array) {
|
_(array).slice(1).takeRightWhile(function(value, index, array) {
|
||||||
args = slice.call(arguments);
|
args = slice.call(arguments);
|
||||||
@@ -22962,15 +22984,15 @@
|
|||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
||||||
actual = _(array).slice(1).map(String).toArray().value();
|
|
||||||
|
|
||||||
assert.deepEqual(actual, lodashStable.map(array.slice(1), String));
|
|
||||||
|
|
||||||
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
||||||
return ['key' + index, index];
|
return ['key' + index, index];
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
var actual = _(array).slice(1).map(String).toArray().value();
|
||||||
|
assert.deepEqual(actual, lodashStable.map(array.slice(1), String));
|
||||||
|
|
||||||
actual = _(object).toArray().slice(1).map(String).value();
|
actual = _(object).toArray().slice(1).map(String).value();
|
||||||
assert.deepEqual(actual, _.map(_.toArray(object).slice(1), String));
|
assert.deepEqual(actual, _.map(_.toArray(object).slice(1), String));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user