spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.AND written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-16 22:08:31
stats
2 file(s) changed, 47 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index f0dbc93..b0bb0dd 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -16,6 +16,18 @@ function checkArgumentsLength(args: any, length: number) {
 6   }
 7 }
 8 
 9+/**
10+ * Checks to see if the arguments are at least a certain length.
11+ * @param args to check length of
12+ * @param length expected length
13+ */
14+function checkArgumentsAtLeastLength(args: any, length: number) {
15+  if (args.length < length) {
16+    throw new CellError(ERRORS.NA_ERROR, "Wrong number of arguments to ABS. Expected 1 arguments, but got " + args.length + " arguments.");
17+  }
18+}
19+
20+
21 /**
22  * Converts any value to a number or throws an error if it cannot coerce it to the number type
23  * @param value to convert
24@@ -160,7 +172,28 @@ var ACOTH = function (value?) {
25 };
26 
27 
28-var AND = Formula["AND"];
29+/**
30+ * Returns true if all of the provided arguments are logically true, and false if any of the provided arguments are logically false.
31+ * @param values At least one 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.
32+ * @returns {boolean} if all values are logically true.
33+ * @constructor
34+ */
35+var AND = function (...values) {
36+  checkArgumentsAtLeastLength(values, 1);
37+  var result = true;
38+  for (var i = 0; i < values.length; i++) {
39+    if (typeof values[i] === "string") {
40+      throw new CellError(ERRORS.VALUE_ERROR, "AND expects boolean values. But '" + values[i] + "' is a text and cannot be coerced to a boolean.")
41+    }
42+    if (!values[i]) {
43+      result = false;
44+      break;
45+    }
46+  }
47+  return result;
48+};
49+
50+
51 var ARABIC = Formula["ARABIC"];
52 var ASIN = Formula["ASIN"];
53 var ASINH = Formula["ASINH"];
54diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
55index 9f02a4b..5e45162 100644
56--- a/tests/FormulasTest.ts
57+++ b/tests/FormulasTest.ts
58@@ -85,8 +85,21 @@ catchAndAssertEquals(function() {
59 }, ERRORS.NUM_ERROR);
60 
61 
62+// Test AND
63 assertEquals(AND(10, 10), true);
64 assertEquals(AND(10, 0), false);
65+assertEquals(AND(10, false), false);
66+assertEquals(AND(0, 0), false);
67+catchAndAssertEquals(function() {
68+  return AND(1, "");
69+}, ERRORS.VALUE_ERROR);
70+catchAndAssertEquals(function() {
71+  return AND();
72+}, ERRORS.NA_ERROR);
73+catchAndAssertEquals(function() {
74+  return AND(1, "str");
75+}, ERRORS.VALUE_ERROR);
76+
77 
78 assertEquals(ARABIC("XIV"), 14);
79