spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.MOD written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 16:24:28
stats
2 file(s) changed, 45 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index 76fcf2e..a07d604 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -566,7 +566,25 @@ var MINA = function (...values) : number {
 6   return MIN.apply(this, values);
 7 };
 8 
 9-var MOD = Formula["MOD"];
10+
11+/**
12+ * Returns the result of the modulo operator, the remainder after a division operation.
13+ * @param values[0] The number to be divided to find the remainder.
14+ * @param values[1] The number to divide by.
15+ * @returns {number}
16+ * @constructor
17+ */
18+var MOD = function (...values) : number {
19+  checkArgumentsLength(values, 2);
20+  var oneN = valueToNumber(values[0]);
21+  var twoN =  valueToNumber(values[1]);
22+  if (twoN === 0) {
23+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Function MOD parameter 2 cannot be zero.");
24+  }
25+  return oneN % twoN;
26+};
27+
28+
29 var TRUE = Formula["TRUE"];
30 var NOT = Formula["NOT"];
31 var ODD = Formula["ODD"];
32diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
33index a6a4227..8cae9a2 100644
34--- a/tests/FormulasTest.ts
35+++ b/tests/FormulasTest.ts
36@@ -464,7 +464,33 @@ catchAndAssertEquals(function() {
37 assertEquals(MINA(100, 22, 44), 22);
38 
39 
40+// Test MOD
41 assertEquals(MOD(10, 3), 1);
42+catchAndAssertEquals(function() {
43+  MOD(10, 3, 10);
44+}, ERRORS.NA_ERROR);
45+catchAndAssertEquals(function() {
46+  MOD([10, 3]);
47+}, ERRORS.NA_ERROR);
48+catchAndAssertEquals(function() {
49+  MOD(0, 0);
50+}, ERRORS.DIV_ZERO_ERROR);
51+catchAndAssertEquals(function() {
52+  MOD(10);
53+}, ERRORS.NA_ERROR);
54+catchAndAssertEquals(function() {
55+  MOD(10, false);
56+}, ERRORS.DIV_ZERO_ERROR);
57+catchAndAssertEquals(function() {
58+  MOD(10, 0);
59+}, ERRORS.DIV_ZERO_ERROR);
60+catchAndAssertEquals(function() {
61+  MOD(10, "str");
62+}, ERRORS.VALUE_ERROR);
63+assertEquals(MOD(10, "3"), 1);
64+assertEquals(MOD(10.1, 3), 1.0999999999999996);
65+assertEquals(MOD(10, 3.1), 0.6999999999999997);
66+
67 
68 assertEquals(NOT(TRUE()), false);
69