Skip to content

Commit

Permalink
fix(string): fix typecasting of boolean values
Browse files Browse the repository at this point in the history
Allow pass through of boolean values. true and false are considered
finite values and will be passed to parse float and return a NaN rather
than true / false
  • Loading branch information
esatterwhite committed Aug 10, 2021
1 parent ac86940 commit 42511bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/string/typecast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
module.exports = function typecast(value) {
if (value === 'null' || value === null) return null
if (value === 'undefined' || value === undefined) return undefined
if (value === 'true') return true
if (value === 'false') return false
if (value === 'true' || value === true) return true
if (value === 'false' || value === false) return false
if (value === '' || isNaN(value)) return value
if (isFinite(value)) return parseFloat(value)
return value
Expand Down
7 changes: 7 additions & 0 deletions test/unit/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ test('string', async (t) => {
}, {
value: 'false'
, expected: false
}, {
value: true
, expected: true
}, {
value: false
, expected: false
}, {
}, {
value: '123'
, expected: 123
Expand Down

0 comments on commit 42511bf

Please sign in to comment.