inRange.js 1.0 KB

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