gcanti / tcomb-form

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

Bug in numberTransformer #380

Closed gbiryukov closed 7 years ago

gbiryukov commented 7 years ago

Version

Expected behaviour

Empty Textbox with Number type should return null after validation even if source of library minified by UglifyJS.

Actual behaviour

Empty Textbox with Number type returns NaN after validation.

Steps to reproduce

  1. Minify project by UglifyJS
  2. Call validate() on Textbox instance and inspect returned result. Actual value will be reported as NaN

Details

Just found weird issue with numberTransformer that occurred after library compression by UglifyJS. Empty Textbox value reported as NaN after validate call.

It caused by incorrect isNumeric detection logic in parseNumber function used in numberTransformer. For empty string parseFloat will return NaN that can't be correctly compared with zero. Result always will be false regardless of operator < or >. After minification (with unsafe_comps option)

function parseNumber(value) {
  const n = parseFloat(value)
  const isNumeric = (value - n + 1) >= 0
  return isNumeric ? n : toNull(value)
}

will became

function c(e) {
  var t = parseFloat(e);
  return 0 > e - t + 1 ? l(e) : t
}

So compare operator has changed but result still the same – false and finally NaN is returned. Hope my explanation is clear. So it will be great if deeply minified tcomb-form will work correctly.