| 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 | 'use strict'; |
| 6 | |
| 7 | module.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 | }; |