mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 17:47:49 +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) {
|
function range(start, end, step) {
|
||||||
step || (step = 1);
|
start = +start || 0;
|
||||||
|
step = +step || 1;
|
||||||
|
|
||||||
if (end == null) {
|
if (end == null) {
|
||||||
end = start || 0;
|
end = start;
|
||||||
start = 0;
|
start = 0;
|
||||||
}
|
}
|
||||||
// use `Array(length)` so V8 will avoid the slower "dictionary" mode
|
// 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 */
|
/** Shortcut used to make object properties immutable */
|
||||||
var freeze = Object.freeze;
|
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');
|
QUnit.module('lodash.reduceRight');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -896,12 +931,12 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should allow a falsey `object` argument', function() {
|
test('should allow a falsey `object` argument', function() {
|
||||||
var func = _.size;
|
_.each(falsey, function(index, value) {
|
||||||
try {
|
try {
|
||||||
var actual = [func(), func(undefined), func(null), func(false), func(0)];
|
var actual = index ? _.size(value) : _.size();
|
||||||
} catch(e) { }
|
} catch(e) { }
|
||||||
|
equal(actual, 0);
|
||||||
deepEqual(actual, [0, 0, 0, 0, 0]);
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with an object that has a `length` property', function() {
|
test('should work with an object that has a `length` property', function() {
|
||||||
@@ -1217,15 +1252,14 @@
|
|||||||
var func = _[methodName],
|
var func = _[methodName],
|
||||||
pass = true;
|
pass = true;
|
||||||
|
|
||||||
try {
|
_.each(falsey, function(value, index) {
|
||||||
func();
|
try {
|
||||||
func(undefined);
|
index ? func() : func(value);
|
||||||
func(null);
|
} catch(e) {
|
||||||
func(false);
|
pass = false;
|
||||||
func(0);
|
}
|
||||||
} catch(e) {
|
});
|
||||||
pass = false;
|
|
||||||
}
|
|
||||||
ok(pass, methodName + ' allows a falsey `array` argument');
|
ok(pass, methodName + ' allows a falsey `array` argument');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1256,23 +1290,18 @@
|
|||||||
identity = _.identity,
|
identity = _.identity,
|
||||||
pass = true;
|
pass = true;
|
||||||
|
|
||||||
try {
|
_.each(falsey, function(value, index) {
|
||||||
if (/^(?:contains|toArray)$/.test(methodName)) {
|
try {
|
||||||
func();
|
if (/^(?:contains|toArray)$/.test(methodName)) {
|
||||||
func(undefined);
|
index ? func() : func(value);
|
||||||
func(null);
|
} else if (index) {
|
||||||
func(false);
|
func(value, identity);
|
||||||
func(0);
|
}
|
||||||
|
} 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');
|
ok(pass, methodName + ' allows a falsey `collection` argument');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user