mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 07:17:50 +00:00
Add _.inRange.
This commit is contained in:
committed by
jdalton
parent
540afb193b
commit
f243ebba91
@@ -9562,6 +9562,48 @@
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Checks if `n` is between `start` and up to but not including, `end`. If
|
||||
* `end` is not specified it defaults to `start` with `start` becoming `0`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Number
|
||||
* @param {number} n The number to check.
|
||||
* @param {number} [start=0] The start of the range.
|
||||
* @param {number} end The end of the range.
|
||||
* @returns {boolean} Returns `true` if `n` is in the range, else `false`.
|
||||
* @example
|
||||
*
|
||||
* _.inRange(3, 2, 4);
|
||||
* // => true
|
||||
*
|
||||
* _.inRange(4, 8);
|
||||
* // => true
|
||||
*
|
||||
* _.inRange(4, 2);
|
||||
* // => false
|
||||
*
|
||||
* _.inRange(2, 2);
|
||||
* // => false
|
||||
*
|
||||
* _.inRange(1.2, 2);
|
||||
* // => true
|
||||
*
|
||||
* _.inRange(5.2, 4);
|
||||
* // => false
|
||||
*/
|
||||
function inRange(value, start, end) {
|
||||
start = +start || 0;
|
||||
if (typeof end === 'undefined') {
|
||||
end = start;
|
||||
start = 0;
|
||||
} else {
|
||||
end = +end || 0;
|
||||
}
|
||||
return value >= start && value < end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a random number between `min` and `max` (inclusive). If only one
|
||||
* argument is provided a number between `0` and the given number is returned.
|
||||
@@ -10845,8 +10887,9 @@
|
||||
|
||||
/**
|
||||
* Creates an array of numbers (positive and/or negative) progressing from
|
||||
* `start` up to, but not including, `end`. If `start` is less than `end` a
|
||||
* zero-length range is created unless a negative `step` is specified.
|
||||
* `start` up to, but not including, `end`. If `end` is not specified it
|
||||
* defaults to `start` with `start` becoming `0`. If `start` is less than
|
||||
* `end` a zero-length range is created unless a negative `step` is specified.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -11131,6 +11174,7 @@
|
||||
lodash.identity = identity;
|
||||
lodash.includes = includes;
|
||||
lodash.indexOf = indexOf;
|
||||
lodash.inRange = inRange;
|
||||
lodash.isArguments = isArguments;
|
||||
lodash.isArray = isArray;
|
||||
lodash.isBoolean = isBoolean;
|
||||
|
||||
48
test/test.js
48
test/test.js
@@ -6091,6 +6091,50 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.inRange');
|
||||
|
||||
(function() {
|
||||
test('should work with an `end` argument', 3, function() {
|
||||
strictEqual(_.inRange(3, 5), true);
|
||||
strictEqual(_.inRange(5, 5), false);
|
||||
strictEqual(_.inRange(6, 5), false);
|
||||
});
|
||||
|
||||
test('should work with `start` and `end` arguments', 4, function() {
|
||||
strictEqual(_.inRange(1, 1, 5), true);
|
||||
strictEqual(_.inRange(3, 1, 5), true);
|
||||
strictEqual(_.inRange(0, 1, 5), false);
|
||||
strictEqual(_.inRange(5, 1, 5), false);
|
||||
});
|
||||
|
||||
test('should treat falsey `start` arguments as `0`', 13, function() {
|
||||
_.each(falsey, function(value, index) {
|
||||
if (index) {
|
||||
strictEqual(_.inRange(0, value), false);
|
||||
strictEqual(_.inRange(0, value, 1), true);
|
||||
} else {
|
||||
strictEqual(_.inRange(0), false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('should work with a floating point `n` value', 4, function() {
|
||||
strictEqual(_.inRange(0.5, 5), true);
|
||||
strictEqual(_.inRange(1.2, 1, 5), true);
|
||||
strictEqual(_.inRange(5.2, 5), false);
|
||||
strictEqual(_.inRange(0.5, 1, 5), false);
|
||||
});
|
||||
|
||||
test('should coerce arguments to finite numbers', 1, function() {
|
||||
var actual = [_.inRange(0, '0', 1), _.inRange(0, '1'), _.inRange(0, 0, '1'), _.inRange(0, NaN, 1), _.inRange(-1, -1, NaN)],
|
||||
expected = _.map(actual, _.constant(true));
|
||||
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.intersection');
|
||||
|
||||
(function() {
|
||||
@@ -11178,7 +11222,7 @@
|
||||
QUnit.module('lodash.range');
|
||||
|
||||
(function() {
|
||||
test('should work with a single `end` argument', 1, function() {
|
||||
test('should work with an `end` argument', 1, function() {
|
||||
deepEqual(_.range(4), [0, 1, 2, 3]);
|
||||
});
|
||||
|
||||
@@ -15482,7 +15526,7 @@
|
||||
|
||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||
|
||||
test('should accept falsey arguments', 208, function() {
|
||||
test('should accept falsey arguments', 209, function() {
|
||||
var emptyArrays = _.map(falsey, _.constant([])),
|
||||
isExposed = '_' in root,
|
||||
oldDash = root._;
|
||||
|
||||
Reference in New Issue
Block a user