spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[SIGN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-05-25 03:29:51
stats
8 file(s) changed, 74 insertions(+), 5 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 9a675a4..a6a0087 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -926,6 +926,15 @@
  6 @constructor
  7 ```
  8 
  9+### SIGN 
 10+
 11+```
 12+  Given an input number, returns `-1` if it is negative, `1` if positive, and `0` if it is zero. 
 13+@param value - The value to check the sign for 
 14+@returns {number} `-1` if it is negative, `1` if positive, and `0` if it is zero. 
 15+@constructor
 16+```
 17+
 18 ### TRUNC 
 19 
 20 ```
 21diff --git a/TODO.md b/TODO.md
 22index ba4e331..5ff5974 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -55,7 +55,6 @@ For example 64 tbs to a qt.
 26 * PRODUCT
 27 * QUOTIENT
 28 * SERIESSUM
 29-* SIGN
 30 * SUBTOTAL
 31 * DIVIDE
 32 * EQ
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 9621939..5c1fb7c 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -56,6 +56,7 @@ exports.MULTIPLY = Math_1.MULTIPLY;
 38 exports.MINUS = Math_1.MINUS;
 39 exports.RAND = Math_1.RAND;
 40 exports.RANDBETWEEN = Math_1.RANDBETWEEN;
 41+exports.SIGN = Math_1.SIGN;
 42 var Logical_1 = require("./Logical");
 43 exports.AND = Logical_1.AND;
 44 exports.EXACT = Logical_1.EXACT;
 45diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 46index 63430a0..126229b 100644
 47--- a/dist/Formulas/Math.js
 48+++ b/dist/Formulas/Math.js
 49@@ -799,6 +799,21 @@ var RANDBETWEEN = function (low, high) {
 50     return Math.round(low + (Math.random() * diff));
 51 };
 52 exports.RANDBETWEEN = RANDBETWEEN;
 53+/**
 54+ * Given an input number, returns `-1` if it is negative, `1` if positive, and `0` if it is zero.
 55+ * @param value - The value to check the sign for
 56+ * @returns {number} `-1` if it is negative, `1` if positive, and `0` if it is zero.
 57+ * @constructor
 58+ */
 59+var SIGN = function (value) {
 60+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "SIGN");
 61+    var x = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
 62+    if (x === 0) {
 63+        return 0;
 64+    }
 65+    return x > 0 ? 1 : -1;
 66+};
 67+exports.SIGN = SIGN;
 68 /**
 69  * Truncates a number to a certain number of significant digits by omitting less significant digits.
 70  * @param value - The value to be truncated.
 71diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 72index f8bc6c9..a3fd20f 100644
 73--- a/src/Formulas/AllFormulas.ts
 74+++ b/src/Formulas/AllFormulas.ts
 75@@ -53,7 +53,8 @@ import {
 76   MULTIPLY,
 77   MINUS,
 78   RAND,
 79-  RANDBETWEEN
 80+  RANDBETWEEN,
 81+  SIGN
 82 } from "./Math";
 83 import {
 84   AND,
 85@@ -275,5 +276,6 @@ export {
 86   MULTIPLY,
 87   MINUS,
 88   RAND,
 89-  RANDBETWEEN
 90+  RANDBETWEEN,
 91+  SIGN
 92 }
 93\ No newline at end of file
 94diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
 95index 8fb48c2..9ebe3ca 100644
 96--- a/src/Formulas/Math.ts
 97+++ b/src/Formulas/Math.ts
 98@@ -808,6 +808,22 @@ var RANDBETWEEN = function (low, high) {
 99 };
100 
101 
102+/**
103+ * Given an input number, returns `-1` if it is negative, `1` if positive, and `0` if it is zero.
104+ * @param value - The value to check the sign for
105+ * @returns {number} `-1` if it is negative, `1` if positive, and `0` if it is zero.
106+ * @constructor
107+ */
108+var SIGN =  function (value) {
109+  ArgsChecker.checkLength(arguments, 1, "SIGN");
110+  var x = TypeConverter.firstValueAsNumber(value);
111+  if (x === 0) {
112+    return 0;
113+  }
114+  return x > 0 ? 1 : -1;
115+};
116+
117+
118 /**
119  * Truncates a number to a certain number of significant digits by omitting less significant digits.
120  * @param value - The value to be truncated.
121@@ -1104,5 +1120,6 @@ export {
122   DEGREES,
123   COMBIN,
124   RAND,
125-  RANDBETWEEN
126+  RANDBETWEEN,
127+  SIGN
128 }
129\ No newline at end of file
130diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
131index e79f5f2..f07fe27 100644
132--- a/tests/Formulas/MathTest.ts
133+++ b/tests/Formulas/MathTest.ts
134@@ -52,7 +52,9 @@ import {
135   DEGREES,
136   COMBIN,
137   MINUS,
138-  RAND, RANDBETWEEN
139+  RAND,
140+  RANDBETWEEN,
141+  SIGN
142 } from "../../src/Formulas/Math";
143 import * as ERRORS from "../../src/Errors";
144 import {
145@@ -1207,3 +1209,17 @@ test("RANDBETWEEN", function(){
146     RANDBETWEEN.apply(this, [3, 4, 5]);
147   }, ERRORS.NA_ERROR);
148 });
149+
150+
151+test("SIGN", function(){
152+  assertEquals(SIGN(2), 1);
153+  assertEquals(SIGN(0), 0);
154+  assertEquals(SIGN(-100), -1);
155+  assertEquals(SIGN([0]), 0);
156+  assertEquals(SIGN("0"), 0);
157+  assertEquals(SIGN(false), 0);
158+  assertEquals(SIGN(true), 1);
159+  catchAndAssertEquals(function() {
160+    SIGN.apply(this, [3, 2]);
161+  }, ERRORS.NA_ERROR);
162+});
163\ No newline at end of file
164diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
165index a9a2a47..b4e1cbf 100644
166--- a/tests/SheetFormulaTest.ts
167+++ b/tests/SheetFormulaTest.ts
168@@ -246,6 +246,10 @@ test("Sheet RAND", function(){
169   assertFormulaResultsInType('=RAND()', "number");
170 });
171 
172+test("Sheet RANDBETWEEN", function(){
173+  assertFormulaResultsInType('=RANDBETWEEN(1, 2)', "number");
174+});
175+
176 test("Sheet MULTIPLY", function(){
177   assertFormulaEquals('=MULTIPLY(10, 10)', 100);
178 });
179@@ -254,6 +258,10 @@ test("Sheet MULTIPLY", function(){
180   assertFormulaEquals('=MINUS(22, 1)', 21);
181 });
182 
183+test("Sheet SIGN", function(){
184+  assertFormulaEquals('=SIGN(100)', 1);
185+});
186+
187 test("Sheet DELTA", function(){
188   assertFormulaEquals('=DELTA(2, 2)', 1);
189 });