spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.ODD written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 17:04:12
stats
2 file(s) changed, 38 insertions(+), 1 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index 54a6265..4f7aac3 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -626,7 +626,25 @@ var NOT = function (...values) : boolean {
 6 };
 7 
 8 
 9-var ODD = Formula["ODD"];
10+/**
11+ * Rounds a number up to the nearest odd integer.
12+ * @param values[0] The value to round to the next greatest odd number.
13+ * @returns {number} value to round up to next greatest odd number.
14+ * @constructor
15+ */
16+var ODD = function (...values) : number {
17+  checkArgumentsLength(values, 1);
18+  if (values[0] instanceof Array) {
19+    if (values[0].length === 0) {
20+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
21+    }
22+    return ODD(values[0][0]);
23+  }
24+  var X = valueToNumber(values[0]);
25+  return X % 2 === 1 ? X : X + 1;
26+};
27+
28+
29 var OR = Formula["OR"];
30 var POWER = Formula["POWER"];
31 var ROUND = Formula["ROUND"];
32diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
33index f51ba44..69b7624 100644
34--- a/tests/FormulasTest.ts
35+++ b/tests/FormulasTest.ts
36@@ -531,7 +531,25 @@ catchAndAssertEquals(function() {
37 }, ERRORS.NA_ERROR);
38 
39 
40+// Test ODD
41 assertEquals(ODD(2), 3);
42+assertEquals(ODD(4), 5);
43+assertEquals(ODD(5), 5);
44+assertEquals(ODD("4"), 5);
45+assertEquals(ODD(false), 1);
46+assertEquals(ODD(true), 1);
47+assertEquals(ODD([10, 22]), 11);
48+assertEquals(ODD([10, 22, "str"]), 11);
49+catchAndAssertEquals(function() {
50+  ODD();
51+}, ERRORS.NA_ERROR);
52+catchAndAssertEquals(function() {
53+  ODD(1, 2, 3);
54+}, ERRORS.NA_ERROR);
55+catchAndAssertEquals(function() {
56+  ODD("str");
57+}, ERRORS.VALUE_ERROR);
58+
59 
60 assertEquals(OR(true, false), true);
61