gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Fix isNumeric check in `numberTransformer`, fix #380 #381

Closed gbiryukov closed 7 years ago

gbiryukov commented 7 years ago

See #380 for more details

gcanti commented 7 years ago

So it will be great if deeply minified tcomb-form will work correctly

I agree, the new version behaves differently though

// old
function isNumeric(value) {
  const n = parseFloat(value)
  return (value - n + 1) >= 0
}

// new
function isNumeric2(value) {
  const n = parseFloat(value)
  return typeof n === 'number' && isFinite(n)
}

console.log(isNumeric('a')) // false
console.log(isNumeric('1')) // true
console.log(isNumeric('1.2')) // true
console.log(isNumeric('1.2a')) // false
console.log(isNumeric('1.a2')) // false
console.log(isNumeric('1a2')) // false

console.log(isNumeric2('a')) // false
console.log(isNumeric2('1')) // true
console.log(isNumeric2('1.2')) // true
console.log(isNumeric2('1.2a')) // true (!)
console.log(isNumeric2('1.a2')) // true (!)
console.log(isNumeric2('1a2')) // true (!)
gbiryukov commented 7 years ago

Yep, my bad! I've added tests to ensure that changes will be completely compatible with current implementation and updated the fix

gcanti commented 7 years ago

Looks good, thanks for the tests. I'll release a patch asap

gcanti commented 7 years ago

Patch released

gbiryukov commented 7 years ago

👍