spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Refactoring some of the functions
author
Ben Vogt <[email protected]>
date
2017-02-04 22:29:23
stats
2 file(s) changed, 122 insertions(+), 114 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/RawFormulas.ts
  1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
  2index ca029b6..41025c8 100644
  3--- a/src/RawFormulas/Math.ts
  4+++ b/src/RawFormulas/Math.ts
  5@@ -1,5 +1,5 @@
  6 import { checkArgumentsLength, checkArgumentsAtLeastLength, valueToNumber, filterOutStringValues, flatten,
  7-    stringValuesToZeros, firstValueAsNumber} from "./Utils"
  8+    stringValuesToZeros, firstValueAsNumber, valueToBoolean} from "./Utils"
  9 import { CellError } from "../Errors"
 10 import * as ERRORS from "../Errors"
 11 
 12@@ -445,6 +445,20 @@ var ODD = function (...values) : number {
 13   return X % 2 === 1 ? X : X + 1;
 14 };
 15 
 16+/**
 17+ * Returns a number raised to a power.
 18+ * @param values[0] The number to raise to the exponent power.
 19+ * @param values[1] The exponent to raise base to.
 20+ * @returns {number} resulting number
 21+ * @constructor
 22+ */
 23+var POWER = function (...values) : number {
 24+  checkArgumentsLength(values, 2);
 25+  var n = firstValueAsNumber(values[0]);
 26+  var p = firstValueAsNumber(values[1]);
 27+  return Math.pow(n, p);
 28+};
 29+
 30 /**
 31  * Returns the sum of a series of numbers and/or cells.
 32  * @param values The first number or range to add together.
 33@@ -613,6 +627,99 @@ var PI = function () {
 34   return Math.PI;
 35 };
 36 
 37+/**
 38+ * Returns the the logarithm of a number, base 10.
 39+ * @param values[0] The value for which to calculate the logarithm, base 10.
 40+ * @returns {number} logarithm of the number, in base 10.
 41+ * @constructor
 42+ */
 43+var LOG10 = function (...values) : number {
 44+  checkArgumentsLength(values, 1);
 45+  var n = firstValueAsNumber(values[0]);
 46+  if (n < 1) {
 47+    throw new CellError(ERRORS.NUM_ERROR, "Function LOG10 parameter 1 value is " + n + ". It should be greater than 0.");
 48+  }
 49+  var ln = Math.log(n);
 50+  var lb = Math.log(10);
 51+  return ln / lb;
 52+};
 53+
 54+/**
 55+ * Returns the the logarithm of a number given a base.
 56+ * @param values[0] The value for which to calculate the logarithm given base.
 57+ * @param values[1] The base to use for calculation of the logarithm. Defaults to 10.
 58+ * @returns {number}
 59+ * @constructor
 60+ */
 61+var LOG = function (...values) : number {
 62+  checkArgumentsAtLeastLength(values, 1);
 63+  var n = firstValueAsNumber(values[0]);
 64+  var b = 10;
 65+  if (values.length > 1) {
 66+    b = firstValueAsNumber(values[1]);
 67+    if (b < 1) {
 68+      throw new CellError(ERRORS.NUM_ERROR, "Function LOG parameter 2 value is " + b + ". It should be greater than 0.");
 69+    }
 70+  }
 71+  if (b < 2) {
 72+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
 73+  }
 74+  var ln = Math.log(n);
 75+  var lb = Math.log(b);
 76+  if (lb === 0) {
 77+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
 78+  }
 79+  return ln / lb;
 80+};
 81+
 82+/**
 83+ * Returns true if any of the provided arguments are logically true, and false if all of the provided arguments are logically false.
 84+ * TODO: Should this allow the acceptance of functions that return true or false?
 85+ * @param values An 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.
 86+ * @returns {boolean}
 87+ * @constructor
 88+ */
 89+var OR = function (...values) {
 90+  checkArgumentsAtLeastLength(values, 1);
 91+  for (var i = 0; i < values.length; i++) {
 92+    if (values[i] instanceof Array) {
 93+      if (values[i].length === 0) {
 94+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 95+      }
 96+      if (OR.apply(this, values[i])) {
 97+        return true;
 98+      }
 99+    } else if (valueToBoolean(values[i])) {
100+      return true;
101+    }
102+  }
103+  return false;
104+};
105+
106+/**
107+ * Returns the tangent of an angle provided in radians.
108+ * @param values The angle to find the tangent of, in radians.
109+ * @returns {number} tangent in radians
110+ * @constructor
111+ */
112+var TAN = function (...values) : number {
113+  checkArgumentsLength(values, 1);
114+  var rad = firstValueAsNumber(values[0]);
115+  return rad === Math.PI ? 0 : Math.tan(rad);
116+};
117+
118+/**
119+ * Returns the hyperbolic tangent of any real number.
120+ * @param values[0] Any real value to calculate the hyperbolic tangent of.
121+ * @returns {number} hyperbolic tangent
122+ * @constructor
123+ */
124+var TANH = function (...values) : number {
125+  checkArgumentsLength(values, 1);
126+  var rad = firstValueAsNumber(values[0]);
127+  return Math["tanh"](rad);
128+};
129+
130 export {
131   ABS,
132   ACOS,
133@@ -646,5 +753,11 @@ export {
134   SINH,
135   SUM,
136   SQRT,
137-  PI
138+  PI,
139+  POWER,
140+  OR,
141+  LOG,
142+  LOG10,
143+  TAN,
144+  TANH
145 }
146\ No newline at end of file
147diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
148index 8effb7f..a072a42 100644
149--- a/src/RawFormulas/RawFormulas.ts
150+++ b/src/RawFormulas/RawFormulas.ts
151@@ -34,7 +34,13 @@ import {
152   SINH,
153   SUM,
154   SQRT,
155-  PI
156+  PI,
157+  POWER,
158+  OR,
159+  LOG,
160+  LOG10,
161+  TAN,
162+  TANH
163 } from "./Math";
164 import {
165   AND,
166@@ -135,91 +141,6 @@ var FISHER = Formula["FISHER"];
167 var FISHERINV = Formula["FISHERINV"];
168 var IF = Formula["IF"];
169 var LN = Formula["LN"];
170-
171-
172-/**
173- * Returns the the logarithm of a number, base 10.
174- * @param values[0] The value for which to calculate the logarithm, base 10.
175- * @returns {number} logarithm of the number, in base 10.
176- * @constructor
177- */
178-var LOG10 = function (...values) : number {
179-  checkArgumentsLength(values, 1);
180-  var n = firstValueAsNumber(values[0]);
181-  if (n < 1) {
182-    throw new CellError(ERRORS.NUM_ERROR, "Function LOG10 parameter 1 value is " + n + ". It should be greater than 0.");
183-  }
184-  var ln = Math.log(n);
185-  var lb = Math.log(10);
186-  return ln / lb;
187-};
188-
189-/**
190- * Returns the the logarithm of a number given a base.
191- * @param values[0] The value for which to calculate the logarithm given base.
192- * @param values[1] The base to use for calculation of the logarithm. Defaults to 10.
193- * @returns {number}
194- * @constructor
195- */
196-var LOG = function (...values) : number {
197-  checkArgumentsAtLeastLength(values, 1);
198-  var n = firstValueAsNumber(values[0]);
199-  var b = 10;
200-  if (values.length > 1) {
201-    b = firstValueAsNumber(values[1]);
202-    if (b < 1) {
203-      throw new CellError(ERRORS.NUM_ERROR, "Function LOG parameter 2 value is " + b + ". It should be greater than 0.");
204-    }
205-  }
206-  if (b < 2) {
207-    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
208-  }
209-  var ln = Math.log(n);
210-  var lb = Math.log(b);
211-  if (lb === 0) {
212-    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
213-  }
214-  return ln / lb;
215-};
216-
217-/**
218- * Returns true if any of the provided arguments are logically true, and false if all of the provided arguments are logically false.
219- * TODO: Should this allow the acceptance of functions that return true or false?
220- * @param values An 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.
221- * @returns {boolean}
222- * @constructor
223- */
224-var OR = function (...values) {
225-  checkArgumentsAtLeastLength(values, 1);
226-  for (var i = 0; i < values.length; i++) {
227-    if (values[i] instanceof Array) {
228-      if (values[i].length === 0) {
229-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
230-      }
231-      if (OR.apply(this, values[i])) {
232-        return true;
233-      }
234-    } else if (valueToBoolean(values[i])) {
235-      return true;
236-    }
237-  }
238-  return false;
239-};
240-
241-/**
242- * Returns a number raised to a power.
243- * @param values[0] The number to raise to the exponent power.
244- * @param values[1] The exponent to raise base to.
245- * @returns {number} resulting number
246- * @constructor
247- */
248-var POWER = function (...values) : number {
249-  checkArgumentsLength(values, 2);
250-  var n = firstValueAsNumber(values[0]);
251-  var p = firstValueAsNumber(values[1]);
252-  return Math.pow(n, p);
253-};
254-
255 var ROUND = Formula["ROUND"];
256 var ROUNDDOWN = Formula["ROUNDDOWN"];
257 var ROUNDUP = Formula["ROUNDUP"];
258@@ -230,32 +151,6 @@ var SUMPRODUCT = Formula["SUMPRODUCT"];
259 var SUMSQ = Formula["SUMSQ"];
260 var SUMX2MY2 = Formula["SUMX2MY2"];
261 var SUMX2PY2 = Formula["SUMX2PY2"];
262-
263-/**
264- * Returns the tangent of an angle provided in radians.
265- * @param values The angle to find the tangent of, in radians.
266- * @returns {number} tangent in radians
267- * @constructor
268- */
269-var TAN = function (...values) : number {
270-  checkArgumentsLength(values, 1);
271-  var rad = firstValueAsNumber(values[0]);
272-  return rad === Math.PI ? 0 : Math.tan(rad);
273-};
274-
275-/**
276- * Returns the hyperbolic tangent of any real number.
277- * @param values[0] Any real value to calculate the hyperbolic tangent of.
278- * @returns {number} hyperbolic tangent
279- * @constructor
280- */
281-var TANH = function (...values) : number {
282-  checkArgumentsLength(values, 1);
283-  var rad = firstValueAsNumber(values[0]);
284-  return Math["tanh"](rad);
285-};
286-
287-
288 var TRUNC = Formula["TRUNC"];
289 var XOR = Formula["XOR"];
290 var YEARFRAC = Formula["YEARFRAC"];