mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Infer sign of range's step when only start and end are provided.
This commit is contained in:
@@ -13282,11 +13282,10 @@
|
|||||||
if (end === undefined) {
|
if (end === undefined) {
|
||||||
end = start;
|
end = start;
|
||||||
start = 0;
|
start = 0;
|
||||||
step = step === undefined ? (start < end ? 1 : -1) : step;
|
|
||||||
} else {
|
} else {
|
||||||
end = toNumber(end) || 0;
|
end = toNumber(end) || 0;
|
||||||
}
|
}
|
||||||
step = step === undefined ? 1 : (toNumber(step) || 0);
|
step = step === undefined ? (start < end ? 1 : -1) : (toNumber(step) || 0);
|
||||||
|
|
||||||
var n = nativeMax(nativeCeil((end - start) / (step || 1)), 0);
|
var n = nativeMax(nativeCeil((end - start) / (step || 1)), 0);
|
||||||
return baseTimes(n, function(index) {
|
return baseTimes(n, function(index) {
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -15555,27 +15555,25 @@
|
|||||||
QUnit.module('lodash.range');
|
QUnit.module('lodash.range');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
QUnit.test('should work with only an `end` argument', function(assert) {
|
QUnit.test('should infer the sign of `step` when provided only an `end` argument', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(2);
|
||||||
|
|
||||||
assert.deepEqual(_.range(4), [0, 1, 2, 3]);
|
assert.deepEqual(_.range(4), [0, 1, 2, 3]);
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should use a `step` of `-1` when provided only a negative `end` argument', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
assert.deepEqual(_.range(-4), [0, -1, -2, -3]);
|
assert.deepEqual(_.range(-4), [0, -1, -2, -3]);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should work with `start` and `end` arguments', function(assert) {
|
QUnit.test('should infer the sign of `step` when provided only a `start` and `end` argument', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(2);
|
||||||
|
|
||||||
assert.deepEqual(_.range(1, 5), [1, 2, 3, 4]);
|
assert.deepEqual(_.range(1, 5), [1, 2, 3, 4]);
|
||||||
|
assert.deepEqual(_.range(5, 1), [5, 4, 3, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should work with `start`, `end`, and `step` arguments', function(assert) {
|
QUnit.test('should work with `start`, `end`, and `step` arguments', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(3);
|
||||||
|
|
||||||
|
assert.deepEqual(_.range(0, -4, -1), [0, -1, -2, -3]);
|
||||||
|
assert.deepEqual(_.range(5, 1, -1), [5, 4, 3, 2]);
|
||||||
assert.deepEqual(_.range(0, 20, 5), [0, 5, 10, 15]);
|
assert.deepEqual(_.range(0, 20, 5), [0, 5, 10, 15]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -77,6 +77,9 @@
|
|||||||
'an array of pairs zipped together into an object',
|
'an array of pairs zipped together into an object',
|
||||||
'an object converted to pairs and back to an object'
|
'an object converted to pairs and back to an object'
|
||||||
],
|
],
|
||||||
|
'range': [
|
||||||
|
'range with two arguments a & b, b<a generates an empty array'
|
||||||
|
],
|
||||||
'rest': [
|
'rest': [
|
||||||
'working rest(0)',
|
'working rest(0)',
|
||||||
'rest can take an index',
|
'rest can take an index',
|
||||||
|
|||||||
Reference in New Issue
Block a user