Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/convict/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ function isWindowsNamedPipe(x) {
return String(x).includes('\\\\.\\pipe\\')
}

/**
* Checks if x is a number
*
* @param {*} x
* @returns {Boolean}
*/
function isNumber(x) {
if (typeof x === 'number') {
return true
} else if (parseFloat(x)) {
return true
} else if (typeof x === 'string' && x === 'NaN') {
return true
}

return false
}

const types = {
'*': function() { },
int: function(x) {
Expand Down Expand Up @@ -406,7 +424,7 @@ function coerce(k, v, schema, instance) {
case 'integer':
case 'int': v = parseInt(v, 10); break
case 'port_or_windows_named_pipe': v = isWindowsNamedPipe(v) ? v : parseInt(v, 10); break
case 'number': v = parseFloat(v); break
case 'number': v = isNumber(v) ? parseFloat(v) : v; break
case 'boolean': v = String(v).toLowerCase() !== 'false'; break
case 'array': v = v.split(','); break
case 'object': v = JSON.parse(v); break
Expand Down