JAVASCRIPT   41

inRange.js

Guest on 23rd August 2021 04:58:02 PM

  1. /* Native method references for those with the same name as other `lodash` methods. */
  2. var nativeMax = Math.max,
  3.     nativeMin = Math.min;
  4.  
  5. /**
  6.  * Checks if `n` is between `start` and up to but not including, `end`. If
  7.  * `end` is not specified it's set to `start` with `start` then set to `0`.
  8.  *
  9.  * @static
  10.  * @memberOf _
  11.  * @category Number
  12.  * @param {number} n The number to check.
  13.  * @param {number} [start=0] The start of the range.
  14.  * @param {number} end The end of the range.
  15.  * @returns {boolean} Returns `true` if `n` is in the range, else `false`.
  16.  * @example
  17.  *
  18.  * _.inRange(3, 2, 4);
  19.  * // => true
  20.  *
  21.  * _.inRange(4, 8);
  22.  * // => true
  23.  *
  24.  * _.inRange(4, 2);
  25.  * // => false
  26.  *
  27.  * _.inRange(2, 2);
  28.  * // => false
  29.  *
  30.  * _.inRange(1.2, 2);
  31.  * // => true
  32.  *
  33.  * _.inRange(5.2, 4);
  34.  * // => false
  35.  */
  36. function inRange(value, start, end) {
  37.   start = +start || 0;
  38.   if (end === undefined) {
  39.     end = start;
  40.     start = 0;
  41.   } else {
  42.     end = +end || 0;
  43.   }
  44.   return value >= nativeMin(start, end) && value < nativeMax(start, end);
  45. }
  46.  
  47. module.exports = inRange;

Raw Paste


Login or Register to edit or fork this paste. It's free.