index.js 792 B

12345678910111213141516171819202122232425262728293031
  1. /*!
  2. * is-natural-number.js | MIT (c) Shinnosuke Watanabe
  3. * https://github.com/shinnn/is-natural-number.js
  4. */
  5. 'use strict';
  6. module.exports = function isNaturalNumber(val, option) {
  7. if (option) {
  8. if (typeof option !== 'object') {
  9. throw new TypeError(
  10. String(option) +
  11. ' is not an object. Expected an object that has boolean `includeZero` property.'
  12. );
  13. }
  14. if ('includeZero' in option) {
  15. if (typeof option.includeZero !== 'boolean') {
  16. throw new TypeError(
  17. String(option.includeZero) +
  18. ' is neither true nor false. `includeZero` option must be a Boolean value.'
  19. );
  20. }
  21. if (option.includeZero && val === 0) {
  22. return true;
  23. }
  24. }
  25. }
  26. return Number.isSafeInteger(val) && val >= 1;
  27. };