Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1.0 not recognized as float #166

Merged
merged 3 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lib/accessors/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module.exports = function asFloat (value) {
const n = parseFloat(value)

if (isNaN(n) || n.toString() !== 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)) {
dror-weiss marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('should be a valid float')
}

Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('env-var', function () {
INVALID_BASE_64: 'a|GV-sb*G8=',
STRING: 'oh hai',
FLOAT: '12.43',
FLOAT_INTEGER: '1.0',
dror-weiss marked this conversation as resolved.
Show resolved Hide resolved
INTEGER: '5',
BOOL: 'false',
JSON: '{"name":"value"}',
Expand Down Expand Up @@ -227,6 +228,15 @@ describe('env-var', function () {
it('should return a float', function () {
expect(mod.get('FLOAT').asFloat()).to.be.a('number')
expect(mod.get('FLOAT').asFloat()).to.equal(parseFloat(TEST_VARS.FLOAT))
expect(mod.get('FLOAT_INTEGER').asFloat()).to.equal(parseFloat(TEST_VARS.FLOAT_INTEGER))
})

it('should throw an exception - non float found', function () {
process.env.FLOAT = '1.o'

expect(function () {
mod.get('FLOAT').asFloat()
}).to.throw()
})

it('should throw an exception - non float found', function () {
Expand Down