blob: f018bd41c9dee7033f86428bb8ae178b771acf49 [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*/
5export default function isNaturalNumber(val, option) {
6 if (option) {
7 if (typeof option !== 'object') {
8 throw new TypeError(
9 String(option) +
10 ' is not an object. Expected an object that has boolean `includeZero` property.'
11 );
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
22 if (option.includeZero && val === 0) {
23 return true;
24 }
25 }
26 }
27
28 return Number.isSafeInteger(val) && val >= 1;
29}