env-var
Version:
Verification, sanitization, and type coercion for environment variables in Node.js
14 lines (10 loc) • 347 B
JavaScript
module.exports = function asFloat (value) {
const n = parseFloat(value)
// Some values are parsed as valid floats despite being obviously invalid, e.g. "1.o" or "192.168.1.1".
// In these cases we would want to throw an error.
if (isNaN(n) || isNaN(value)) {
throw new Error('should be a valid float')
}
return n
}