mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Make _.range coerce arguments to numbers.
Former-commit-id: 5408800fef0bc8a418c298a112049232b6d85e78
This commit is contained in:
@@ -1695,9 +1695,11 @@
|
||||
* // => []
|
||||
*/
|
||||
function range(start, end, step) {
|
||||
step || (step = 1);
|
||||
start = +start || 0;
|
||||
step = +step || 1;
|
||||
|
||||
if (end == null) {
|
||||
end = start || 0;
|
||||
end = start;
|
||||
start = 0;
|
||||
}
|
||||
// use `Array(length)` so V8 will avoid the slower "dictionary" mode
|
||||
|
||||
91
test/test.js
91
test/test.js
@@ -27,6 +27,17 @@
|
||||
_._ || _
|
||||
);
|
||||
|
||||
/** Used to pass falsey values to methods */
|
||||
var falsey = [
|
||||
,
|
||||
'',
|
||||
0,
|
||||
false,
|
||||
NaN,
|
||||
null,
|
||||
undefined
|
||||
];
|
||||
|
||||
/** Shortcut used to make object properties immutable */
|
||||
var freeze = Object.freeze;
|
||||
|
||||
@@ -829,6 +840,30 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.range');
|
||||
|
||||
(function() {
|
||||
var func = _.range;
|
||||
|
||||
test('should treat falsey `start` arguments as `0`', function() {
|
||||
_.each(falsey, function(value, index) {
|
||||
if (index) {
|
||||
deepEqual(_.range(value), []);
|
||||
deepEqual(_.range(value, 1), [0]);
|
||||
} else {
|
||||
deepEqual(_.range(), []);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('should coerce arguments to numbers', function() {
|
||||
var actual = [func('0',1), func('1'), func(0, 1, '1')];
|
||||
deepEqual(actual, [[0], [0], [0]]);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.reduceRight');
|
||||
|
||||
(function() {
|
||||
@@ -896,12 +931,12 @@
|
||||
});
|
||||
|
||||
test('should allow a falsey `object` argument', function() {
|
||||
var func = _.size;
|
||||
try {
|
||||
var actual = [func(), func(undefined), func(null), func(false), func(0)];
|
||||
} catch(e) { }
|
||||
|
||||
deepEqual(actual, [0, 0, 0, 0, 0]);
|
||||
_.each(falsey, function(index, value) {
|
||||
try {
|
||||
var actual = index ? _.size(value) : _.size();
|
||||
} catch(e) { }
|
||||
equal(actual, 0);
|
||||
})
|
||||
});
|
||||
|
||||
test('should work with an object that has a `length` property', function() {
|
||||
@@ -1217,15 +1252,14 @@
|
||||
var func = _[methodName],
|
||||
pass = true;
|
||||
|
||||
try {
|
||||
func();
|
||||
func(undefined);
|
||||
func(null);
|
||||
func(false);
|
||||
func(0);
|
||||
} catch(e) {
|
||||
pass = false;
|
||||
}
|
||||
_.each(falsey, function(value, index) {
|
||||
try {
|
||||
index ? func() : func(value);
|
||||
} catch(e) {
|
||||
pass = false;
|
||||
}
|
||||
});
|
||||
|
||||
ok(pass, methodName + ' allows a falsey `array` argument');
|
||||
});
|
||||
});
|
||||
@@ -1256,23 +1290,18 @@
|
||||
identity = _.identity,
|
||||
pass = true;
|
||||
|
||||
try {
|
||||
if (/^(?:contains|toArray)$/.test(methodName)) {
|
||||
func();
|
||||
func(undefined);
|
||||
func(null);
|
||||
func(false);
|
||||
func(0);
|
||||
_.each(falsey, function(value, index) {
|
||||
try {
|
||||
if (/^(?:contains|toArray)$/.test(methodName)) {
|
||||
index ? func() : func(value);
|
||||
} else if (index) {
|
||||
func(value, identity);
|
||||
}
|
||||
} catch(e) {
|
||||
pass = false;
|
||||
}
|
||||
else {
|
||||
func(undefined, identity);
|
||||
func(null, identity);
|
||||
func(false, identity);
|
||||
func(0, identity);
|
||||
}
|
||||
} catch(e) {
|
||||
pass = false;
|
||||
}
|
||||
});
|
||||
|
||||
ok(pass, methodName + ' allows a falsey `collection` argument');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user