spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Refactoring finished formulas into appropriate categories
author
Ben Vogt <[email protected]>
date
2017-02-04 18:00:32
stats
3 file(s) changed, 205 insertions(+), 178 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/Misc.ts
src/RawFormulas/RawFormulas.ts
  1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
  2index 4846433..ca029b6 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} from "./Utils"
  8+    stringValuesToZeros, firstValueAsNumber} from "./Utils"
  9 import { CellError } from "../Errors"
 10 import * as ERRORS from "../Errors"
 11 
 12@@ -467,6 +467,143 @@ var SUM = function (...values) : number {
 13   return result;
 14 };
 15 
 16+/**
 17+ * Returns the positive square root of a positive number.
 18+ * @param values[0] The number for which to calculate the positive square root.
 19+ * @returns {number} square root
 20+ * @constructor
 21+ */
 22+var SQRT = function (...values) : number {
 23+  checkArgumentsLength(values, 1);
 24+  var x = firstValueAsNumber(values[0]);
 25+  if (x < 0) {
 26+    throw new CellError(ERRORS.VALUE_ERROR, "Function SQRT parameter 1 expects number values. But '" + values[0] + "' is a text and cannot be coerced to a number.");
 27+  }
 28+  return Math.sqrt(x);
 29+};
 30+
 31+/**
 32+ * Returns the cosine of an angle provided in radians.
 33+ * @param values[0] The angle to find the cosine of, in radians.
 34+ * @returns {number} cosine of angle
 35+ * @constructor
 36+ */
 37+var COS = function (...values) : number {
 38+  checkArgumentsLength(values, 1);
 39+  var r = firstValueAsNumber(values[0]);
 40+  return Math.cos(r);
 41+};
 42+
 43+/**
 44+ * Returns the hyperbolic cosine of any real number.
 45+ * @param values[0] Any real value to calculate the hyperbolic cosine of.
 46+ * @returns {number} the hyperbolic cosine of the input
 47+ * @constructor
 48+ */
 49+var COSH = function (...values) : number {
 50+  checkArgumentsLength(values, 1);
 51+  var r = firstValueAsNumber(values[0]);
 52+  return Math["cosh"](r);
 53+};
 54+
 55+/**
 56+ * Returns the cotangent of any real number. Defined as cot(x) = 1 / tan(x).
 57+ * @param values[0] number to calculate the cotangent for
 58+ * @returns {number} cotangent
 59+ * @constructor
 60+ */
 61+var COT = function (...values) : number {
 62+  checkArgumentsLength(values, 1);
 63+  var x = firstValueAsNumber(values[0]);
 64+  if (x === 0) {
 65+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function COT caused a divide by zero error.");
 66+  }
 67+  return 1 / Math.tan(x);
 68+};
 69+
 70+/**
 71+ * Return the hyperbolic cotangent of a value, defined as coth(x) = 1 / tanh(x).
 72+ * @param values[0] value to calculate the hyperbolic cotangent value of
 73+ * @returns {number} hyperbolic cotangent
 74+ * @constructor
 75+ */
 76+var COTH = function (...values) : number {
 77+  checkArgumentsLength(values, 1);
 78+  var x = firstValueAsNumber(values[0]);
 79+  if (x === 0) {
 80+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function COTH caused a divide by zero error.");
 81+  }
 82+  return 1 / Math["tanh"](x);
 83+};
 84+
 85+/**
 86+ * Rounds a number down to the nearest integer that is less than or equal to it.
 87+ * @param values[0] The value to round down to the nearest integer.
 88+ * @returns {number} Rounded number
 89+ * @constructor
 90+ */
 91+var INT = function (...values) : number {
 92+  checkArgumentsLength(values, 1);
 93+  var x = firstValueAsNumber(values[0]);
 94+  return Math.floor(x);
 95+};
 96+
 97+
 98+/**
 99+ * Checks whether the provided value is even.
100+ * @param values[0] The value to be verified as even.
101+ * @returns {boolean} whether this value is even or not
102+ * @constructor
103+ */
104+var ISEVEN = function (...values) : boolean {
105+  checkArgumentsLength(values, 1);
106+  if (values[0] === "") {
107+    throw new CellError(ERRORS.VALUE_ERROR, "Function ISEVEN parameter 1 expects boolean values. But '" + values[0] + "' is a text and cannot be coerced to a boolean.");
108+  }
109+  var x = firstValueAsNumber(values[0]);
110+  return Math.floor(x) % 2 === 0;
111+};
112+
113+
114+/**
115+ * Checks whether the provided value is odd.
116+ * @param values[0] The value to be verified as odd.
117+ * @returns {boolean} whether this value is odd or not
118+ * @constructor
119+ */
120+var ISODD = function (...values) : boolean {
121+  checkArgumentsLength(values, 1);
122+  if (values[0] === "") {
123+    throw new CellError(ERRORS.VALUE_ERROR, "Function ISODD parameter 1 expects boolean values. But '" + values[0] + "' is a text and cannot be coerced to a boolean.");
124+  }
125+  var x = firstValueAsNumber(values[0]);
126+  return Math.floor(x) % 2 === 1;
127+};
128+
129+/**
130+ * Returns the sine of an angle provided in radians.
131+ * @param values[0] The angle to find the sine of, in radians.
132+ * @returns {number} Sine of angle.
133+ * @constructor
134+ */
135+var SIN = function (...values) {
136+  checkArgumentsLength(values, 1);
137+  var rad = firstValueAsNumber(values[0]);
138+  return rad === Math.PI ? 0 : Math.sin(rad);
139+};
140+
141+/**
142+ * Returns the hyperbolic sine of any real number.
143+ * @param values[0] real number to find the hyperbolic sine of
144+ * @returns {number} hyperbolic sine
145+ * @constructor
146+ */
147+var SINH = function (...values) : number {
148+  checkArgumentsLength(values, 1);
149+  var rad = firstValueAsNumber(values[0]);
150+  return Math["sinh"](rad);
151+};
152+
153 /**
154  * The value Pi.
155  * @returns {number} Pi.
156@@ -490,7 +627,14 @@ export {
157   AVERAGE,
158   AVERAGEA,
159   AVEDEV,
160+  COT,
161+  COTH,
162+  COSH,
163+  COS,
164   EVEN,
165+  INT,
166+  ISEVEN,
167+  ISODD,
168   MAX,
169   MAXA,
170   MEDIAN,
171@@ -498,6 +642,9 @@ export {
172   MINA,
173   MOD,
174   ODD,
175+  SIN,
176+  SINH,
177   SUM,
178+  SQRT,
179   PI
180 }
181\ No newline at end of file
182diff --git a/src/RawFormulas/Misc.ts b/src/RawFormulas/Misc.ts
183new file mode 100644
184index 0000000..e9fd1b8
185--- /dev/null
186+++ b/src/RawFormulas/Misc.ts
187@@ -0,0 +1,38 @@
188+import { firstValueAsNumber, checkArgumentsLength, firstValueAsString } from "./Utils"
189+import { CellError } from "../Errors"
190+import * as ERRORS from "../Errors"
191+
192+/**
193+ * Convert a number into a character according to the current Unicode table.
194+ * @param values[0] The number of the character to look up from the current Unicode table in decimal format.
195+ * @returns {string} character corresponding to Unicode number
196+ * @constructor
197+ */
198+var CHAR = function (...values) : string {
199+  checkArgumentsLength(values, 1);
200+  var n = firstValueAsNumber(values[0]);
201+  if (n < 1 || n > 1114112) { //limit
202+    throw new CellError(ERRORS.NUM_ERROR, "Function CHAR parameter 1 value " + n + " is out of range.");
203+  }
204+  return String.fromCharCode(n);
205+};
206+
207+/**
208+ * Returns the numeric Unicode map value of the first character in the string provided.
209+ * @param values[0] The string whose first character's Unicode map value will be returned.
210+ * @returns {number} number of the first character's Unicode value
211+ * @constructor
212+ */
213+var CODE = function (...values) : number {
214+  checkArgumentsLength(values, 1);
215+  var text = firstValueAsString(values[0]);
216+  if (text === "") {
217+    throw new CellError(ERRORS.VALUE_ERROR, "Function CODE parameter 1 value should be non-empty.");
218+  }
219+  return text.charCodeAt(0);
220+};
221+
222+export {
223+  CHAR,
224+  CODE
225+}
226\ No newline at end of file
227diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
228index e993bf5..80c1f55 100644
229--- a/src/RawFormulas/RawFormulas.ts
230+++ b/src/RawFormulas/RawFormulas.ts
231@@ -15,7 +15,14 @@ import {
232   AVERAGE,
233   AVERAGEA,
234   AVEDEV,
235+  COT,
236+  COTH,
237+  COSH,
238+  COS,
239   EVEN,
240+  INT,
241+  ISEVEN,
242+  ISODD,
243   MAX,
244   MAXA,
245   MEDIAN,
246@@ -23,7 +30,10 @@ import {
247   MINA,
248   MOD,
249   ODD,
250+  SIN,
251+  SINH,
252   SUM,
253+  SQRT,
254   PI
255 } from "./Math";
256 import {
257@@ -31,7 +41,11 @@ import {
258   EXACT,
259   TRUE,
260   NOT
261-} from "./Logical"
262+} from "./Logical";
263+import {
264+  CHAR,
265+  CODE
266+} from "./Misc";
267 import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber, checkArgumentsLength, firstValueAsNumber, firstValueAsString} from "./Utils";
268 import { CellError } from "../Errors"
269 import * as ERRORS from "../Errors"
270@@ -57,100 +71,12 @@ var DECIMAL = Formula["DECIMAL"];
271 var CEILING = Formula["CEILING"];
272 var CEILINGMATH = Formula["CEILINGMATH"];
273 var CEILINGPRECISE = Formula["CEILINGPRECISE"];
274-
275-
276-/**
277- * Convert a number into a character according to the current Unicode table.
278- * @param values[0] The number of the character to look up from the current Unicode table in decimal format.
279- * @returns {string} character corresponding to Unicode number
280- * @constructor
281- */
282-var CHAR = function (...values) : string {
283-  checkArgumentsLength(values, 1);
284-  var n = firstValueAsNumber(values[0]);
285-  if (n < 1 || n > 1114112) { //limit
286-    throw new CellError(ERRORS.NUM_ERROR, "Function CHAR parameter 1 value " + n + " is out of range.");
287-  }
288-  return String.fromCharCode(n);
289-};
290-
291-/**
292- * Returns the numeric Unicode map value of the first character in the string provided.
293- * @param values[0] The string whose first character's Unicode map value will be returned.
294- * @returns {number} number of the first character's Unicode value
295- * @constructor
296- */
297-var CODE = function (...values) : number {
298-  checkArgumentsLength(values, 1);
299-  var text = firstValueAsString(values[0]);
300-  if (text === "") {
301-    throw new CellError(ERRORS.VALUE_ERROR, "Function CODE parameter 1 value should be non-empty.");
302-  }
303-  return text.charCodeAt(0);
304-};
305 var COMBIN = Formula["COMBIN"];
306 var COMBINA = Formula["COMBINA"];
307 var COMPLEX = Formula["COMPLEX"];
308 var CONCATENATE = Formula["CONCATENATE"];
309 var CONVERT = Formula["CONVERT"];
310 var CORREL = Formula["CORREL"];
311-
312-/**
313- * Returns the cosine of an angle provided in radians.
314- * @param values[0] The angle to find the cosine of, in radians.
315- * @returns {number} cosine of angle
316- * @constructor
317- */
318-var COS = function (...values) : number {
319-  checkArgumentsLength(values, 1);
320-  var r = firstValueAsNumber(values[0]);
321-  return Math.cos(r);
322-};
323-
324-/**
325- * Returns the hyperbolic cosine of any real number.
326- * @param values[0] Any real value to calculate the hyperbolic cosine of.
327- * @returns {number} the hyperbolic cosine of the input
328- * @constructor
329- */
330-var COSH = function (...values) : number {
331-  checkArgumentsLength(values, 1);
332-  var r = firstValueAsNumber(values[0]);
333-  return Math["cosh"](r);
334-};
335-
336-/**
337- * Returns the cotangent of any real number. Defined as cot(x) = 1 / tan(x).
338- * @param values[0] number to calculate the cotangent for
339- * @returns {number} cotangent
340- * @constructor
341- */
342-var COT = function (...values) : number {
343-  checkArgumentsLength(values, 1);
344-  var x = firstValueAsNumber(values[0]);
345-  if (x === 0) {
346-    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function COT caused a divide by zero error.");
347-  }
348-  return 1 / Math.tan(x);
349-};
350-
351-
352-/**
353- * Return the hyperbolic cotangent of a value, defined as coth(x) = 1 / tanh(x).
354- * @param values[0] value to calculate the hyperbolic cotangent value of
355- * @returns {number} hyperbolic cotangent
356- * @constructor
357- */
358-var COTH = function (...values) : number {
359-  checkArgumentsLength(values, 1);
360-  var x = firstValueAsNumber(values[0]);
361-  if (x === 0) {
362-    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function COTH caused a divide by zero error.");
363-  }
364-  return 1 / Math["tanh"](x);
365-};
366-
367-
368 var COUNT = Formula["COUNT"];
369 var COUNTA = Formula["COUNTA"];
370 var COUNTIF = Formula["COUNTIF"];
371@@ -200,52 +126,6 @@ var __COMPLEX = {
372 var FISHER = Formula["FISHER"];
373 var FISHERINV = Formula["FISHERINV"];
374 var IF = Formula["IF"];
375-
376-/**
377- * Rounds a number down to the nearest integer that is less than or equal to it.
378- * @param values[0] The value to round down to the nearest integer.
379- * @returns {number} Rounded number
380- * @constructor
381- */
382-var INT = function (...values) : number {
383-  checkArgumentsLength(values, 1);
384-  var x = firstValueAsNumber(values[0]);
385-  return Math.floor(x);
386-};
387-
388-
389-/**
390- * Checks whether the provided value is even.
391- * @param values[0] The value to be verified as even.
392- * @returns {boolean} whether this value is even or not
393- * @constructor
394- */
395-var ISEVEN = function (...values) : boolean {
396-  checkArgumentsLength(values, 1);
397-  if (values[0] === "") {
398-    throw new CellError(ERRORS.VALUE_ERROR, "Function ISEVEN parameter 1 expects boolean values. But '" + values[0] + "' is a text and cannot be coerced to a boolean.");
399-  }
400-  var x = firstValueAsNumber(values[0]);
401-  return Math.floor(x) % 2 === 0;
402-};
403-
404-
405-/**
406- * Checks whether the provided value is odd.
407- * @param values[0] The value to be verified as odd.
408- * @returns {boolean} whether this value is odd or not
409- * @constructor
410- */
411-var ISODD = function (...values) : boolean {
412-  checkArgumentsLength(values, 1);
413-  if (values[0] === "") {
414-    throw new CellError(ERRORS.VALUE_ERROR, "Function ISODD parameter 1 expects boolean values. But '" + values[0] + "' is a text and cannot be coerced to a boolean.");
415-  }
416-  var x = firstValueAsNumber(values[0]);
417-  return Math.floor(x) % 2 === 1;
418-};
419-
420-
421 var LN = Formula["LN"];
422 var LOG = Formula["LOG"];
423 var LOG10 = Formula["LOG10"];
424@@ -254,50 +134,7 @@ var POWER = Formula["POWER"];
425 var ROUND = Formula["ROUND"];
426 var ROUNDDOWN = Formula["ROUNDDOWN"];
427 var ROUNDUP = Formula["ROUNDUP"];
428-
429-/**
430- * Returns the sine of an angle provided in radians.
431- * @param values[0] The angle to find the sine of, in radians.
432- * @returns {number} Sine of angle.
433- * @constructor
434- */
435-var SIN = function (...values) {
436-  checkArgumentsLength(values, 1);
437-  var rad = firstValueAsNumber(values[0]);
438-  return rad === Math.PI ? 0 : Math.sin(rad);
439-};
440-
441-/**
442- * Returns the hyperbolic sine of any real number.
443- * @param values[0] real number to find the hyperbolic sine of
444- * @returns {number} hyperbolic sine
445- * @constructor
446- */
447-var SINH = function (...values) : number {
448-  checkArgumentsLength(values, 1);
449-  var rad = firstValueAsNumber(values[0]);
450-  return Math["sinh"](rad);
451-};
452-
453-
454 var SPLIT = Formula["SPLIT"];
455-
456-
457-/**
458- * Returns the positive square root of a positive number.
459- * @param values[0] The number for which to calculate the positive square root.
460- * @returns {number} square root
461- * @constructor
462- */
463-var SQRT = function (...values) : number {
464-  checkArgumentsLength(values, 1);
465-  var x = firstValueAsNumber(values[0]);
466-  if (x < 0) {
467-    throw new CellError(ERRORS.VALUE_ERROR, "Function SQRT parameter 1 expects number values. But '" + values[0] + "' is a text and cannot be coerced to a number.");
468-  }
469-  return Math.sqrt(x);
470-};
471-
472 var SQRTPI = Formula["SQRTPI"];
473 var SUMIF = Formula["SUMIF"];
474 var SUMPRODUCT = Formula["SUMPRODUCT"];