inRange.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var baseInRange = require('./_baseInRange'),
  2. toFinite = require('./toFinite'),
  3. toNumber = require('./toNumber');
  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. * If `start` is greater than `end` the params are swapped to support
  8. * negative ranges.
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 3.3.0
  13. * @category Number
  14. * @param {number} number The number to check.
  15. * @param {number} [start=0] The start of the range.
  16. * @param {number} end The end of the range.
  17. * @returns {boolean} Returns `true` if `number` is in the range, else `false`.
  18. * @see _.range, _.rangeRight
  19. * @example
  20. *
  21. * _.inRange(3, 2, 4);
  22. * // => true
  23. *
  24. * _.inRange(4, 8);
  25. * // => true
  26. *
  27. * _.inRange(4, 2);
  28. * // => false
  29. *
  30. * _.inRange(2, 2);
  31. * // => false
  32. *
  33. * _.inRange(1.2, 2);
  34. * // => true
  35. *
  36. * _.inRange(5.2, 4);
  37. * // => false
  38. *
  39. * _.inRange(-3, -2, -6);
  40. * // => true
  41. */
  42. function inRange(number, start, end) {
  43. start = toFinite(start);
  44. if (end === undefined) {
  45. end = start;
  46. start = 0;
  47. } else {
  48. end = toFinite(end);
  49. }
  50. number = toNumber(number);
  51. return baseInRange(number, start, end);
  52. }
  53. module.exports = inRange;