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