mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Move _.range to the Utilities category.
This commit is contained in:
111
lodash.js
111
lodash.js
@@ -2833,61 +2833,6 @@
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an array of numbers (positive and/or negative) progressing from
|
|
||||||
* `start` up to but not including `end`. If `start` is less than `stop` a
|
|
||||||
* zero-length range is created unless a negative `step` is specified.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @memberOf _
|
|
||||||
* @category Arrays
|
|
||||||
* @param {number} [start=0] The start of the range.
|
|
||||||
* @param {number} end The end of the range.
|
|
||||||
* @param {number} [step=1] The value to increment or decrement by.
|
|
||||||
* @returns {Array} Returns the new array of numbers.
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* _.range(4);
|
|
||||||
* // => [0, 1, 2, 3]
|
|
||||||
*
|
|
||||||
* _.range(1, 5);
|
|
||||||
* // => [1, 2, 3, 4]
|
|
||||||
*
|
|
||||||
* _.range(0, 20, 5);
|
|
||||||
* // => [0, 5, 10, 15]
|
|
||||||
*
|
|
||||||
* _.range(0, -4, -1);
|
|
||||||
* // => [0, -1, -2, -3]
|
|
||||||
*
|
|
||||||
* _.range(1, 4, 0);
|
|
||||||
* // => [1, 1, 1]
|
|
||||||
*
|
|
||||||
* _.range(0);
|
|
||||||
* // => []
|
|
||||||
*/
|
|
||||||
function range(start, end, step) {
|
|
||||||
start = +start || 0;
|
|
||||||
step = step == null ? 1 : (+step || 0);
|
|
||||||
|
|
||||||
if (end == null) {
|
|
||||||
end = start;
|
|
||||||
start = 0;
|
|
||||||
} else {
|
|
||||||
end = +end || 0;
|
|
||||||
}
|
|
||||||
// use `Array(length)` so engines like Chakra and V8 avoid slower modes
|
|
||||||
// http://youtu.be/XAqIpGU8ZZk#t=17m25s
|
|
||||||
var index = -1,
|
|
||||||
length = nativeMax(0, ceil((end - start) / (step || 1))),
|
|
||||||
result = Array(length);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
result[index] = start;
|
|
||||||
start += step;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all elements from an array that the predicate returns truthy for
|
* Removes all elements from an array that the predicate returns truthy for
|
||||||
* and returns an array of removed elements. The predicate is bound to `thisArg`
|
* and returns an array of removed elements. The predicate is bound to `thisArg`
|
||||||
@@ -8019,6 +7964,62 @@
|
|||||||
return baseRandom(min, max);
|
return baseRandom(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an array of numbers (positive and/or negative) progressing from
|
||||||
|
* `start` up to but not including `end`. If `start` is less than `stop` a
|
||||||
|
* zero-length range is created unless a negative `step` is specified.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Utilities
|
||||||
|
* @param {number} [start=0] The start of the range.
|
||||||
|
* @param {number} end The end of the range.
|
||||||
|
* @param {number} [step=1] The value to increment or decrement by.
|
||||||
|
* @returns {Array} Returns the new array of numbers.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.range(4);
|
||||||
|
* // => [0, 1, 2, 3]
|
||||||
|
*
|
||||||
|
* _.range(1, 5);
|
||||||
|
* // => [1, 2, 3, 4]
|
||||||
|
*
|
||||||
|
* _.range(0, 20, 5);
|
||||||
|
* // => [0, 5, 10, 15]
|
||||||
|
*
|
||||||
|
* _.range(0, -4, -1);
|
||||||
|
* // => [0, -1, -2, -3]
|
||||||
|
*
|
||||||
|
* _.range(1, 4, 0);
|
||||||
|
* // => [1, 1, 1]
|
||||||
|
*
|
||||||
|
* _.range(0);
|
||||||
|
* // => []
|
||||||
|
*/
|
||||||
|
function range(start, end, step) {
|
||||||
|
start = +start || 0;
|
||||||
|
step = step == null ? 1 : (+step || 0);
|
||||||
|
|
||||||
|
if (end == null) {
|
||||||
|
end = start;
|
||||||
|
start = 0;
|
||||||
|
} else {
|
||||||
|
end = +end || 0;
|
||||||
|
}
|
||||||
|
// use `Array(length)` so engines like Chakra and V8 avoid slower modes
|
||||||
|
// http://youtu.be/XAqIpGU8ZZk#t=17m25s
|
||||||
|
var index = -1,
|
||||||
|
length = nativeMax(0, ceil((end - start) / (step || 1))),
|
||||||
|
result = Array(length);
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
result[index] = start;
|
||||||
|
start += step;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves the value of property `key` on `object`. If `key` is a function
|
* Resolves the value of property `key` on `object`. If `key` is a function
|
||||||
* it will be invoked with the `this` binding of `object` and its result
|
* it will be invoked with the `this` binding of `object` and its result
|
||||||
|
|||||||
Reference in New Issue
Block a user