is-natural-number
Advanced tools
Comparing version 3.0.0 to 4.0.0
26
index.js
@@ -7,4 +7,26 @@ /*! | ||
module.exports = function isNaturalNumber(val, zero) { | ||
return val >= (zero ? 0 : 1) && Number.isSafeInteger(val); | ||
module.exports = function isNaturalNumber(val, option) { | ||
if (option) { | ||
if (typeof option !== 'object') { | ||
throw new TypeError( | ||
String(option) + | ||
' is not an object. Expected an object that has boolean `includeZero` property.' | ||
); | ||
} | ||
if ('includeZero' in option) { | ||
if (typeof option.includeZero !== 'boolean') { | ||
throw new TypeError( | ||
String(option.includeZero) + | ||
' is neither true nor false. `includeZero` option must be a Boolean value.' | ||
); | ||
} | ||
if (option.includeZero && val === 0) { | ||
return true; | ||
} | ||
} | ||
} | ||
return val >= 1 && Number.isSafeInteger(val); | ||
}; |
@@ -5,4 +5,26 @@ /*! | ||
*/ | ||
export default function isNaturalNumber(val, zero) { | ||
return val >= (zero ? 0 : 1) && Number.isSafeInteger(val); | ||
export default function isNaturalNumber(val, option) { | ||
if (option) { | ||
if (typeof option !== 'object') { | ||
throw new TypeError( | ||
String(option) + | ||
' is not an object. Expected an object that has boolean `includeZero` property.' | ||
); | ||
} | ||
if ('includeZero' in option) { | ||
if (typeof option.includeZero !== 'boolean') { | ||
throw new TypeError( | ||
String(option.includeZero) + | ||
' is neither true nor false. `includeZero` option must be a Boolean value.' | ||
); | ||
} | ||
if (option.includeZero && val === 0) { | ||
return true; | ||
} | ||
} | ||
} | ||
return val >= 1 && Number.isSafeInteger(val); | ||
} |
{ | ||
"name": "is-natural-number", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Check if a value is a natural number", | ||
@@ -5,0 +5,0 @@ "repository": "shinnn/is-natural-number.js", |
3645
53