spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Adding Formula.DELTA
author
Ben Vogt <[email protected]>
date
2017-02-07 04:07:24
stats
3 file(s) changed, 55 insertions(+), 0 deletions(-)
files
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index 51856e9..ca2b8f1 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -56,6 +56,7 @@ import {
  6 } from "./Misc";
  7 import {
  8   checkArgumentsAtLeastLength,
  9+  checkArgumentsAtWithin,
 10   valueCanCoerceToNumber,
 11   filterOutStringValues,
 12   valueToNumber,
 13@@ -164,7 +165,22 @@ var DEC2BIN = Formula["DEC2BIN"];
 14 var DEC2HEX = Formula["DEC2HEX"];
 15 var DEC2OCT = Formula["DEC2OCT"];
 16 var DEGREES = Formula["DEGREES"];
 17-var DELTA = Formula["DELTA"];
 18+
 19+/**
 20+ * Compare two numeric values, returning 1 if they're equal.
 21+ * @param values[0] The first number to compare.
 22+ * @param values[1] The second number to compare.
 23+ * @returns {number} 1 if they're equal, 0 if they're not equal.
 24+ * @constructor
 25+ */
 26+var DELTA = function (...values) : number {
 27+  checkArgumentsAtWithin(values, 1, 2);
 28+  if (values.length === 1) {
 29+    return valueToNumber(values[0]) === 0 ? 1 : 0;
 30+  }
 31+  return valueToNumber(values[0]) === valueToNumber(values[1]) ? 1 : 0;
 32+};
 33+
 34 var DEVSQ = Formula["DEVSQ"];
 35 var DOLLAR = Formula["DOLLAR"];
 36 var DOLLARDE = Formula["DOLLARDE"];
 37diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 38index 9de85de..4ae1be6 100644
 39--- a/src/RawFormulas/Utils.ts
 40+++ b/src/RawFormulas/Utils.ts
 41@@ -23,6 +23,18 @@ function checkArgumentsAtLeastLength(args: any, length: number) {
 42   }
 43 }
 44 
 45+/**
 46+ * Checks to see if the arguments are within a max and min, inclusively
 47+ * @param args to check length of
 48+ * @param low least number of arguments
 49+ * @param high max number of arguments
 50+ */
 51+function checkArgumentsAtWithin(args: any, low: number, high: number) {
 52+  if (args.length > high || args.length < low) {
 53+    throw new CellError(ERRORS.NA_ERROR, "Wrong number of arguments to ___. Expected 1 arguments, but got " + args.length + " arguments.");
 54+  }
 55+}
 56+
 57 /**
 58  * Filter out all strings from an array.
 59  * @param arr to filter
 60@@ -192,5 +204,6 @@ export {
 61   firstValueAsString,
 62   filterOutStringValues,
 63   checkArgumentsAtLeastLength,
 64+  checkArgumentsAtWithin,
 65   checkArgumentsLength
 66 }
 67\ No newline at end of file
 68diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 69index aff0b6d..c496deb 100644
 70--- a/tests/FormulasTest.ts
 71+++ b/tests/FormulasTest.ts
 72@@ -521,7 +521,31 @@ assertEquals(DEC2OCT("100"), "144");
 73 
 74 assertEquals(DEGREES(PI()), 180);
 75 
 76+
 77+// Test DELTA
 78 assertEquals(DELTA(2, 2), 1);
 79+assertEquals(DELTA(2, 1), 0);
 80+assertEquals(DELTA(2), 0);
 81+assertEquals(DELTA("", ""), 1);
 82+assertEquals(DELTA(false), 1);
 83+assertEquals(DELTA(true), 0);
 84+assertEquals(DELTA(2.2, 2.1), 0);
 85+assertEquals(DELTA(1, true), 1);
 86+assertEquals(DELTA(0, false), 1);
 87+assertEquals(DELTA(true, true), 1);
 88+catchAndAssertEquals(function() {
 89+  DELTA("str");
 90+}, ERRORS.VALUE_ERROR);
 91+catchAndAssertEquals(function() {
 92+  DELTA("n", "n");
 93+}, ERRORS.VALUE_ERROR);
 94+catchAndAssertEquals(function() {
 95+  DELTA();
 96+}, ERRORS.NA_ERROR);
 97+catchAndAssertEquals(function() {
 98+  DELTA(1, 2, 3);
 99+}, ERRORS.NA_ERROR);
100+
101 
102 assertEquals(DEVSQ(1, 2), 0.5);
103