mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
Ensure _.slice coerces start and end arguments appropriately.
This commit is contained in:
12
lodash.js
12
lodash.js
@@ -2975,21 +2975,19 @@
|
||||
var index = -1,
|
||||
length = array ? array.length : 0;
|
||||
|
||||
if (typeof start == 'undefined') {
|
||||
start = 0;
|
||||
} else if (start < 0) {
|
||||
start |= 0;
|
||||
if (start < 0) {
|
||||
start = nativeMax(length + start, 0);
|
||||
} else if (start > length) {
|
||||
start = length;
|
||||
}
|
||||
if (typeof end == 'undefined') {
|
||||
end = length;
|
||||
} else if (end < 0) {
|
||||
end = typeof end == 'undefined' ? length : (end | 0);
|
||||
if (end < 0) {
|
||||
end = nativeMax(length + end, 0);
|
||||
} else if (end > length) {
|
||||
end = length;
|
||||
}
|
||||
length = (length = (end - start) | 0) < 0 ? 0 : length;
|
||||
length = start > end ? 0 : (end - start);
|
||||
|
||||
var result = Array(length);
|
||||
while (++index < length) {
|
||||
|
||||
11
test/test.js
11
test/test.js
@@ -6602,8 +6602,6 @@
|
||||
QUnit.module('lodash.range');
|
||||
|
||||
(function() {
|
||||
var func = _.range;
|
||||
|
||||
test('should work when passing a single `end` argument', 1, function() {
|
||||
deepEqual(_.range(4), [0, 1, 2, 3]);
|
||||
});
|
||||
@@ -6640,8 +6638,8 @@
|
||||
});
|
||||
});
|
||||
|
||||
test('should coerce arguments to numbers', 1, function() {
|
||||
var actual = [func('0',1), func('1'), func(0, 1, '1'), func(NaN), func(NaN, NaN)];
|
||||
test('should coerce arguments to finite numbers', 1, function() {
|
||||
var actual = [_.range('0', 1), _.range('1'), _.range(0, 1, '1'), _.range(NaN), _.range(NaN, NaN)];
|
||||
deepEqual(actual, [[0], [0], [0], [], []]);
|
||||
});
|
||||
}());
|
||||
@@ -7411,6 +7409,11 @@
|
||||
deepEqual(_.slice(array, 0, end), []);
|
||||
});
|
||||
});
|
||||
|
||||
test('should coerce `start` and `end` to finite numbers', 1, function() {
|
||||
var actual = [_.slice(array, '0', 1), _.slice(array, 0, '1'), _.slice(array, '1'), _.slice(array, NaN, 1), _.slice(array, 1, NaN)];
|
||||
deepEqual(actual, [[1], [1], [2, 3], [1], []]);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user