commit
message
[ALL] refactoring the way formulas are organized and named. tests still pass.
author
Ben Vogt <[email protected]>
date
2017-04-29 23:10:26
stats
21 file(s) changed,
29 insertions(+),
99 deletions(-)
files
README.md
src/Formulas.ts
src/Main.ts
src/RawFormulas/Date.ts
src/Formulas/Date.ts
src/RawFormulas/Engineering.ts
src/Formulas/Engineering.ts
src/RawFormulas/Financial.ts
src/Formulas/Financial.ts
src/RawFormulas/Logical.ts
src/Formulas/Logical.ts
src/RawFormulas/Math.ts
src/Formulas/Math.ts
src/RawFormulas/RawFormulas.ts
src/Formulas/AllFormulas.ts
src/RawFormulas/Statistical.ts
src/Formulas/Statistical.ts
src/RawFormulas/Text.ts
src/Formulas/Text.ts
src/RawFormulas/Utils.ts
src/Formulas/Utils.ts
tests/DateFormulasTest.ts
tests/DateFormulasTestTimeOverride.ts
tests/EngineeringTest.ts
tests/FinancialTest.ts
tests/LogicalTest.ts
tests/MathTest.ts
tests/SheetFormulaTest.ts
tests/StatisticalTest.ts
tests/TextTest.ts
1diff --git a/README.md b/README.md
2index 77ea268..c2e8def 100644
3--- a/README.md
4+++ b/README.md
5@@ -10,7 +10,6 @@ And the same for MAX, MAXA, COUNT, COUNTA, etc. Look these over.
6 ### Criteria evaluations should escape reg-ex characters
7 http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
8
9-
10 ### jStat functions should know their caller
11 Either through `arguments`, or directly passed in like `mean("FORMULA", [10, 20, 30])`
12
13@@ -19,9 +18,6 @@ Although dollar functions look like they just format `number`s, it seems like th
14 This means that we should do dollar->number casting in all casting functions. For now, just using number primitive.
15 See `DOLLAR` function for more info.
16
17-### Get a better way to tie formulas into single export
18-Listing them inside RawFormulas.ts is unwieldy.
19-
20 ### Error formatting
21 Pass name of calling formula into all functions that throw user-facing errors, or have some sort of error mapper.
22
23diff --git a/src/Formulas.ts b/src/Formulas.ts
24index 7eee42c..78ea988 100644
25--- a/src/Formulas.ts
26+++ b/src/Formulas.ts
27@@ -1,37 +1,16 @@
28-import * as Formula from "formulajs"
29-import * as RawFormulas from "./RawFormulas/RawFormulas";
30-
31-const SUPPORTED_FORMULAS = [
32- 'ABS', 'ACCRINT', 'ACOS', 'ACOSH', 'ACOTH', 'AND', 'ARABIC', 'ASIN', 'ASINH', 'ATAN', 'ATAN2', 'ATANH', 'AVEDEV', 'AVERAGE', 'AVERAGEA', 'AVERAGEIF',
33- 'BIN2DEC', 'BIN2HEX', 'BIN2OCT', 'BINOMDIST',
34- 'CEILING', 'CHAR', 'CODE', 'COMBIN', 'COMBINA', 'COMPLEX', 'CONCATENATE', 'CONFIDENCE', 'CONVERT', 'CORREL', 'COS', 'COSH', 'COT', 'COTH', 'COUNT', 'COUNTA', 'COUNTBLANK', 'COUNTIF', 'COUNTIFS', 'COUNTUNIQUE', 'COVARIANCEP', 'COVARIANCES', 'CUMIPMT', 'CUMPRINC',
35- 'DATE', 'DATEVALUE', 'DAY', 'DAYS', 'DAYS360', 'DB', 'DDB', 'DEC2BIN', 'DEC2HEX', 'DEC2OCT', 'DEGREES', 'DELTA', 'DEVSQ', 'DOLLAR', 'DOLLARDE', 'DOLLARFR',
36- 'E', 'EDATE', 'EFFECT', 'EOMONTH', 'ERF', 'ERFC', 'EVEN', 'EXACT', 'EXPONDIST',
37- 'FALSE', 'FDIST', 'FINV', 'FISHER', 'FISHERINV',
38- 'IF', 'INT', 'ISEVEN', 'ISODD',
39- 'LN', 'LOG', 'LOG10',
40- 'MAX', 'MAXA', 'MEDIAN', 'MIN', 'MINA', 'MOD',
41- 'NOT',
42- 'ODD', 'OR',
43- 'PI', 'POWER',
44- 'ROUND', 'ROUNDDOWN', 'ROUNDUP',
45- 'SIN', 'SINH', 'SPLIT', 'SQRT', 'SQRTPI', 'SUM', 'SUMIF', 'SUMIFS', 'SUMPRODUCT', 'SUMSQ', 'SUMX2MY2', 'SUMX2PY2', 'SUMXMY2',
46- 'TAN', 'TANH', 'TRUE', 'TRUNC',
47- 'XOR'
48-];
49+import * as AllFormulas from "./Formulas/AllFormulas";
50
51 var Formulas = {
52 exists: function(fn: string) {
53- return (SUPPORTED_FORMULAS.indexOf(fn) > -1 || (fn in RawFormulas) || (fn in RawFormulas.__COMPLEX));
54+ return ((fn in AllFormulas) || (fn in AllFormulas.__COMPLEX));
55 },
56 get: function(fn: string) {
57- if (fn in RawFormulas) {
58- return RawFormulas[fn];
59+ if (fn in AllFormulas) {
60+ return AllFormulas[fn];
61 }
62- if (fn in RawFormulas.__COMPLEX) {
63- return RawFormulas.__COMPLEX[fn];
64+ if (fn in AllFormulas.__COMPLEX) {
65+ return AllFormulas.__COMPLEX[fn];
66 }
67- return Formula[fn];
68 }
69 };
70
71diff --git a/src/RawFormulas/RawFormulas.ts b/src/Formulas/AllFormulas.ts
72similarity index 100%
73rename from src/RawFormulas/RawFormulas.ts
74rename to src/Formulas/AllFormulas.ts
75diff --git a/src/RawFormulas/Date.ts b/src/Formulas/Date.ts
76similarity index 100%
77rename from src/RawFormulas/Date.ts
78rename to src/Formulas/Date.ts
79diff --git a/src/RawFormulas/Engineering.ts b/src/Formulas/Engineering.ts
80similarity index 100%
81rename from src/RawFormulas/Engineering.ts
82rename to src/Formulas/Engineering.ts
83diff --git a/src/RawFormulas/Financial.ts b/src/Formulas/Financial.ts
84similarity index 100%
85rename from src/RawFormulas/Financial.ts
86rename to src/Formulas/Financial.ts
87diff --git a/src/RawFormulas/Logical.ts b/src/Formulas/Logical.ts
88similarity index 100%
89rename from src/RawFormulas/Logical.ts
90rename to src/Formulas/Logical.ts
91diff --git a/src/RawFormulas/Math.ts b/src/Formulas/Math.ts
92similarity index 100%
93rename from src/RawFormulas/Math.ts
94rename to src/Formulas/Math.ts
95diff --git a/src/RawFormulas/Statistical.ts b/src/Formulas/Statistical.ts
96similarity index 100%
97rename from src/RawFormulas/Statistical.ts
98rename to src/Formulas/Statistical.ts
99diff --git a/src/RawFormulas/Text.ts b/src/Formulas/Text.ts
100similarity index 100%
101rename from src/RawFormulas/Text.ts
102rename to src/Formulas/Text.ts
103diff --git a/src/RawFormulas/Utils.ts b/src/Formulas/Utils.ts
104similarity index 100%
105rename from src/RawFormulas/Utils.ts
106rename to src/Formulas/Utils.ts
107diff --git a/src/Main.ts b/src/Main.ts
108index ea2af3c..68f7753 100644
109--- a/src/Main.ts
110+++ b/src/Main.ts
111@@ -1,4 +1,4 @@
112-import { Sheet } from "./Sheet"
113+import { Sheet } from "./Sheet";
114
115 var input = [
116 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "=SUM(A1:D1, H1)"],
117diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
118index 2c2cd00..139858b 100644
119--- a/tests/DateFormulasTest.ts
120+++ b/tests/DateFormulasTest.ts
121@@ -22,7 +22,7 @@ import {
122 TIME,
123 WORKDAY,
124 WORKDAY$INTL
125-} from "../src/RawFormulas/RawFormulas"
126+} from "../src/Formulas/Date"
127 import * as ERRORS from "../src/Errors"
128 import {
129 assertEquals,
130diff --git a/tests/DateFormulasTestTimeOverride.ts b/tests/DateFormulasTestTimeOverride.ts
131index 7d12090..0ff7950 100644
132--- a/tests/DateFormulasTestTimeOverride.ts
133+++ b/tests/DateFormulasTestTimeOverride.ts
134@@ -1,9 +1,8 @@
135-
136 import {
137 NOW,
138 DATEVALUE,
139 TODAY
140-} from "../src/RawFormulas/RawFormulas"
141+} from "../src/Formulas/Date"
142 import * as ERRORS from "../src/Errors"
143 import {
144 assertEquals,
145diff --git a/tests/EngineeringTest.ts b/tests/EngineeringTest.ts
146index 54c9a54..e8c603c 100644
147--- a/tests/EngineeringTest.ts
148+++ b/tests/EngineeringTest.ts
149@@ -6,7 +6,7 @@ import {
150 DEC2HEX,
151 DEC2OCT,
152 DELTA
153-} from "../src/RawFormulas/Engineering";
154+} from "../src/Formulas/Engineering";
155 import * as ERRORS from "../src/Errors"
156 import {
157 assertEquals,
158diff --git a/tests/FinancialTest.ts b/tests/FinancialTest.ts
159index 924109e..b245f6a 100644
160--- a/tests/FinancialTest.ts
161+++ b/tests/FinancialTest.ts
162@@ -8,13 +8,13 @@ import {
163 DOLLARDE,
164 DOLLARFR,
165 EFFECT
166-} from "../src/RawFormulas/Financial";
167+} from "../src/Formulas/Financial";
168 import {
169 DATE
170-} from "../src/RawFormulas/Date";
171+} from "../src/Formulas/Date";
172 import {
173 PI
174-} from "../src/RawFormulas/Math";
175+} from "../src/Formulas/Math";
176 import * as ERRORS from "../src/Errors"
177 import {
178 assertEquals,
179diff --git a/tests/LogicalTest.ts b/tests/LogicalTest.ts
180index 05ae802..04e8355 100644
181--- a/tests/LogicalTest.ts
182+++ b/tests/LogicalTest.ts
183@@ -6,7 +6,7 @@ import {
184 NOT,
185 OR,
186 XOR
187-} from "../src/RawFormulas/Logical";
188+} from "../src/Formulas/Logical";
189 import * as ERRORS from "../src/Errors"
190 import {
191 assertEquals,
192diff --git a/tests/MathTest.ts b/tests/MathTest.ts
193index 7d97770..f9e4824 100644
194--- a/tests/MathTest.ts
195+++ b/tests/MathTest.ts
196@@ -50,7 +50,7 @@ import {
197 RADIANS,
198 DEGREES,
199 COMBIN
200-} from "../src/RawFormulas/Math";
201+} from "../src/Formulas/Math";
202 import * as ERRORS from "../src/Errors"
203 import {
204 assertEquals,
205diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
206index ce95311..eb8e20b 100644
207--- a/tests/SheetFormulaTest.ts
208+++ b/tests/SheetFormulaTest.ts
209@@ -1,5 +1,9 @@
210-import { Sheet } from "../src/Sheet"
211-import {assertEquals} from "./utils/Asserts"
212+import {
213+ Sheet
214+} from "../src/Sheet";
215+import {
216+ assertEquals
217+} from "./utils/Asserts";
218
219 function testFormula(formula: string, expectation: any) {
220 var sheet = new Sheet();
221@@ -9,14 +13,6 @@ function testFormula(formula: string, expectation: any) {
222 assertEquals(expectation, cell.getValue());
223 }
224
225-function testFormulaToDate(formula: string, expectation: any) {
226- var sheet = new Sheet();
227- sheet.setCell("A1", formula);
228- var cell = sheet.getCell("A1");
229- assertEquals(null, cell.getError());
230- assertEquals(expectation, cell.getValue().getTime());
231-}
232-
233 function testFormulaToArray(formula: string, expectation: any) {
234 var sheet = new Sheet();
235 sheet.setCell("A1", formula);
236@@ -32,11 +28,6 @@ function testFormulaToArray(formula: string, expectation: any) {
237 testFormula("=ABS(-10)", 10);
238 testFormula("=ABS(0)", 0);
239
240-// Test ACCRINT
241-// TODO: The second one is really close, but should be correct. Fix this.
242-// testFormula("=ACCRINT(DATE(2011, 1, 1), DATE(2011, 2, 1), DATE(2014, 7, 1), 0.1, 1000, 1, 0)", 350);
243-// testFormula('=ACCRINT(DATE(2000, 1, 1), DATE(2000, 2, 1), DATE(2002, 12, 31), 0.05, 100, 4)', 14.98611111);
244-
245 // Test ACOS
246 testFormula("=ACOS(0)", 1.5707963267948966);
247
248@@ -83,19 +74,12 @@ testFormula("=AVERAGEIF([1, 5, 10], '>2')", 7.5);
249 // Test BIN2DEC
250 testFormula("=BIN2DEC('1010101010')", -342);
251
252-// Test BINOMINV
253-// TODO: This should work.
254-// testFormula('=BINOMINV(6, 0.5, 0.75)', 4);
255-
256 // Test BIN2HEX
257 testFormula("=BIN2HEX(1010101010)", "FFFFFFFEAA");
258
259 // Test BIN2OCT
260 testFormula("=BIN2OCT(1010101010)", "7777777252");
261
262-// Test BINOMDIST
263-// TODO: This. FormulaJS implementation differs from GS.
264-
265 // Test CEIL
266 testFormula("=CEILING(22.22, 0.1)", 22.3);
267
268@@ -111,10 +95,6 @@ testFormula("=COMBIN(4, 2)", 6);
269 // Test CONCATENATE
270 testFormula('=CONCATENATE("hey", " ", "there")', "hey there");
271
272-// Test CONFIDENCE
273-// TODO: This should work.
274-// testFormula('=CONFIDENCE(0.05, 1.6, 250)', 0.1983344105);
275-
276 // Test CONVERT
277 testFormula('=CONVERT(5.1, "mm", "m")', 0.0050999999999999995);
278
279@@ -139,9 +119,6 @@ testFormula('=COUNT([1, 5, 10])', 3);
280 // Test COUNTA
281 testFormula("=COUNTA(10, 10, 22)", 3);
282
283-// Test COUNTBLANK
284-// TODO: Fix COUNTBLANK. Does not work properly.
285-
286 // Test COUNTIF
287 testFormula('=COUNTIF([1, 5, 10], ">4")', 2);
288
289@@ -151,39 +128,12 @@ testFormula('=COUNTIFS([1, 5, 10], ">4", [1, 5, 10], ">4")', 2);
290 // Test COUNTUNIQUE
291 testFormula('=COUNTUNIQUE([1, 1, 10])', 2);
292
293-// Test COVARIANCEP
294-testFormula('=COVARIANCEP([3,2,4,5,6], [9,7,12,15,17])', 5.2);
295-
296-// Test COVARIANCES
297-testFormula('=COVARIANCES([2,4,8], [5,11,12])', 9.666666666666668);
298-
299 // Test CUMIPMT
300 testFormula("=CUMIPMT(0.12, 12, 100, 1, 5, 0)", -54.39423242396348);
301
302 // Test CUMPRINC
303 testFormula("=CUMPRINC(0.12, 12, 100, 1, 5, 0)", -26.324171373034403);
304
305-// Test DATE
306-// testFormulaToDate("=DATE(1992, 6, 24)", new Date("6/24/1992").getTime());
307-// testFormulaToDate("=DATE(1992, 13, 24)", new Date("1/24/1993").getTime());
308-// testFormulaToDate("=DATE(1992, 6, 44)", new Date("7/14/1992").getTime());
309-// testFormulaToDate("=DATE(2, 6, 44)", new Date("7/14/1902").getTime());
310-// testFormulaToDate("=DATE(2, 33, 44)", new Date("10/14/1904").getTime());
311-// testFormulaToDate("=DATE(1976, 2, 29)", new Date("2/29/1976").getTime());
312-// testFormulaToDate("=DATE(1976, 2, 30)", new Date("3/1/1976").getTime());
313-//
314-// // Test DATEVALUE
315-// testFormulaToDate('=DATEVALUE("1992-6-24")', new Date("6/24/1992").getTime());
316-//
317-// // Test DAY
318-// testFormula('=DAY(DATEVALUE("1992-6-24"))', 24);
319-//
320-// // Test DAYS
321-// testFormula('=DAYS(DATEVALUE("1993-6-24"), DATEVALUE("1992-6-24"))', 365);
322-//
323-// // Test DAYS360
324-// testFormula('=DAYS360(DATE(1969, 7, 16), DATE(1970, 7, 24), 1)', 368);
325-
326 // Test DB
327 testFormula("=DB(100, 50, 10, 2, 12)", 6.2482428240683285);
328
329diff --git a/tests/StatisticalTest.ts b/tests/StatisticalTest.ts
330index 6da9cd4..ef72b87 100644
331--- a/tests/StatisticalTest.ts
332+++ b/tests/StatisticalTest.ts
333@@ -18,7 +18,7 @@ import {
334 MAXA,
335 MIN,
336 MINA
337-} from "../src/RawFormulas/Statistical";
338+} from "../src/Formulas/Statistical";
339 import * as ERRORS from "../src/Errors"
340 import {
341 assertEquals,
342diff --git a/tests/TextTest.ts b/tests/TextTest.ts
343index 4e2c469..e7c8997 100644
344--- a/tests/TextTest.ts
345+++ b/tests/TextTest.ts
346@@ -1,13 +1,11 @@
347-import {
348- CONVERT
349-} from "../src/RawFormulas/RawFormulas";
350 import {
351 ARABIC,
352 CHAR,
353 CODE,
354 CONCATENATE,
355- SPLIT
356-} from "../src/RawFormulas/Text";
357+ SPLIT,
358+ CONVERT
359+} from "../src/Formulas/Text";
360 import * as ERRORS from "../src/Errors";
361 import {
362 assertEquals,