spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.NOT written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 16:52:52
stats
2 file(s) changed, 77 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
  2index a07d604..54a6265 100644
  3--- a/src/RawFormulas.ts
  4+++ b/src/RawFormulas.ts
  5@@ -70,6 +70,7 @@ function valueToNumber(value: any) : number {
  6   return 0;
  7 }
  8 
  9+
 10 /**
 11  * Returns the absolute value of a number.
 12  * @param value to get the absolute value of.
 13@@ -585,8 +586,46 @@ var MOD = function (...values) : number {
 14 };
 15 
 16 
 17-var TRUE = Formula["TRUE"];
 18-var NOT = Formula["NOT"];
 19+/**
 20+ * Returns true.
 21+ * @returns {boolean} true boolean
 22+ * @constructor
 23+ */
 24+var TRUE = function () : boolean {
 25+  return true;
 26+};
 27+
 28+
 29+/**
 30+ * Returns the opposite of a logical value - NOT(TRUE) returns FALSE; NOT(FALSE) returns TRUE.
 31+ * @param values[0] An expression or reference to a cell holding an expression that represents some logical value.
 32+ * @returns {boolean} opposite of a logical value input
 33+ * @constructor
 34+ */
 35+var NOT = function (...values) : boolean {
 36+  checkArgumentsLength(values, 1);
 37+  var X = values[0];
 38+  if (typeof(X) === "boolean") {
 39+    return !X;
 40+  }
 41+  if (typeof(X) === "string") {
 42+    if (X === "") {
 43+      return true;
 44+    }
 45+    throw new CellError(ERRORS.VALUE_ERROR, "Function NOT parameter 1 expects boolean values. But '" + X + "' is a text and cannot be coerced to a boolean.")
 46+  }
 47+  if (typeof(X) === "number") {
 48+    return X === 0;
 49+  }
 50+  if (X instanceof Array) {
 51+    if (X.length === 0) {
 52+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 53+    }
 54+    return NOT(X[0]);
 55+  }
 56+};
 57+
 58+
 59 var ODD = Formula["ODD"];
 60 var OR = Formula["OR"];
 61 var POWER = Formula["POWER"];
 62diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 63index 8cae9a2..f51ba44 100644
 64--- a/tests/FormulasTest.ts
 65+++ b/tests/FormulasTest.ts
 66@@ -492,7 +492,44 @@ assertEquals(MOD(10.1, 3), 1.0999999999999996);
 67 assertEquals(MOD(10, 3.1), 0.6999999999999997);
 68 
 69 
 70+// Test NOT
 71 assertEquals(NOT(TRUE()), false);
 72+assertEquals(NOT(""), true);
 73+catchAndAssertEquals(function() {
 74+  NOT(" ");
 75+}, ERRORS.VALUE_ERROR);
 76+assertEquals(NOT(100), false);
 77+assertEquals(NOT(0), true);
 78+assertEquals(NOT(-1), false);
 79+assertEquals(NOT(1), false);
 80+catchAndAssertEquals(function() {
 81+  NOT("0");
 82+}, ERRORS.VALUE_ERROR);
 83+catchAndAssertEquals(function() {
 84+  NOT([]);
 85+}, ERRORS.REF_ERROR);
 86+assertEquals(NOT([10]), false);
 87+assertEquals(NOT([0, 0]), true);
 88+assertEquals(NOT([0, false]), true);
 89+assertEquals(NOT([false, 0]), true);
 90+assertEquals(NOT([10, "str"]), false);
 91+catchAndAssertEquals(function() {
 92+  NOT("str");
 93+}, ERRORS.VALUE_ERROR);
 94+assertEquals(NOT([""]), true);
 95+assertEquals(NOT([0]), true);
 96+assertEquals(NOT([1]), false);
 97+assertEquals(NOT([0, 1]), true);
 98+catchAndAssertEquals(function() {
 99+  NOT("1.2");
100+}, ERRORS.VALUE_ERROR);
101+catchAndAssertEquals(function() {
102+  NOT();
103+}, ERRORS.NA_ERROR);
104+catchAndAssertEquals(function() {
105+  NOT(false, false);
106+}, ERRORS.NA_ERROR);
107+
108 
109 assertEquals(ODD(2), 3);
110