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 more formulas into their correct files
author
Ben Vogt <[email protected]>
date
2017-02-16 03:00:40
stats
3 file(s) changed, 149 insertions(+), 108 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 2ae9e1e..c378311 100644
  3--- a/src/RawFormulas/Math.ts
  4+++ b/src/RawFormulas/Math.ts
  5@@ -1,7 +1,19 @@
  6-import { checkArgumentsLength, checkArgumentsAtLeastLength, valueToNumber, filterOutStringValues, flatten,
  7-    stringValuesToZeros, firstValueAsNumber, valueToBoolean, checkArgumentsAtWithin, CriteriaFunctionFactory, valueCanCoerceToNumber} from "./Utils"
  8-import { CellError } from "../Errors"
  9-import * as ERRORS from "../Errors"
 10+import {
 11+  checkArgumentsLength,
 12+  checkArgumentsAtLeastLength,
 13+  valueToNumber,
 14+  filterOutStringValues,
 15+  flatten,
 16+  filterOutNonNumberValues,
 17+  stringValuesToZeros,
 18+  firstValueAsNumber,
 19+  valueToBoolean,
 20+  checkArgumentsAtWithin,
 21+  CriteriaFunctionFactory,
 22+  valueCanCoerceToNumber
 23+} from "./Utils";
 24+import { CellError } from "../Errors";
 25+import * as ERRORS from "../Errors";
 26 
 27 /**
 28  * Returns the absolute value of a number.
 29@@ -1031,6 +1043,54 @@ var SUMIF = function (...values) {
 30   return sum;
 31 };
 32 
 33+/**
 34+ * Returns the sum of the squares of a series of numbers and/or cells.
 35+ * @param values  The values or range(s) whose squares to add together.
 36+ * @returns {number} the sum of the squares if the input.
 37+ * @constructor
 38+ */
 39+var SUMSQ = function (...values) {
 40+  checkArgumentsAtLeastLength(values, 1);
 41+  var result = 0;
 42+  for (var i = 0; i < values.length; i++) {
 43+    if (values[i] instanceof Array) {
 44+      if (values[i].length === 0) {
 45+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 46+      }
 47+      result = result + SUMSQ.apply(this, filterOutNonNumberValues(values[i]));
 48+    } else {
 49+      var n = valueToNumber(values[i]);
 50+      result = result + (n * n);
 51+    }
 52+  }
 53+  return result;
 54+};
 55+
 56+
 57+/**
 58+ * Truncates a number to a certain number of significant digits by omitting less significant digits.
 59+ * @param values[0] The value to be truncated.
 60+ * @param values[1]  [ OPTIONAL - 0 by default ] - The number of significant digits to the right of the decimal point to
 61+ * retain. If places is greater than the number of significant digits in value, value is returned without modification.
 62+ * places may be negative, in which case the specified number of digits to the left of the decimal place are changed to
 63+ * zero. All digits to the right of the decimal place are discarded. If all digits of value are changed to zero, TRUNC
 64+ * simply returns 0.
 65+ * @returns {number} after truncation
 66+ * @constructor
 67+ */
 68+var TRUNC = function (...values) : number {
 69+  checkArgumentsAtWithin(values, 1, 2);
 70+  var n = firstValueAsNumber(values[0]);
 71+  var digits = 0;
 72+  if (values.length === 2) {
 73+    digits = firstValueAsNumber(values[1]);
 74+  }
 75+  var sign = (n > 0) ? 1 : -1;
 76+  return sign * (Math.floor(Math.abs(n) * Math.pow(10, digits))) / Math.pow(10, digits);
 77+};
 78+
 79+
 80+
 81 export {
 82   ABS,
 83   ACOS,
 84@@ -1083,5 +1143,7 @@ export {
 85   COUNTA,
 86   COUNTIF,
 87   COUNTIFS,
 88-  CEILING
 89+  CEILING,
 90+  SUMSQ,
 91+  TRUNC
 92 }
 93\ No newline at end of file
 94diff --git a/src/RawFormulas/Misc.ts b/src/RawFormulas/Misc.ts
 95index e9fd1b8..adfefdd 100644
 96--- a/src/RawFormulas/Misc.ts
 97+++ b/src/RawFormulas/Misc.ts
 98@@ -1,4 +1,12 @@
 99-import { firstValueAsNumber, checkArgumentsLength, firstValueAsString } from "./Utils"
100+import {
101+  valueToString,
102+  firstValueAsNumber,
103+  firstValueAsString,
104+  firstValueAsBoolean,
105+  checkArgumentsLength,
106+  checkArgumentsAtWithin,
107+  checkArgumentsAtLeastLength
108+} from "./Utils";
109 import { CellError } from "../Errors"
110 import * as ERRORS from "../Errors"
111 
112@@ -32,7 +40,67 @@ var CODE = function (...values) : number {
113   return text.charCodeAt(0);
114 };
115 
116+/**
117+ * Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
118+ * @param values[0] text - The text to divide.
119+ * @param values[1] delimiter - The character or characters to use to split text.
120+ * @param values[2] split_by_each - [optional] Whether or not to divide text around each character contained in
121+ * delimiter.
122+ * @returns {Array<string>} containing the split
123+ * @constructor
124+ * TODO: At some point this needs to return a more complex type than Array. Needs to return a type that has a dimension.
125+ */
126+var SPLIT = function (...values) : Array<string> {
127+  checkArgumentsAtWithin(values, 2, 3);
128+  var text = firstValueAsString(values[0]);
129+  var delimiter = firstValueAsString(values[1]);
130+  var splitByEach = false;
131+  if (values.length === 3) {
132+    splitByEach = firstValueAsBoolean(values[2]);
133+  }
134+  if (splitByEach) {
135+    var result = [text];
136+    for (var i = 0; i < delimiter.length; i++) {
137+      var char = delimiter[i];
138+      var subResult = [];
139+      for (var x = 0; x < result.length; x++) {
140+        subResult = subResult.concat(result[x].split(char));
141+      }
142+      result = subResult;
143+    }
144+    return result.filter(function (val) {
145+      return val.trim() !== "";
146+    });
147+  } else {
148+    return text.split(delimiter);
149+  }
150+};
151+
152+/**
153+ * Appends strings to one another.
154+ * @param values to append to one another. Must contain at least one value
155+ * @returns {string} concatenated string
156+ * @constructor
157+ */
158+var CONCATENATE = function (...values) : string {
159+  checkArgumentsAtLeastLength(values, 1);
160+  var string = '';
161+  for (var i = 0; i < values.length; i++) {
162+    if (values[i] instanceof Array) {
163+      if (values[i].length === 0) {
164+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
165+      }
166+      string += CONCATENATE.apply(this, arguments[i]);
167+    } else {
168+      string += valueToString(values[i]);
169+    }
170+  }
171+  return string;
172+};
173+
174 export {
175   CHAR,
176-  CODE
177+  CODE,
178+  SPLIT,
179+  CONCATENATE
180 }
181\ No newline at end of file
182diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
183index 2240129..8b79422 100644
184--- a/src/RawFormulas/RawFormulas.ts
185+++ b/src/RawFormulas/RawFormulas.ts
186@@ -53,7 +53,9 @@ import {
187   COUNTA,
188   COUNTIF,
189   COUNTIFS,
190-  CEILING
191+  CEILING,
192+  SUMSQ,
193+  TRUNC
194 } from "./Math";
195 import {
196   AND,
197@@ -66,7 +68,9 @@ import {
198 } from "./Logical";
199 import {
200   CHAR,
201-  CODE
202+  CODE,
203+  SPLIT,
204+  CONCATENATE
205 } from "./Misc";
206 import {
207   checkArgumentsAtLeastLength,
208@@ -142,112 +146,6 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
209 var YEARFRAC = Formula["YEARFRAC"];
210 
211 
212-/**
213- * Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
214- * @param values[0] text - The text to divide.
215- * @param values[1] delimiter - The character or characters to use to split text.
216- * @param values[2] split_by_each - [optional] Whether or not to divide text around each character contained in
217- * delimiter.
218- * @returns {Array<string>} containing the split
219- * @constructor
220- * TODO: At some point this needs to return a more complex type than Array. Needs to return a type that has a dimension.
221- */
222-var SPLIT = function (...values) : Array<string> {
223-  checkArgumentsAtWithin(values, 2, 3);
224-  var text = firstValueAsString(values[0]);
225-  var delimiter = firstValueAsString(values[1]);
226-  var splitByEach = false;
227-  if (values.length === 3) {
228-    splitByEach = firstValueAsBoolean(values[2]);
229-  }
230-  if (splitByEach) {
231-    var result = [text];
232-    for (var i = 0; i < delimiter.length; i++) {
233-      var char = delimiter[i];
234-      var subResult = [];
235-      for (var x = 0; x < result.length; x++) {
236-        subResult = subResult.concat(result[x].split(char));
237-      }
238-      result = subResult;
239-    }
240-    return result.filter(function (val) {
241-      return val.trim() !== "";
242-    });
243-  } else {
244-    return text.split(delimiter);
245-  }
246-};
247-
248-
249-/**
250- * Returns the sum of the squares of a series of numbers and/or cells.
251- * @param values  The values or range(s) whose squares to add together.
252- * @returns {number} the sum of the squares if the input.
253- * @constructor
254- */
255-var SUMSQ = function (...values) {
256-  checkArgumentsAtLeastLength(values, 1);
257-  var result = 0;
258-  for (var i = 0; i < values.length; i++) {
259-    if (values[i] instanceof Array) {
260-      if (values[i].length === 0) {
261-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
262-      }
263-      result = result + SUMSQ.apply(this, filterOutNonNumberValues(values[i]));
264-    } else {
265-      var n = valueToNumber(values[i]);
266-      result = result + (n * n);
267-    }
268-  }
269-  return result;
270-};
271-
272-
273-/**
274- * Appends strings to one another.
275- * @param values to append to one another. Must contain at least one value
276- * @returns {string} concatenated string
277- * @constructor
278- */
279-var CONCATENATE = function (...values) : string {
280-  checkArgumentsAtLeastLength(values, 1);
281-  var string = '';
282-  for (var i = 0; i < values.length; i++) {
283-    if (values[i] instanceof Array) {
284-      if (values[i].length === 0) {
285-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
286-      }
287-      string += CONCATENATE.apply(this, arguments[i]);
288-    } else {
289-      string += valueToString(values[i]);
290-    }
291-  }
292-  return string;
293-};
294-
295-/**
296- * Truncates a number to a certain number of significant digits by omitting less significant digits.
297- * @param values[0] The value to be truncated.
298- * @param values[1]  [ OPTIONAL - 0 by default ] - The number of significant digits to the right of the decimal point to
299- * retain. If places is greater than the number of significant digits in value, value is returned without modification.
300- * places may be negative, in which case the specified number of digits to the left of the decimal place are changed to
301- * zero. All digits to the right of the decimal place are discarded. If all digits of value are changed to zero, TRUNC
302- * simply returns 0.
303- * @returns {number} after truncation
304- * @constructor
305- */
306-var TRUNC = function (...values) : number {
307-  checkArgumentsAtWithin(values, 1, 2);
308-  var n = firstValueAsNumber(values[0]);
309-  var digits = 0;
310-  if (values.length === 2) {
311-    digits = firstValueAsNumber(values[1]);
312-  }
313-  var sign = (n > 0) ? 1 : -1;
314-  return sign * (Math.floor(Math.abs(n) * Math.pow(10, digits))) / Math.pow(10, digits);
315-};
316-
317-
318 export {
319   __COMPLEX,
320