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(string-utils): detect [0,1] ranged values as numbers #388

Merged
merged 5 commits into from Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/string-utils.ts
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
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