spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[MAXA] Now differs from MAX
author
Ben Vogt <[email protected]>
date
2017-05-20 00:28:30
stats
4 file(s) changed, 31 insertions(+), 14 deletions(-)
files
TODO.md
src/Formulas/Statistical.ts
src/Utilities/Filter.ts
tests/Formulas/StatisticalTest.ts
 1diff --git a/TODO.md b/TODO.md
 2index c3cbd28..5d908b4 100644
 3--- a/TODO.md
 4+++ b/TODO.md
 5@@ -2,15 +2,6 @@
 6 Things I should do.
 7 
 8 
 9-### SUM and SUMA should be different.
10-
11-
12-### MAX and MAXA should be different.
13-
14-
15-### COUNT and COUNTA should be different.
16-
17-
18 ### Cells should have `formatAs` fields.
19 Instead of having non-primitives, (i.e. Date, DateTime, Time, Dollar), cells should have formats based on the highest-order type that was used during the compilation and execution of a cell's dependency. For example, `DATE` might return a number, but the cell that called `DATE` would be aware of it calling a formula that returns an non-primitive type, and would display the returned number as a Date. If you're using `DATE` in conjunction with `DOLLAR` it would still display the returned value as a Date. The heirarhchy would look like: [Date, DateTime, Time, Dollar, number, boolean, string]. Advantages to this would include not having to cast down when using primitive operators, and flexibility in display. It would also simplify the types themselves, by having types be constants and just having helpers to convert, display, and do normal operations with them.
20 
21diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
22index a46657f..e849178 100644
23--- a/src/Formulas/Statistical.ts
24+++ b/src/Formulas/Statistical.ts
25@@ -374,7 +374,22 @@ var MAX = function (...values) {
26  */
27 var MAXA = function (...values) : number {
28   ArgsChecker.checkAtLeastLength(values, 1, "MAXA");
29-  return MAX.apply(this, values);
30+  var maxSoFar = -Infinity;
31+  var filteredValues = Filter.stringValuesToZeros(values);
32+  for (var i = 0; i < filteredValues.length; i++) {
33+    if (filteredValues[i] instanceof Array) {
34+      if (values[i].length === 0) {
35+        throw new RefError("Reference does not exist.");
36+      }
37+      var filtered = Filter.stringValuesToZeros(filteredValues[i]);
38+      if (filtered.length !== 0) {
39+        maxSoFar = Math.max(MAXA.apply(this, filtered), maxSoFar);
40+      }
41+    } else {
42+      maxSoFar = Math.max(TypeConverter.valueToNumber(filteredValues[i]), maxSoFar);
43+    }
44+  }
45+  return maxSoFar;
46 };
47 
48 
49diff --git a/src/Utilities/Filter.ts b/src/Utilities/Filter.ts
50index f7d070b..f26c83a 100644
51--- a/src/Utilities/Filter.ts
52+++ b/src/Utilities/Filter.ts
53@@ -14,10 +14,10 @@ class Filter {
54   static stringValuesToZeros(arr: Array<any>) : Array<any> {
55     var toReturn = [];
56     for (var i = 0; i < arr.length; i++) {
57-      if (typeof arr[i] !== "string") {
58-        toReturn.push(arr[i]);
59-      } else {
60+      if (typeof arr[i] === "string") {
61         toReturn.push(0);
62+      } else {
63+        toReturn.push(arr[i]);
64       }
65     }
66     return toReturn;
67diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
68index 77df6ef..44db1f8 100644
69--- a/tests/Formulas/StatisticalTest.ts
70+++ b/tests/Formulas/StatisticalTest.ts
71@@ -329,9 +329,19 @@ test("MAX", function(){
72 });
73 
74 
75-// TODO: More tests here.
76 test("MAXA", function(){
77   assertEquals(MAXA(100, 22, 44), 100);
78+  assertEquals(MAXA("100", 22, 44), 44);
79+  assertEquals(MAXA(-1, -10, "stuff"), 0);
80+  catchAndAssertEquals(function() {
81+    MAX(100, []);
82+  }, ERRORS.REF_ERROR);
83+  catchAndAssertEquals(function() {
84+    MAX([]);
85+  }, ERRORS.REF_ERROR);
86+  catchAndAssertEquals(function() {
87+    MAX.apply(this, []);
88+  }, ERRORS.NA_ERROR);
89 });
90 
91