spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.EXACT written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 17:34:04
stats
2 file(s) changed, 71 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
  2index d84366f..0b7b0c4 100644
  3--- a/src/RawFormulas.ts
  4+++ b/src/RawFormulas.ts
  5@@ -42,6 +42,23 @@ function filterOutStringValues(arr: Array<any>) : Array<any> {
  6   return toReturn;
  7 }
  8 
  9+/**
 10+ * Convert a value to string.
 11+ * @param value of any type, including array. array cannot be empty.
 12+ * @returns {string} string representation of value
 13+ */
 14+function valueToString(value: any) : string {
 15+  if (typeof value === "number") {
 16+    return value.toString();
 17+  } else if (typeof value === "string") {
 18+    return value;
 19+  } else if (typeof value === "boolean") {
 20+    return value ? "TRUE" : "FALSE";
 21+  } else if (value instanceof Array) {
 22+    return valueToString(value[0]);
 23+  }
 24+}
 25+
 26 
 27 /**
 28  * Converts any value to a number or throws an error if it cannot coerce it to the number type
 29@@ -453,7 +470,35 @@ var EVEN = function (...values) : number {
 30   return X % 2 === 1 ? X + 1 : X;
 31 };
 32 
 33-var EXACT = Formula["EXACT"];
 34+
 35+/**
 36+ * Tests whether two strings are identical, returning true if they are.
 37+ * @param values[0] The first string to compare
 38+ * @param values[1] The second string to compare
 39+ * @returns {boolean}
 40+ * @constructor
 41+ */
 42+var EXACT = function (...values) {
 43+  checkArgumentsLength(values, 2);
 44+  var one = values[0];
 45+  var two = values[1];
 46+  if (one instanceof Array) {
 47+    if (one.length === 0) {
 48+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 49+    }
 50+  }
 51+  if (two instanceof Array) {
 52+    if (two.length === 0) {
 53+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 54+    }
 55+  }
 56+  one = valueToString(one);
 57+  two = valueToString(two);
 58+  return one === two;
 59+};
 60+
 61+
 62+
 63 var EXPONDIST = Formula["EXPONDIST"];
 64 var FALSE = Formula["FALSE"];
 65 var __COMPLEX = {
 66diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 67index 42deefe..4666c62 100644
 68--- a/tests/FormulasTest.ts
 69+++ b/tests/FormulasTest.ts
 70@@ -367,7 +367,32 @@ catchAndAssertEquals(function() {
 71   EVEN("str");
 72 }, ERRORS.VALUE_ERROR);
 73 
 74+// Test EXACT
 75 assertEquals(EXACT("m", "M"), false);
 76+assertEquals(EXACT("m", "m"), true);
 77+assertEquals(EXACT("m", false), false);
 78+assertEquals(EXACT(false, false), true);
 79+assertEquals(EXACT(10, 10), true);
 80+assertEquals(EXACT(10, "10"), true);
 81+assertEquals(EXACT(10, "str"), false);
 82+assertEquals(EXACT([10], [10]), true);
 83+assertEquals(EXACT(["str"], [10, 22]), false);
 84+catchAndAssertEquals(function() {
 85+  EXACT([], []);
 86+}, ERRORS.REF_ERROR);
 87+catchAndAssertEquals(function() {
 88+  EXACT([]);
 89+}, ERRORS.NA_ERROR);
 90+catchAndAssertEquals(function() {
 91+  EXACT("m");
 92+}, ERRORS.NA_ERROR);
 93+catchAndAssertEquals(function() {
 94+  EXACT(10, 10, 10);
 95+}, ERRORS.NA_ERROR);
 96+catchAndAssertEquals(function() {
 97+  EXACT(false);
 98+}, ERRORS.NA_ERROR);
 99+
100 
101 assertEquals(EXPONDIST(4, 0.5, false), 0.06766764161830635);
102