Skip to content

Commit

Permalink
fix(string-utils): detect [0,1] ranged values as numbers (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Jun 20, 2021
1 parent 49cf368 commit efcc32c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export function looksLikeNumber (x: null | undefined | number | string): boolean
// hexadecimal.
if (/^0x[0-9a-f]+$/i.test(x)) return true
// don't treat 0123 as a number; as it drops the leading '0'.
if (x.length > 1 && x[0] === '0') return false
if (/^0[^.]/.test(x)) return false
return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
}
5 changes: 4 additions & 1 deletion test/string-utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ describe('string-utils', function () {
it('it detects strings that could be parsed as numbers', () => {
strictEqual(looksLikeNumber('3293'), true)
strictEqual(looksLikeNumber('0x10'), true)
strictEqual(looksLikeNumber('0x10'), true)
strictEqual(looksLikeNumber('.1'), true)
strictEqual(looksLikeNumber('0.1'), true)
strictEqual(looksLikeNumber('0.10'), true)

strictEqual(looksLikeNumber('00.1'), false)
strictEqual(looksLikeNumber('0100'), false)
strictEqual(looksLikeNumber('apple'), false)
})
Expand Down

0 comments on commit efcc32c

Please sign in to comment.