commit
message
Formulas.MIN written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-21 23:59:56
stats
2 file(s) changed,
55 insertions(+),
1 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
2index 275d39d..bd7f7dc 100644
3--- a/src/RawFormulas.ts
4+++ b/src/RawFormulas.ts
5@@ -464,7 +464,10 @@ var MAX = function (...values) {
6 if (values[i].length === 0) {
7 throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
8 }
9- maxSoFar = Math.max(MAX.apply(this, values[i]), maxSoFar);
10+ var filtered = filterOutStringValues(values[i]);
11+ if (filtered.length !== 0) {
12+ maxSoFar = Math.max(MAX.apply(this, filtered), maxSoFar);
13+ }
14 } else {
15 maxSoFar = Math.max(valueToNumber(values[i]), maxSoFar);
16 }
17@@ -474,7 +477,34 @@ var MAX = function (...values) {
18
19 var MAXA = Formula["MAXA"];
20 var MEDIAN = Formula["MEDIAN"];
21-var MIN = Formula["MIN"];
22+
23+
24+/**
25+ * Returns the minimum value in a numeric dataset.
26+ * @param values The value(s) or range(s) to consider when calculating the minimum value.
27+ * @returns {number} the minimum value of the dataset
28+ * @constructor
29+ */
30+var MIN = function (...values) {
31+ checkArgumentsAtLeastLength(values, 1);
32+ var minSoFar = Infinity;
33+ for (var i = 0; i < values.length; i++) {
34+ if (values[i] instanceof Array) {
35+ if (values[i].length === 0) {
36+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
37+ }
38+ var filtered = filterOutStringValues(values[i]);
39+ if (filtered.length !== 0) {
40+ minSoFar = Math.min(MIN.apply(this, filtered), minSoFar);
41+ }
42+ } else {
43+ minSoFar = Math.min(valueToNumber(values[i]), minSoFar);
44+ }
45+ }
46+ return minSoFar;
47+};
48+
49+
50 var MINA = Formula["MINA"];
51 var MOD = Formula["MOD"];
52 var TRUE = Formula["TRUE"];
53diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
54index ff87a2d..c550ec2 100644
55--- a/tests/FormulasTest.ts
56+++ b/tests/FormulasTest.ts
57@@ -386,6 +386,8 @@ assertEquals(MAX(100, "22"), 100);
58 assertEquals(MAX(-100, false), 0);
59 assertEquals(MAX(-100, true), 1);
60 assertEquals(MAX(100, [101, 2]), 101);
61+assertEquals(MAX(100, [101, 2, "10000"]), 101);
62+assertEquals(MAX(100, ["10000"]), 100);
63 catchAndAssertEquals(function() {
64 return MAX(100, []);
65 }, ERRORS.REF_ERROR);
66@@ -404,7 +406,28 @@ assertEquals(MAXA(100, 22, 44), 100);
67
68 assertEquals(MEDIAN(100, 22, 54), 54);
69
70+
71+// Test MIN
72 assertEquals(MIN(100, 22, 44), 22);
73+assertEquals(MIN(100, "22"), 22);
74+assertEquals(MIN(100, false), 0);
75+assertEquals(MIN(100, true), 1);
76+assertEquals(MIN(100, [101, 2]), 2);
77+assertEquals(MIN(100, [101, 2, "-10"]), 2);
78+assertEquals(MIN(100, ["-10"]), 100);
79+catchAndAssertEquals(function() {
80+ return MIN(100, []);
81+}, ERRORS.REF_ERROR);
82+catchAndAssertEquals(function() {
83+ return MIN([]);
84+}, ERRORS.REF_ERROR);
85+catchAndAssertEquals(function() {
86+ return MIN();
87+}, ERRORS.NA_ERROR);
88+catchAndAssertEquals(function() {
89+ return MIN(100, "str");
90+}, ERRORS.VALUE_ERROR);
91+
92
93 assertEquals(MINA(100, 22, 44), 22);
94