From 8f3c832d7003b4ab725cfad06600f8c5acc3f78b Mon Sep 17 00:00:00 2001 From: EvertE Date: Tue, 6 Nov 2018 12:03:04 +0100 Subject: [PATCH] Fix bug with small floats and add support for scientific notation --- src/js/helpers/parseInput.js | 16 ++++++++++++++-- test/tests/js/helpers/parseInput-test.js | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/js/helpers/parseInput.js b/src/js/helpers/parseInput.js index 24030717..f2bd5db3 100644 --- a/src/js/helpers/parseInput.js +++ b/src/js/helpers/parseInput.js @@ -19,14 +19,26 @@ export default function parseInput(input) { input.match(/\-?\d+\.\d+/) && input.match(/\-?\d+\.\d+/)[0] === input ) { - //integer + //float return formatResponse('float', parseFloat(input)); + } else if ( + input.match(/\-?\d+e-\d+/) + && input.match(/\-?\d+e-\d+/)[0] === input + ) { + //scientific float + return formatResponse('float', Number(input)); } else if ( input.match(/\-?\d+/) && input.match(/\-?\d+/)[0] === input ) { - //float + //integer return formatResponse('integer', parseInt(input)); + } else if ( + input.match(/\-?\d+e\+\d+/) + && input.match(/\-?\d+e\+\d+/)[0] === input + ) { + //scientific integer + return formatResponse('integer', Number(input)); } } catch (e) { // no-op diff --git a/test/tests/js/helpers/parseInput-test.js b/test/tests/js/helpers/parseInput-test.js index 1cdd64c7..1d091afe 100644 --- a/test/tests/js/helpers/parseInput-test.js +++ b/test/tests/js/helpers/parseInput-test.js @@ -20,6 +20,14 @@ describe("parseInput", function() { expect(parseInput("5.22").type).to.equal("float") }) + it("parseInput small float", function() { + expect(parseInput("0.0000002").type).to.equal("float") + }) + + it("parseInput scientific notation float", function() { + expect(parseInput("2e-7").type).to.equal("float") + }) + it("parseInput date", function() { expect(parseInput("5/22").type).to.equal("date") }) @@ -28,6 +36,10 @@ describe("parseInput", function() { expect(parseInput("22").type).to.equal("integer") }) + it("parseInput scientific notation integer", function() { + expect(parseInput("4e+8").type).to.equal("integer") + }) + it("parseInput NaN", function() { expect(parseInput("nan").type).to.equal("nan")