Skip to content

Commit 87011cd

Browse files
authored
Merge pull request mac-s-g#231 from EvertEt/fix/scientific-float
Fix bug with small floats and add support for scientific notation.
2 parents 46f0a72 + 8f3c832 commit 87011cd

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/js/helpers/parseInput.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ export default function parseInput(input) {
1919
input.match(/\-?\d+\.\d+/)
2020
&& input.match(/\-?\d+\.\d+/)[0] === input
2121
) {
22-
//integer
22+
//float
2323
return formatResponse('float', parseFloat(input));
24+
} else if (
25+
input.match(/\-?\d+e-\d+/)
26+
&& input.match(/\-?\d+e-\d+/)[0] === input
27+
) {
28+
//scientific float
29+
return formatResponse('float', Number(input));
2430
} else if (
2531
input.match(/\-?\d+/)
2632
&& input.match(/\-?\d+/)[0] === input
2733
) {
28-
//float
34+
//integer
2935
return formatResponse('integer', parseInt(input));
36+
} else if (
37+
input.match(/\-?\d+e\+\d+/)
38+
&& input.match(/\-?\d+e\+\d+/)[0] === input
39+
) {
40+
//scientific integer
41+
return formatResponse('integer', Number(input));
3042
}
3143
} catch (e) {
3244
// no-op

test/tests/js/helpers/parseInput-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ describe("parseInput", function() {
2020
expect(parseInput("5.22").type).to.equal("float")
2121
})
2222

23+
it("parseInput small float", function() {
24+
expect(parseInput("0.0000002").type).to.equal("float")
25+
})
26+
27+
it("parseInput scientific notation float", function() {
28+
expect(parseInput("2e-7").type).to.equal("float")
29+
})
30+
2331
it("parseInput date", function() {
2432
expect(parseInput("5/22").type).to.equal("date")
2533
})
@@ -28,6 +36,10 @@ describe("parseInput", function() {
2836
expect(parseInput("22").type).to.equal("integer")
2937
})
3038

39+
it("parseInput scientific notation integer", function() {
40+
expect(parseInput("4e+8").type).to.equal("integer")
41+
})
42+
3143
it("parseInput NaN", function() {
3244
expect(parseInput("nan").type).to.equal("nan")
3345

0 commit comments

Comments
 (0)