spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Moving helper.number to Helpers.number, adding tests for parsing number-like strings
author
Ben Vogt <[email protected]>
date
2017-01-02 20:47:01
stats
4 file(s) changed, 43 insertions(+), 12 deletions(-)
files
README.md
src/Helpers.ts
src/Sheet.ts
tests/HelperTest.ts
 1diff --git a/README.md b/README.md
 2index f59ccc3..77dd672 100644
 3--- a/README.md
 4+++ b/README.md
 5@@ -14,4 +14,4 @@ Things I should do.
 6 * If I add a cell with a decimal, what's the most precision I can get?
 7 
 8 ### Parsing interesting values
 9-* `10 + 10 + 10` should be parsed to 0, or null, not 10. We should do some regex checking before calling `parseInt`
10+* Expand tests greatly.
11\ No newline at end of file
12diff --git a/src/Helpers.ts b/src/Helpers.ts
13new file mode 100644
14index 0000000..6a4b18f
15--- /dev/null
16+++ b/src/Helpers.ts
17@@ -0,0 +1,27 @@
18+var Helpers = {
19+  number: function (num) {
20+    var reg = new RegExp('(^[-+]?([0-9]+)(\.[0-9]+)?)$');
21+    switch (typeof num) {
22+      case 'number':
23+        return num;
24+      case 'string':
25+        var t = num.trim();
26+        if (!isNaN(t)) {
27+          if (reg.test(t.trim())) {
28+            if (t.indexOf('.') > -1) {
29+              return parseFloat(t);
30+            } else {
31+              return parseInt(t, 10);
32+            }
33+          } else {
34+            return 0;
35+          }
36+        }
37+    }
38+    return num;
39+  }
40+};
41+
42+export {
43+  Helpers
44+}
45\ No newline at end of file
46diff --git a/src/Sheet.ts b/src/Sheet.ts
47index b0630bf..ed87e42 100644
48--- a/src/Sheet.ts
49+++ b/src/Sheet.ts
50@@ -4,6 +4,7 @@ import { SUPPORTED_FORMULAS } from "./SupportedFormulas"
51 import { Cell } from "./Cell"
52 import { Errors } from "./Errors"
53 import * as Formula from "formulajs"
54+import { Helpers } from "./Helpers";
55 
56 /**
57  * Model representing a spreadsheet. When values/cells are added, dependencies recalculated, and true-values of those
58@@ -341,18 +342,7 @@ var Sheet = (function () {
59   };
60 
61   var helper = {
62-    number: function (num) {
63-      switch (typeof num) {
64-        case 'number':
65-          return num;
66-        case 'string':
67-          if (!isNaN(num)) {
68-            return num.indexOf('.') > -1 ? parseFloat(num) : parseInt(num, 10);
69-          }
70-      }
71-
72-      return num;
73-    },
74+    number: Helpers.number,
75 
76     string: function (str) {
77       return str.substring(1, str.length - 1);
78diff --git a/tests/HelperTest.ts b/tests/HelperTest.ts
79new file mode 100644
80index 0000000..734ddbe
81--- /dev/null
82+++ b/tests/HelperTest.ts
83@@ -0,0 +1,14 @@
84+import { Helpers } from "../src/Helpers";
85+import {assertEquals,} from "./utils/Asserts"
86+
87+// Test Helpers.number
88+assertEquals(-1, Helpers.number("-1"));
89+assertEquals(1, Helpers.number("+1"));
90+assertEquals(1.8239817293871983e+28, Helpers.number("18239817293871982379817157562"));
91+assertEquals(1.8239817293871983e+28, Helpers.number("+18239817293871982379817157562"));
92+assertEquals(-1.8239817293871983e+28, Helpers.number("-18239817293871982379817157562"));
93+assertEquals(0, Helpers.number("10."));
94+assertEquals(111, Helpers.number("000111"));
95+assertEquals("10 + 10", Helpers.number("10 + 10"));
96+assertEquals("10/10", Helpers.number("10/10"));
97+assertEquals("10*10", Helpers.number("10*10"));