spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.MEDIAN written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 00:51:41
stats
2 file(s) changed, 132 insertions(+), 41 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
  2index bd7f7dc..86bf4ee 100644
  3--- a/src/RawFormulas.ts
  4+++ b/src/RawFormulas.ts
  5@@ -476,7 +476,49 @@ var MAX = function (...values) {
  6 };
  7 
  8 var MAXA = Formula["MAXA"];
  9-var MEDIAN = Formula["MEDIAN"];
 10+
 11+
 12+/**
 13+ * Returns the median value in a numeric dataset.
 14+ * @param values The value(s) or range(s) to consider when calculating the median value.
 15+ * @returns {number} the median value of the dataset
 16+ * @constructor
 17+ */
 18+var MEDIAN = function (...values) : number {
 19+  checkArgumentsAtLeastLength(values, 1);
 20+  if (values.length === 1) {
 21+    return valueToNumber(values[0]);
 22+  }
 23+  var sortedArray = [];
 24+  values.forEach(function (currentValue) {
 25+    if (currentValue instanceof Array) {
 26+      if (currentValue.length === 0) {
 27+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 28+      }
 29+      var filtered = filterOutStringValues(currentValue);
 30+      sortedArray = sortedArray.concat(filtered);
 31+    } else {
 32+      sortedArray.push(currentValue);
 33+    }
 34+  });
 35+  sortedArray = sortedArray.sort(function (a, b) {
 36+    var aN = valueToNumber(a);
 37+    var bN = valueToNumber(b);
 38+    return aN - bN;
 39+  });
 40+  // even number of values
 41+  if (sortedArray.length % 2 === 0) {
 42+    if (sortedArray.length === 2) {
 43+      return AVERAGE(sortedArray[0], sortedArray[1]);
 44+    }
 45+    var top = sortedArray[sortedArray.length / 2];
 46+    var bottom = sortedArray[(sortedArray.length / 2) - 1];
 47+    return AVERAGE(top, bottom);
 48+  } else {
 49+    // odd number of values
 50+    return sortedArray[Math.round(sortedArray.length / 2) - 1];
 51+  }
 52+};
 53 
 54 
 55 /**
 56diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 57index c550ec2..bfb46cb 100644
 58--- a/tests/FormulasTest.ts
 59+++ b/tests/FormulasTest.ts
 60@@ -29,10 +29,10 @@ assertEquals(ABS(0), 0);
 61 assertEquals(ABS(false), 0);
 62 assertEquals(ABS("-44"), 44);
 63 catchAndAssertEquals(function() {
 64-  return ABS();
 65+  ABS();
 66 }, ERRORS.NA_ERROR);
 67 catchAndAssertEquals(function() {
 68-  return ABS("str");
 69+  ABS("str");
 70 }, ERRORS.VALUE_ERROR);
 71 
 72 
 73@@ -48,10 +48,10 @@ assertEquals(ACOS(1), 0);
 74 assertEquals(ACOS("-1"), 3.141592653589793);
 75 assertEquals(ACOS(false), 1.5707963267948966);
 76 catchAndAssertEquals(function() {
 77-  return ACOS("str");
 78+  ACOS("str");
 79 }, ERRORS.VALUE_ERROR);
 80 catchAndAssertEquals(function() {
 81-  return ACOS(2);
 82+  ACOS(2);
 83 }, ERRORS.NUM_ERROR);
 84 
 85 
 86@@ -60,13 +60,13 @@ assertEquals(ACOSH(22), 3.783672704329451);
 87 assertEquals(ACOSH(1), 0);
 88 assertEquals(ACOSH("11"), 3.0889699048446033);
 89 catchAndAssertEquals(function() {
 90-  return ACOSH(-1);
 91+  ACOSH(-1);
 92 }, ERRORS.NUM_ERROR);
 93 catchAndAssertEquals(function() {
 94-  return ACOSH("str");
 95+  ACOSH("str");
 96 }, ERRORS.VALUE_ERROR);
 97 catchAndAssertEquals(function() {
 98-  return ACOSH(false);
 99+  ACOSH(false);
100 }, ERRORS.NUM_ERROR);
101 
102 
103@@ -75,13 +75,13 @@ assertEquals(ACOTH(22), 0.04548588910286339);
104 assertEquals(ACOTH(-1.1), -1.522261218861711);
105 assertEquals(ACOTH("-22"), -0.04548588910286338);
106 catchAndAssertEquals(function() {
107-  return ACOTH(-1);
108+  ACOTH(-1);
109 }, ERRORS.NUM_ERROR);
110 catchAndAssertEquals(function() {
111-  return ACOTH("str");
112+  ACOTH("str");
113 }, ERRORS.VALUE_ERROR);
114 catchAndAssertEquals(function() {
115-  return ACOTH(false);
116+  ACOTH(false);
117 }, ERRORS.NUM_ERROR);
118 
119 
120@@ -91,18 +91,18 @@ assertEquals(AND(10, 0), false);
121 assertEquals(AND(10, false), false);
122 assertEquals(AND(0, 0), false);
123 catchAndAssertEquals(function() {
124-  return AND(1, "");
125+  AND(1, "");
126 }, ERRORS.VALUE_ERROR);
127 catchAndAssertEquals(function() {
128-  return AND();
129+  AND();
130 }, ERRORS.NA_ERROR);
131 catchAndAssertEquals(function() {
132-  return AND(1, "str");
133+  AND(1, "str");
134 }, ERRORS.VALUE_ERROR);
135 assertEquals(AND(0, [1, 1]), false);
136 assertEquals(AND(1, [1, 1]), true);
137 catchAndAssertEquals(function() {
138-  return AND(1, [1, "str"]);
139+  AND(1, [1, "str"]);
140 }, ERRORS.VALUE_ERROR);
141 
142 
143@@ -111,13 +111,13 @@ assertEquals(ARABIC("XIV"), 14);
144 assertEquals(ARABIC("M"), 1000);
145 assertEquals(ARABIC("-IV"), -4);
146 catchAndAssertEquals(function() {
147-  return ARABIC("b");
148+  ARABIC("b");
149 }, ERRORS.VALUE_ERROR);
150 catchAndAssertEquals(function() {
151-  return ARABIC(false);
152+  ARABIC(false);
153 }, ERRORS.VALUE_ERROR);
154 catchAndAssertEquals(function() {
155-  return ARABIC(10);
156+  ARABIC(10);
157 }, ERRORS.VALUE_ERROR);
158 
159 
160@@ -127,10 +127,10 @@ assertEquals(ASIN(1), 1.5707963267948966);
161 assertEquals(ASIN("1"), 1.5707963267948966);
162 assertEquals(ASIN(false), 0);
163 catchAndAssertEquals(function() {
164-  return ASIN(2);
165+  ASIN(2);
166 }, ERRORS.NUM_ERROR);
167 catchAndAssertEquals(function() {
168-  return ASIN("str");
169+  ASIN("str");
170 }, ERRORS.VALUE_ERROR);
171 
172 
173@@ -141,7 +141,7 @@ assertEquals(ASINH("1"), 0.8813735870195429);
174 assertEquals(ASINH(false), 0);
175 assertEquals(ASINH(true), 0.8813735870195429);
176 catchAndAssertEquals(function() {
177-  return ASINH("str");
178+  ASINH("str");
179 }, ERRORS.VALUE_ERROR);
180 
181 
182@@ -152,14 +152,14 @@ assertEquals(ATAN("1"), 0.7853981633974483);
183 assertEquals(ATAN(false), 0);
184 assertEquals(ATAN(true), 0.7853981633974483);
185 catchAndAssertEquals(function() {
186-  return ASINH("str");
187+  ASINH("str");
188 }, ERRORS.VALUE_ERROR);
189 
190 // Test ATAN2
191 assertEquals(ATAN2(4, 3), 0.6435011087932844);
192 assertEquals(ATAN2(-1, -1), -2.356194490192345);
193 catchAndAssertEquals(function() {
194-  return ATAN2(0, 0);
195+  ATAN2(0, 0);
196 }, ERRORS.DIV_ZERO_ERROR);
197 assertEquals(ATAN2(1, 0), 0);
198 assertEquals(ATAN2(0, 1), 1.5707963267948966);
199@@ -167,7 +167,7 @@ assertEquals(ATAN2(-1, "-1"), -2.356194490192345);
200 assertEquals(ATAN2(true, false), 0);
201 assertEquals(ATAN2(true, true), 0.7853981633974483);
202 catchAndAssertEquals(function() {
203-  return ATAN2("str", false);
204+  ATAN2("str", false);
205 }, ERRORS.VALUE_ERROR);
206 
207 
208@@ -178,10 +178,10 @@ assertEquals(ATANH(0), 0);
209 assertEquals(ATANH("0.1"), 0.10033534773107562);
210 assertEquals(ATANH(false), 0);
211 catchAndAssertEquals(function() {
212-  return ATANH(true);
213+  ATANH(true);
214 }, ERRORS.NUM_ERROR);
215 catchAndAssertEquals(function() {
216-  return ATANH("str");
217+  ATANH("str");
218 }, ERRORS.VALUE_ERROR);
219 
220 
221@@ -196,16 +196,16 @@ assertEquals(AVERAGE(1, 2, 4, 55, true), 12.6);
222 assertEquals(AVERAGE(1, 2, 4, 55, 0), 12.4);
223 assertEquals(AVERAGE(1, 2, 4, 55, 1), 12.6);
224 catchAndAssertEquals(function() {
225-  return AVERAGE(1, 2, 4, "str");
226+  AVERAGE(1, 2, 4, "str");
227 }, ERRORS.VALUE_ERROR);
228 assertEquals(AVERAGE([1, 2, 4, 55, "str"]), 15.5);
229 assertEquals(AVERAGE([1, 2, 4, 55, "22"]), 15.5);
230 assertEquals(AVERAGE([0]), 0);
231 catchAndAssertEquals(function() {
232-  return AVERAGE();
233+  AVERAGE();
234 }, ERRORS.NA_ERROR);
235 catchAndAssertEquals(function() {
236-  return AVERAGE([]);
237+  AVERAGE([]);
238 }, ERRORS.REF_ERROR);
239 
240 assertEquals(AVERAGEA(10, 20, 4.1), 11.366666666666667);
241@@ -389,22 +389,53 @@ assertEquals(MAX(100, [101, 2]), 101);
242 assertEquals(MAX(100, [101, 2, "10000"]), 101);
243 assertEquals(MAX(100, ["10000"]), 100);
244 catchAndAssertEquals(function() {
245-  return MAX(100, []);
246+  MAX(100, []);
247 }, ERRORS.REF_ERROR);
248 catchAndAssertEquals(function() {
249-  return MAX([]);
250+  MAX([]);
251 }, ERRORS.REF_ERROR);
252 catchAndAssertEquals(function() {
253-  return MAX();
254+  MAX();
255 }, ERRORS.NA_ERROR);
256 catchAndAssertEquals(function() {
257-  return MAX(100, "str");
258+  MAX(100, "str");
259 }, ERRORS.VALUE_ERROR);
260 
261 
262 assertEquals(MAXA(100, 22, 44), 100);
263 
264+
265+// Tes MEDIAN
266 assertEquals(MEDIAN(100, 22, 54), 54);
267+assertEquals(MEDIAN(100, 22, "54"), 54);
268+assertEquals(MEDIAN(100, 22), 61);
269+assertEquals(MEDIAN(2), 2);
270+assertEquals(MEDIAN(false), false);
271+assertEquals(MEDIAN(1, 1, 2, 6, 6, 9, 5), 5);
272+assertEquals(MEDIAN(6, 6, 1, 1, 2, 9), 4);
273+assertEquals(MEDIAN(1, 1, 2, [5, 6, 6, 9]), 5);
274+catchAndAssertEquals(function() {
275+  MEDIAN(1, 1, 2, 5, "mmm", 6, 6, 9);
276+}, ERRORS.VALUE_ERROR);
277+assertEquals(MEDIAN(1, 1, 2, [5, "mmm", 6, 6, 9]), 5);
278+assertEquals(MEDIAN(1, 1, 2, ["mm"]), 1);
279+catchAndAssertEquals(function() {
280+  MEDIAN(1, 1, 2, ["mm"]);
281+}, ERRORS.REF_ERROR);
282+assertEquals(MEDIAN(100, 22, 1, 14), 18);
283+assertEquals(MEDIAN(100, 22, 1, 1), 11.5);
284+assertEquals(MEDIAN(100, 22, 1), 22);
285+assertEquals(MEDIAN(100, 22, [54]), 54);
286+assertEquals(MEDIAN(100, 22, ["str"]), 61);
287+catchAndAssertEquals(function() {
288+  MEDIAN(10, 22, "str");
289+}, ERRORS.VALUE_ERROR);
290+catchAndAssertEquals(function() {
291+  MEDIAN();
292+}, ERRORS.NA_ERROR);
293+catchAndAssertEquals(function() {
294+  MEDIAN(["str"]);
295+}, ERRORS.NUM_ERROR);
296 
297 
298 // Test MIN
299@@ -416,16 +447,16 @@ assertEquals(MIN(100, [101, 2]), 2);
300 assertEquals(MIN(100, [101, 2, "-10"]), 2);
301 assertEquals(MIN(100, ["-10"]), 100);
302 catchAndAssertEquals(function() {
303-  return MIN(100, []);
304+  MIN(100, []);
305 }, ERRORS.REF_ERROR);
306 catchAndAssertEquals(function() {
307-  return MIN([]);
308+  MIN([]);
309 }, ERRORS.REF_ERROR);
310 catchAndAssertEquals(function() {
311-  return MIN();
312+  MIN();
313 }, ERRORS.NA_ERROR);
314 catchAndAssertEquals(function() {
315-  return MIN(100, "str");
316+  MIN(100, "str");
317 }, ERRORS.VALUE_ERROR);
318 
319 
320@@ -471,13 +502,13 @@ assertEquals(SUM("10", [5, 5]), 20);
321 assertEquals(SUM(false, [10, 10]), 20);
322 assertEquals(SUM(true, [10, 10]), 21);
323 catchAndAssertEquals(function() {
324-  return SUM([10, 10], "");
325+  SUM([10, 10], "");
326 }, ERRORS.VALUE_ERROR);
327 catchAndAssertEquals(function() {
328-  return SUM([10, 10], "str");
329+  SUM([10, 10], "str");
330 }, ERRORS.VALUE_ERROR);
331 catchAndAssertEquals(function() {
332-  return SUM();
333+  SUM();
334 }, ERRORS.NA_ERROR);
335 
336