| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /*! |
| 2 | * is-natural-number.js | MIT (c) Shinnosuke Watanabe |
| 3 | * https://github.com/shinnn/is-natural-number.js |
| 4 | */ |
| 5 | export 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 | } |