rili-live / react-native-phone-input

Phone input box for React Native
https://www.npmjs.com/package/react-native-phone-input
MIT License
44 stars 55 forks source link

Fix checking a symbol is a number #2

Closed kovetskiy closed 3 years ago

kovetskiy commented 3 years ago
Number.isFinite() is different from the global isFinite() function. The global isFinite() function converts the tested value to a Number, then tests it.

Number.isFinite() does not convert the values to a Number, and will not return true for any value that is not of the type Number.

Well, it turns out, the library never really worked.

const is = (n) => !Number.isNaN(parseFloat(n)) && Number.isFinite(n);

is(1)
true

is('1')
false

is('1.0')
false
n='1'; console.log('Number.isFinite', Number.isFinite(n), 'isFinite', isFinite(n))
Number.isFinite false isFinite true
zizzle6717 commented 3 years ago

Thanks for finding this bug. I've fixed this by converting the string to a number before calling Number.isFinite() on it and published a new version. I'd prefer to use Number.isFinite as it is best practice and makes the linter happy :)