spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.OR written and tested.
author
Ben Vogt <[email protected]>
date
2017-02-04 21:13:34
stats
3 file(s) changed, 72 insertions(+), 1 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 972fda4..ea86778 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -46,7 +46,15 @@ import {
  6   CHAR,
  7   CODE
  8 } from "./Misc";
  9-import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber, checkArgumentsLength, firstValueAsNumber, firstValueAsString} from "./Utils";
 10+import {
 11+  checkArgumentsAtLeastLength,
 12+  filterOutStringValues,
 13+  valueToNumber,
 14+  checkArgumentsLength,
 15+  firstValueAsNumber,
 16+  firstValueAsString,
 17+  valueToBoolean
 18+} from "./Utils";
 19 import { CellError } from "../Errors"
 20 import * as ERRORS from "../Errors"
 21 
 22@@ -129,7 +137,32 @@ var IF = Formula["IF"];
 23 var LN = Formula["LN"];
 24 var LOG = Formula["LOG"];
 25 var LOG10 = Formula["LOG10"];
 26-var OR = Formula["OR"];
 27+
 28+
 29+/**
 30+ * Returns true if any of the provided arguments are logically true, and false if all of the provided arguments are logically false.
 31+ * TODO: Should this allow the acceptance of functions that return true or false?
 32+ * @param values An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE, or an expression that can be coerced to a logical value.
 33+ * @returns {boolean}
 34+ * @constructor
 35+ */
 36+var OR = function (...values) {
 37+  checkArgumentsAtLeastLength(values, 1);
 38+  for (var i = 0; i < values.length; i++) {
 39+    if (values[i] instanceof Array) {
 40+      if (values[i].length === 0) {
 41+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 42+      }
 43+      if (OR.apply(this, values[i])) {
 44+        return true;
 45+      }
 46+    } else if (valueToBoolean(values[i])) {
 47+      return true;
 48+    }
 49+  }
 50+  return false;
 51+};
 52+
 53 var POWER = Formula["POWER"];
 54 var ROUND = Formula["ROUND"];
 55 var ROUNDDOWN = Formula["ROUNDDOWN"];
 56diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 57index aa3768d..df07e1b 100644
 58--- a/src/RawFormulas/Utils.ts
 59+++ b/src/RawFormulas/Utils.ts
 60@@ -134,6 +134,20 @@ function stringValuesToZeros(arr: Array<any>) : Array<any> {
 61   return toReturn;
 62 }
 63 
 64+/**
 65+ * Converts any value to a boolean or throws an error if it cannot coerce it to the boolean type.
 66+ * @param value to convert
 67+ * @returns {boolean} to return.
 68+ */
 69+function valueToBoolean(value: any) : boolean {
 70+  if (typeof value === "number") {
 71+    return value !== 0;
 72+  } else if (typeof value === "string") {
 73+    throw new CellError(ERRORS.VALUE_ERROR, "AND expects boolean values. But '" + value + "' is a text and cannot be coerced to a boolean.")
 74+  } else if (typeof value === "boolean") {
 75+    return value;
 76+  }
 77+}
 78 
 79 /**
 80  * Flatten an array of arrays of ...
 81@@ -152,6 +166,7 @@ export {
 82   flatten,
 83   valueToNumber,
 84   valueToString,
 85+  valueToBoolean,
 86   firstValueAsNumber,
 87   firstValueAsString,
 88   filterOutStringValues,
 89diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 90index 5949415..b86f508 100644
 91--- a/tests/FormulasTest.ts
 92+++ b/tests/FormulasTest.ts
 93@@ -809,7 +809,30 @@ catchAndAssertEquals(function() {
 94 }, ERRORS.VALUE_ERROR);
 95 
 96 
 97+// Test OR
 98 assertEquals(OR(true, false), true);
 99+assertEquals(OR(false, false), false);
100+assertEquals(OR(1, 0), true);
101+assertEquals(OR([1, 0]), true);
102+assertEquals(OR(false, 0, -10), true);
103+assertEquals(OR([false, 0, -10]), true);
104+assertEquals(OR([false, 0, [-10]]), true);
105+catchAndAssertEquals(function() {
106+  OR([false, 0, []]);
107+}, ERRORS.REF_ERROR);
108+catchAndAssertEquals(function() {
109+  OR(false, "d");
110+}, ERRORS.VALUE_ERROR);
111+catchAndAssertEquals(function() {
112+  OR(false, "10");
113+}, ERRORS.VALUE_ERROR);
114+catchAndAssertEquals(function() {
115+  OR(false, "1.1");
116+}, ERRORS.VALUE_ERROR);
117+catchAndAssertEquals(function() {
118+  OR();
119+}, ERRORS.NA_ERROR);
120+
121 
122 assertEquals(PI(), 3.141592653589793);
123