diff --git a/HISTORY.md b/HISTORY.md index 9fdcf6c..4757012 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,12 +3,6 @@ https://github.com/josdejong/jsoneditor -## not yet published, version 7.0.5 - -- Fix #794: fix discrepancy between valid JSON numbers and valid JavaScript - numbers (i.e. `+2` is a valid in JavaScript but not in JSON). - - ## 2019-09-11, version 7.0.4 - Fixed #723: schema error popup and color picker not always fully visible. diff --git a/src/js/util.js b/src/js/util.js index 79eb3bb..2ae529a 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -1345,14 +1345,10 @@ export function parseString (str) { return false } - try { - // will nicely fail for strings like '123ab', ' ', and '+1' - const num = JSON.parse(str) - if (typeof num === 'number' && !isNaN(num)) { - return num - } - } catch (err) { - // no need to handle this error, it was just to try parsing into a number + const num = Number(str) // will nicely fail with '123ab' + const numFloat = parseFloat(str) // will nicely fail with ' ' + if (!isNaN(num) && !isNaN(numFloat)) { + return num } return str diff --git a/test/util.test.js b/test/util.test.js index 8f967d9..76accec 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -399,7 +399,7 @@ describe('util', () => { assert.strictEqual(parseString('null'), null) assert.strictEqual(parseString('true'), true) assert.strictEqual(parseString('false'), false) - assert.strictEqual(parseString('+1'), '+1') + assert.strictEqual(parseString('+1'), 1) assert.strictEqual(parseString(' '), ' ') assert.strictEqual(parseString(''), '') assert.strictEqual(parseString('"foo"'), '"foo"')