spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Refactoring recently added formulas into correct files
author
Ben Vogt <[email protected]>
date
2017-01-22 19:38:22
stats
3 file(s) changed, 108 insertions(+), 95 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
  1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
  2index 3dfeea7..f45d041 100644
  3--- a/src/RawFormulas/Math.ts
  4+++ b/src/RawFormulas/Math.ts
  5@@ -1,4 +1,5 @@
  6-import { checkArgumentsLength, checkArgumentsAtLeastLength, valueToNumber, filterOutStringValues} from "./Utils"
  7+import { checkArgumentsLength, checkArgumentsAtLeastLength, valueToNumber, filterOutStringValues, flatten,
  8+    stringValuesToZeros} from "./Utils"
  9 import { CellError } from "../Errors"
 10 import * as ERRORS from "../Errors"
 11 
 12@@ -202,6 +203,76 @@ var AVERAGE = function (...values) : number {
 13   return result / count;
 14 };
 15 
 16+/**
 17+ * Calculates the average of the magnitudes of deviations of data from a dataset's mean.
 18+ * @param values The value(s) or range(s)
 19+ * @returns {number} average of the magnitudes of deviations of data from a dataset's mean
 20+ * @constructor
 21+ */
 22+var AVEDEV = function (...values) {
 23+  checkArgumentsAtLeastLength(values, 1);
 24+
 25+  // Sort to array-values, and non-array-values
 26+  var arrayValues = [];
 27+  var nonArrayValues = [];
 28+  for (var i = 0; i < values.length; i++) {
 29+    var X = values[i];
 30+    if (X instanceof Array) {
 31+      if (X.length === 0) {
 32+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 33+      }
 34+      arrayValues.push(X);
 35+    } else {
 36+      nonArrayValues.push(valueToNumber(X));
 37+    }
 38+  }
 39+
 40+  // Remove string values from array-values, but not from non-array-values, and concat.
 41+  var flatValues = filterOutStringValues(flatten(arrayValues)).map(function (value) {
 42+    return valueToNumber(value);
 43+  }).concat(nonArrayValues);
 44+
 45+  // Calculating mean
 46+  var result = 0;
 47+  var count = 0;
 48+  for (var i = 0; i < flatValues.length; i++) {
 49+    result = result + valueToNumber(flatValues[i]);
 50+    count++;
 51+  }
 52+  var mean = result / count;
 53+
 54+  for (var i = 0; i < flatValues.length; i++) {
 55+    flatValues[i] = ABS(valueToNumber(flatValues[i]) - mean);
 56+  }
 57+  return SUM(flatValues) / flatValues.length;
 58+};
 59+
 60+/**
 61+ * Returns the numerical average value in a dataset, coercing text values in ranges to 0 values.
 62+ * @param values value(s) or range(s) to consider when calculating the average value.
 63+ * @returns {number} the numerical average value in a dataset
 64+ * @constructor
 65+ */
 66+var AVERAGEA = function (...values) {
 67+  checkArgumentsAtLeastLength(values, 1);
 68+  var result = 0;
 69+  var count = 0;
 70+  for (var i = 0; i < values.length; i++) {
 71+    if (values[i] instanceof Array) {
 72+      if (values[i].length === 0) {
 73+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 74+      }
 75+      var filtered = stringValuesToZeros(values[i]);
 76+      result = result + SUM.apply(this, filtered);
 77+      count += filtered.length;
 78+    } else {
 79+      result = result + valueToNumber(values[i]);
 80+      count++;
 81+    }
 82+  }
 83+  return result / count;
 84+};
 85+
 86 /**
 87  * Rounds a number up to the nearest even integer.
 88  * @param values[0] The value to round to the next greatest even number.
 89@@ -402,6 +473,8 @@ export {
 90   ATAN2,
 91   ATANH,
 92   AVERAGE,
 93+  AVERAGEA,
 94+  AVEDEV,
 95   EVEN,
 96   MAX,
 97   MAXA,
 98diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 99index 74cadf6..3a1eea1 100644
100--- a/src/RawFormulas/RawFormulas.ts
101+++ b/src/RawFormulas/RawFormulas.ts
102@@ -13,6 +13,8 @@ import {
103   ATAN2,
104   ATANH,
105   AVERAGE,
106+  AVERAGEA,
107+  AVEDEV,
108   EVEN,
109   MAX,
110   MAXA,
111@@ -33,100 +35,6 @@ import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber} from
112 import { CellError } from "../Errors"
113 import * as ERRORS from "../Errors"
114 
115-
116-function flatten(values: Array<any>) : Array<any> {
117-  return values.reduce(function (flat, toFlatten) {
118-    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
119-  }, []);
120-}
121-
122-/**
123- * Converts string values in array to 0
124- * @param arr to convert
125- * @returns {Array} array in which all string values have been converted to 0.
126- */
127-function stringValuesToZeros(arr: Array<any>) : Array<any> {
128-  var toReturn = [];
129-  for (var i = 0; i < arr.length; i++) {
130-    if (typeof arr[i] !== "string") {
131-      toReturn.push(arr[i]);
132-    } else {
133-      toReturn.push(0);
134-    }
135-  }
136-  return toReturn;
137-}
138-
139-/**
140- * Calculates the average of the magnitudes of deviations of data from a dataset's mean.
141- * @param values The value(s) or range(s)
142- * @returns {number} average of the magnitudes of deviations of data from a dataset's mean
143- * @constructor
144- */
145-var AVEDEV = function (...values) {
146-  checkArgumentsAtLeastLength(values, 1);
147-
148-  // Sort to array-values, and non-array-values
149-  var arrayValues = [];
150-  var nonArrayValues = [];
151-  for (var i = 0; i < values.length; i++) {
152-    var X = values[i];
153-    if (X instanceof Array) {
154-      if (X.length === 0) {
155-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
156-      }
157-      arrayValues.push(X);
158-    } else {
159-      nonArrayValues.push(valueToNumber(X));
160-    }
161-  }
162-
163-  // Remove string values from array-values, but not from non-array-values, and concat.
164-  var flatValues = filterOutStringValues(flatten(arrayValues)).map(function (value) {
165-    return valueToNumber(value);
166-  }).concat(nonArrayValues);
167-
168-  // Calculating mean
169-  var result = 0;
170-  var count = 0;
171-  for (var i = 0; i < flatValues.length; i++) {
172-    result = result + valueToNumber(flatValues[i]);
173-    count++;
174-  }
175-  var mean = result / count;
176-
177-  for (var i = 0; i < flatValues.length; i++) {
178-    flatValues[i] = ABS(valueToNumber(flatValues[i]) - mean);
179-  }
180-  return SUM(flatValues) / flatValues.length;
181-};
182-
183-/**
184- * Returns the numerical average value in a dataset, coercing text values in ranges to 0 values.
185- * @param values value(s) or range(s) to consider when calculating the average value.
186- * @returns {number} the numerical average value in a dataset
187- * @constructor
188- */
189-var AVERAGEA = function (...values) {
190-  checkArgumentsAtLeastLength(values, 1);
191-  var result = 0;
192-  var count = 0;
193-  for (var i = 0; i < values.length; i++) {
194-    if (values[i] instanceof Array) {
195-      if (values[i].length === 0) {
196-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
197-      }
198-      var filtered = stringValuesToZeros(values[i]);
199-      result = result + SUM.apply(this, filtered);
200-      count += filtered.length;
201-    } else {
202-      result = result + valueToNumber(values[i]);
203-      count++;
204-    }
205-  }
206-  return result / count;
207-};
208-
209 var ACCRINT = Formula["ACCRINT"];
210 var AVERAGEIF = Formula["AVERAGEIF"];
211 var BASE = Formula["BASE"];
212diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
213index b21e770..16351b4 100644
214--- a/src/RawFormulas/Utils.ts
215+++ b/src/RawFormulas/Utils.ts
216@@ -84,7 +84,39 @@ function valueToNumber(value: any) : number {
217 }
218 
219 
220+/**
221+ * Converts string values in array to 0
222+ * @param arr to convert
223+ * @returns {Array} array in which all string values have been converted to 0.
224+ */
225+function stringValuesToZeros(arr: Array<any>) : Array<any> {
226+  var toReturn = [];
227+  for (var i = 0; i < arr.length; i++) {
228+    if (typeof arr[i] !== "string") {
229+      toReturn.push(arr[i]);
230+    } else {
231+      toReturn.push(0);
232+    }
233+  }
234+  return toReturn;
235+}
236+
237+
238+/**
239+ * Flatten an array of arrays of ...
240+ * @param values array of values
241+ * @returns {Array} flattened array
242+ */
243+function flatten(values: Array<any>) : Array<any> {
244+  return values.reduce(function (flat, toFlatten) {
245+    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
246+  }, []);
247+}
248+
249+
250 export {
251+  stringValuesToZeros,
252+  flatten,
253   valueToNumber,
254   valueToString,
255   filterOutStringValues,