spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.DEVSQ
author
Ben Vogt <[email protected]>
date
2017-02-19 17:16:21
stats
4 file(s) changed, 46 insertions(+), 2 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
  2index 0ed50d9..7bbb51b 100644
  3--- a/src/RawFormulas/Math.ts
  4+++ b/src/RawFormulas/Math.ts
  5@@ -1122,6 +1122,29 @@ var DEGREES = function (...values) {
  6 };
  7 
  8 
  9+/**
 10+ * Calculates the sum of squares of deviations based on a sample.
 11+ * @param values The values or ranges of the sample.
 12+ * @returns {number} sum of squares of deviations
 13+ * @constructor
 14+ */
 15+var DEVSQ = function (...values) : number {
 16+  ArgsChecker.checkAtLeastLength(values, 1);
 17+  var range = Filter.flattenAndThrow(values);
 18+  var result = 0;
 19+  var count = 0;
 20+  for (var i = 0; i < range.length; i++) {
 21+    result = result + TypeCaster.valueToNumber(range[i]);
 22+    count++;
 23+  }
 24+  var mean = result / count;
 25+  var result = 0;
 26+  for (var i = 0; i < range.length; i++) {
 27+    result += Math.pow((TypeCaster.valueToNumber(range[i]) - mean), 2);
 28+  }
 29+  return result;
 30+};
 31+
 32 
 33 export {
 34   ABS,
 35@@ -1141,6 +1164,7 @@ export {
 36   COTH,
 37   COSH,
 38   COS,
 39+  DEVSQ,
 40   EVEN,
 41   INT,
 42   ISEVEN,
 43diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 44index 865dfaa..c45bcca 100644
 45--- a/src/RawFormulas/RawFormulas.ts
 46+++ b/src/RawFormulas/RawFormulas.ts
 47@@ -19,6 +19,7 @@ import {
 48   COTH,
 49   COSH,
 50   COS,
 51+  DEVSQ,
 52   EVEN,
 53   INT,
 54   ISEVEN,
 55@@ -107,7 +108,6 @@ var DATEVALUE = function (dateString: string) : Date {
 56 var DAY = Formula["DAY"];
 57 var DAYS = Formula["DAYS"];
 58 var DAYS360 = Formula["DAYS360"];
 59-var DEVSQ = Formula["DEVSQ"];
 60 var DOLLAR = Formula["DOLLAR"];
 61 var DOLLARDE = Formula["DOLLARDE"];
 62 var DOLLARFR = Formula["DOLLARFR"];
 63diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 64index 003b260..92bde34 100644
 65--- a/src/RawFormulas/Utils.ts
 66+++ b/src/RawFormulas/Utils.ts
 67@@ -279,7 +279,7 @@ class Filter {
 68       if (Array.isArray(toFlatten) && toFlatten.length === 0) {
 69         throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 70       }
 71-      return flat.concat(Array.isArray(toFlatten) ? Filter.flatten(toFlatten) : toFlatten);
 72+      return flat.concat(Array.isArray(toFlatten) ? Filter.flattenAndThrow(toFlatten) : toFlatten);
 73     }, []);
 74   }
 75 
 76diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 77index 76faf86..e43a062 100644
 78--- a/tests/FormulasTest.ts
 79+++ b/tests/FormulasTest.ts
 80@@ -850,7 +850,25 @@ catchAndAssertEquals(function() {
 81 }, ERRORS.NA_ERROR);
 82 
 83 
 84+// Test DEVSQ
 85 assertEquals(DEVSQ(1, 2), 0.5);
 86+assertEquals(DEVSQ([1, 2]), 0.5);
 87+assertEquals(DEVSQ([1, [2]]), 0.5);
 88+assertEquals(DEVSQ(1), 0);
 89+assertEquals(DEVSQ(false), 0);
 90+assertEquals(DEVSQ(true), 0);
 91+assertEquals(DEVSQ(1, 2, 3, 4), 5);
 92+assertEquals(DEVSQ([1, 2, 3, 4]), 5);
 93+catchAndAssertEquals(function() {
 94+  DEVSQ(1, "str");
 95+}, ERRORS.VALUE_ERROR);
 96+catchAndAssertEquals(function() {
 97+  DEVSQ();
 98+}, ERRORS.NA_ERROR);
 99+catchAndAssertEquals(function() {
100+  DEVSQ([1, 2, [], 3]);
101+}, ERRORS.REF_ERROR);
102+
103 
104 assertEquals(DOLLAR(1.2351, 4), "$1.2351");
105