isSafeInteger.js 949 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var isInteger = require('./isInteger');
  2. /** Used as references for various `Number` constants. */
  3. var MAX_SAFE_INTEGER = 9007199254740991;
  4. /**
  5. * Checks if `value` is a safe integer. An integer is safe if it's an IEEE-754
  6. * double precision number which isn't the result of a rounded unsafe integer.
  7. *
  8. * **Note:** This method is based on
  9. * [`Number.isSafeInteger`](https://mdn.io/Number/isSafeInteger).
  10. *
  11. * @static
  12. * @memberOf _
  13. * @since 4.0.0
  14. * @category Lang
  15. * @param {*} value The value to check.
  16. * @returns {boolean} Returns `true` if `value` is a safe integer, else `false`.
  17. * @example
  18. *
  19. * _.isSafeInteger(3);
  20. * // => true
  21. *
  22. * _.isSafeInteger(Number.MIN_VALUE);
  23. * // => false
  24. *
  25. * _.isSafeInteger(Infinity);
  26. * // => false
  27. *
  28. * _.isSafeInteger('3');
  29. * // => false
  30. */
  31. function isSafeInteger(value) {
  32. return isInteger(value) && value >= -MAX_SAFE_INTEGER && value <= MAX_SAFE_INTEGER;
  33. }
  34. module.exports = isSafeInteger;