commit
message
Formulas.AVERAGE written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-21 23:27:53
stats
2 file(s) changed,
60 insertions(+),
1 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
2index d653e03..67e0e74 100644
3--- a/src/RawFormulas.ts
4+++ b/src/RawFormulas.ts
5@@ -27,6 +27,21 @@ function checkArgumentsAtLeastLength(args: any, length: number) {
6 }
7 }
8
9+/**
10+ * Filter out all strings from an array.
11+ * @param arr to filter
12+ * @returns {Array} filtered array
13+ */
14+function filterOutStringValues(arr: Array<any>) : Array<any> {
15+ var toReturn = [];
16+ for (var i = 0; i < arr.length; i++) {
17+ if (typeof arr[i] !== "string") {
18+ toReturn.push(arr[i]);
19+ }
20+ }
21+ return toReturn;
22+}
23+
24
25 /**
26 * Converts any value to a number or throws an error if it cannot coerce it to the number type
27@@ -314,7 +329,33 @@ var ATANH = function (value?) : number {
28
29
30 var AVEDEV = Formula["AVEDEV"];
31-var AVERAGE = Formula["AVERAGE"];
32+
33+/**
34+ * Returns the numerical average value in a dataset, ignoring text.
35+ * @param values The values or ranges to consider when calculating the average value.
36+ * @returns {number} the average value of this dataset.
37+ * @constructor
38+ */
39+var AVERAGE = function (...values) : number {
40+ checkArgumentsAtLeastLength(values, 1);
41+ var result = 0;
42+ var count = 0;
43+ for (var i = 0; i < values.length; i++) {
44+ if (values[i] instanceof Array) {
45+ if (values[i].length === 0) {
46+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
47+ }
48+ var filtered = filterOutStringValues(values[i]);
49+ result = result + SUM.apply(this, filtered);
50+ count += filtered.length;
51+ } else {
52+ result = result + valueToNumber(values[i]);
53+ count++;
54+ }
55+ }
56+ return result / count;
57+
58+};
59 var AVERAGEA = Formula["AVERAGEA"];
60 var AVERAGEIF = Formula["AVERAGEIF"];
61 var BASE = Formula["BASE"];
62diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
63index ac53cb2..a2ff686 100644
64--- a/tests/FormulasTest.ts
65+++ b/tests/FormulasTest.ts
66@@ -187,7 +187,24 @@ catchAndAssertEquals(function() {
67
68 assertEquals(AVEDEV(1, 2, 4, 56.7), 20.3875);
69
70-assertEquals(AVERAGE(10, 20, 4.1), 11.366666666666667);
71+assertEquals(AVERAGE(1, 2, 4, 55), 15.5);
72+assertEquals(AVERAGE(1, 2, 4, "55"), 15.5);
73+assertEquals(AVERAGE(1, 2, 4, 55, false), 12.4);
74+assertEquals(AVERAGE(1, 2, 4, 55, true), 12.6);
75+assertEquals(AVERAGE(1, 2, 4, 55, 0), 12.4);
76+assertEquals(AVERAGE(1, 2, 4, 55, 1), 12.6);
77+catchAndAssertEquals(function() {
78+ return AVERAGE(1, 2, 4, "str");
79+}, ERRORS.VALUE_ERROR);
80+assertEquals(AVERAGE([1, 2, 4, 55, "str"]), 15.5);
81+assertEquals(AVERAGE([1, 2, 4, 55, "22"]), 15.5);
82+assertEquals(AVERAGE([0]), 0);
83+catchAndAssertEquals(function() {
84+ return AVERAGE();
85+}, ERRORS.NA_ERROR);
86+catchAndAssertEquals(function() {
87+ return AVERAGE([]);
88+}, ERRORS.REF_ERROR);
89
90 assertEquals(AVERAGEA(10, 20, 4.1), 11.366666666666667);
91