Skip to content

Commit

Permalink
Fix parseInt & parseFloat result is NaN when value is not a number, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
welefen committed Sep 27, 2017
1 parent f4851d9 commit c2dc66f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module.exports = class MysqlSchema extends Schema {
*/
parseType(tinyType, value) {
if (tinyType === 'enum' || tinyType === 'set' || tinyType === 'bigint') return value;
if (tinyType.indexOf('int') > -1) return parseInt(value, 10);
if (['double', 'float', 'decimal'].indexOf(tinyType) > -1) return parseFloat(value, 10);
if (tinyType.indexOf('int') > -1) return parseInt(value, 10) || 0;
if (['double', 'float', 'decimal'].indexOf(tinyType) > -1) return parseFloat(value, 10) || 0;
if (tinyType === 'bool') return value ? 1 : 0;
return value;
}
Expand Down
2 changes: 2 additions & 0 deletions test/lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,10 @@ test('schema parse type', t => {
t.is(schema.parseType('set', 'True'), 'True');
t.is(schema.parseType('bigint', 'False'), 'False');
t.is(schema.parseType('int(10)', '3'), 3);
t.is(schema.parseType('int(10)', 'fasdfadf'), 0);
t.is(schema.parseType('double', '3.3'), 3.3);
t.is(schema.parseType('float', '3.3'), 3.3);
t.is(schema.parseType('float', 'fasdfasdf'), 0);
t.is(schema.parseType('decimal', '3.3'), 3.3);
t.is(schema.parseType('bool', '0'), 1);
t.is(schema.parseType('bool', ''), 0);
Expand Down

0 comments on commit c2dc66f

Please sign in to comment.