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 functions to Text.ts and Engineering.ts
author
Ben Vogt <[email protected]>
date
2017-02-20 22:38:52
stats
4 file(s) changed, 148 insertions(+), 135 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/Misc.ts
src/RawFormulas/Engineering.ts
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Text.ts
  1diff --git a/src/RawFormulas/Misc.ts b/src/RawFormulas/Engineering.ts
  2similarity index 76%
  3rename from src/RawFormulas/Misc.ts
  4rename to src/RawFormulas/Engineering.ts
  5index 4a0b9f5..5da4703 100644
  6--- a/src/RawFormulas/Misc.ts
  7+++ b/src/RawFormulas/Engineering.ts
  8@@ -5,94 +5,6 @@ import {
  9 import { CellError } from "../Errors"
 10 import * as ERRORS from "../Errors"
 11 
 12-/**
 13- * Convert a number into a character according to the current Unicode table.
 14- * @param values[0] The number of the character to look up from the current Unicode table in decimal format.
 15- * @returns {string} character corresponding to Unicode number
 16- * @constructor
 17- */
 18-var CHAR = function (...values) : string {
 19-  ArgsChecker.checkLength(values, 1);
 20-  var n = TypeCaster.firstValueAsNumber(values[0]);
 21-  if (n < 1 || n > 1114112) { //limit
 22-    throw new CellError(ERRORS.NUM_ERROR, "Function CHAR parameter 1 value " + n + " is out of range.");
 23-  }
 24-  return String.fromCharCode(n);
 25-};
 26-
 27-/**
 28- * Returns the numeric Unicode map value of the first character in the string provided.
 29- * @param values[0] The string whose first character's Unicode map value will be returned.
 30- * @returns {number} number of the first character's Unicode value
 31- * @constructor
 32- */
 33-var CODE = function (...values) : number {
 34-  ArgsChecker.checkLength(values, 1);
 35-  var text = TypeCaster.firstValueAsString(values[0]);
 36-  if (text === "") {
 37-    throw new CellError(ERRORS.VALUE_ERROR, "Function CODE parameter 1 value should be non-empty.");
 38-  }
 39-  return text.charCodeAt(0);
 40-};
 41-
 42-/**
 43- * Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
 44- * @param values[0] text - The text to divide.
 45- * @param values[1] delimiter - The character or characters to use to split text.
 46- * @param values[2] split_by_each - [optional] Whether or not to divide text around each character contained in
 47- * delimiter.
 48- * @returns {Array<string>} containing the split
 49- * @constructor
 50- * TODO: At some point this needs to return a more complex type than Array. Needs to return a type that has a dimension.
 51- */
 52-var SPLIT = function (...values) : Array<string> {
 53-  ArgsChecker.checkLengthWithin(values, 2, 3);
 54-  var text = TypeCaster.firstValueAsString(values[0]);
 55-  var delimiter = TypeCaster.firstValueAsString(values[1]);
 56-  var splitByEach = false;
 57-  if (values.length === 3) {
 58-    splitByEach = TypeCaster.firstValueAsBoolean(values[2]);
 59-  }
 60-  if (splitByEach) {
 61-    var result = [text];
 62-    for (var i = 0; i < delimiter.length; i++) {
 63-      var char = delimiter[i];
 64-      var subResult = [];
 65-      for (var x = 0; x < result.length; x++) {
 66-        subResult = subResult.concat(result[x].split(char));
 67-      }
 68-      result = subResult;
 69-    }
 70-    return result.filter(function (val) {
 71-      return val.trim() !== "";
 72-    });
 73-  } else {
 74-    return text.split(delimiter);
 75-  }
 76-};
 77-
 78-/**
 79- * Appends strings to one another.
 80- * @param values to append to one another. Must contain at least one value
 81- * @returns {string} concatenated string
 82- * @constructor
 83- */
 84-var CONCATENATE = function (...values) : string {
 85-  ArgsChecker.checkAtLeastLength(values, 1);
 86-  var string = '';
 87-  for (var i = 0; i < values.length; i++) {
 88-    if (values[i] instanceof Array) {
 89-      if (values[i].length === 0) {
 90-        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 91-      }
 92-      string += CONCATENATE.apply(this, arguments[i]);
 93-    } else {
 94-      string += TypeCaster.valueToString(values[i]);
 95-    }
 96-  }
 97-  return string;
 98-};
 99-
100 /**
101  * Converts a signed binary number to decimal format.
102  * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to decimal, provided as a
103@@ -377,14 +289,10 @@ var DEC2BIN = function (...values) : string {
104 
105 
106 export {
107-  CHAR, // Text
108-  CODE, // Text
109-  SPLIT, // Text
110-  CONCATENATE, // Text
111-  BIN2DEC, // Engineering
112-  BIN2HEX, // Engineering
113-  BIN2OCT, // Engineering
114-  DEC2BIN, // Engineering
115-  DEC2HEX, // Engineering
116-  DEC2OCT // Engineering
117+  BIN2DEC,
118+  BIN2HEX,
119+  BIN2OCT,
120+  DEC2BIN,
121+  DEC2HEX,
122+  DEC2OCT
123 }
124\ No newline at end of file
125diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
126index ffdfcff..f2342aa 100644
127--- a/src/RawFormulas/Math.ts
128+++ b/src/RawFormulas/Math.ts
129@@ -69,36 +69,6 @@ var ACOTH = function (value?) {
130   return 0.5 * Math.log((value + 1) / (value - 1));
131 };
132 
133-/**
134- * Computes the value of a Roman numeral.
135- * @param text The Roman numeral to format, whose value must be between 1 and 3999, inclusive.
136- * @returns {number} value in integer format
137- * @constructor
138- */
139-var ARABIC = function (text?) {
140-  ArgsChecker.checkLength(arguments, 1);
141-  if (typeof text !== "string") {
142-    throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
143-  }
144-  var negative = false;
145-  if (text[0] === "-") {
146-    negative = true;
147-    text = text.substr(1);
148-  }
149-  // Credits: Rafa? Kukawski
150-  if (!/^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/.test(text)) {
151-    throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
152-  }
153-  var r = 0;
154-  text.replace(/[MDLV]|C[MD]?|X[CL]?|I[XV]?/g, function (i) {
155-    r += {M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1}[i];
156-  });
157-  if (negative) {
158-    return r * -1;
159-  }
160-  return r;
161-};
162-
163 /**
164  * Returns the inverse sine of a value, in radians.
165  * @param value The value for which to calculate the inverse sine. Must be between -1 and 1, inclusive.
166@@ -1744,7 +1714,6 @@ export {
167   ACOS,
168   ACOSH,
169   ACOTH,
170-  ARABIC, // Text
171   ASIN,
172   ASINH,
173   ATAN,
174diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
175index 1b18a91..5a57644 100644
176--- a/src/RawFormulas/RawFormulas.ts
177+++ b/src/RawFormulas/RawFormulas.ts
178@@ -6,7 +6,6 @@ import {
179   ACOS,
180   ACOSH,
181   ACOTH,
182-  ARABIC,
183   ASIN,
184   ASINH,
185   ATAN,
186@@ -82,17 +81,13 @@ import {
187   XOR
188 } from "./Logical";
189 import {
190-  CHAR,
191-  CODE,
192-  SPLIT,
193-  CONCATENATE,
194   BIN2DEC,
195   BIN2HEX,
196   BIN2OCT,
197   DEC2BIN,
198   DEC2HEX,
199   DEC2OCT
200-} from "./Misc";
201+} from "./Engineering";
202 import {
203   DOLLAR,
204   DOLLARDE,
205@@ -106,6 +101,13 @@ import {
206   PEARSON,
207   MEDIAN
208 } from "./Statistical";
209+import {
210+  ARABIC,
211+  CHAR,
212+  CODE,
213+  SPLIT,
214+  CONCATENATE
215+} from "./Text"
216 
217 import {
218   CriteriaFunctionFactory,
219diff --git a/src/RawFormulas/Text.ts b/src/RawFormulas/Text.ts
220new file mode 100644
221index 0000000..e18bd8a
222--- /dev/null
223+++ b/src/RawFormulas/Text.ts
224@@ -0,0 +1,134 @@
225+import {
226+  ArgsChecker,
227+  TypeCaster
228+} from "./Utils";
229+import {
230+  CellError
231+} from "../Errors";
232+import * as ERRORS from "../Errors";
233+
234+/**
235+ * Computes the value of a Roman numeral.
236+ * @param text The Roman numeral to format, whose value must be between 1 and 3999, inclusive.
237+ * @returns {number} value in integer format
238+ * @constructor
239+ */
240+var ARABIC = function (text?) {
241+  ArgsChecker.checkLength(arguments, 1);
242+  if (typeof text !== "string") {
243+    throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
244+  }
245+  var negative = false;
246+  if (text[0] === "-") {
247+    negative = true;
248+    text = text.substr(1);
249+  }
250+  // Credits: Rafa? Kukawski
251+  if (!/^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/.test(text)) {
252+    throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
253+  }
254+  var r = 0;
255+  text.replace(/[MDLV]|C[MD]?|X[CL]?|I[XV]?/g, function (i) {
256+    r += {M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1}[i];
257+  });
258+  if (negative) {
259+    return r * -1;
260+  }
261+  return r;
262+};
263+
264+/**
265+ * Convert a number into a character according to the current Unicode table.
266+ * @param values[0] The number of the character to look up from the current Unicode table in decimal format.
267+ * @returns {string} character corresponding to Unicode number
268+ * @constructor
269+ */
270+var CHAR = function (...values) : string {
271+  ArgsChecker.checkLength(values, 1);
272+  var n = TypeCaster.firstValueAsNumber(values[0]);
273+  if (n < 1 || n > 1114112) { //limit
274+    throw new CellError(ERRORS.NUM_ERROR, "Function CHAR parameter 1 value " + n + " is out of range.");
275+  }
276+  return String.fromCharCode(n);
277+};
278+
279+/**
280+ * Returns the numeric Unicode map value of the first character in the string provided.
281+ * @param values[0] The string whose first character's Unicode map value will be returned.
282+ * @returns {number} number of the first character's Unicode value
283+ * @constructor
284+ */
285+var CODE = function (...values) : number {
286+  ArgsChecker.checkLength(values, 1);
287+  var text = TypeCaster.firstValueAsString(values[0]);
288+  if (text === "") {
289+    throw new CellError(ERRORS.VALUE_ERROR, "Function CODE parameter 1 value should be non-empty.");
290+  }
291+  return text.charCodeAt(0);
292+};
293+
294+/**
295+ * Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
296+ * @param values[0] text - The text to divide.
297+ * @param values[1] delimiter - The character or characters to use to split text.
298+ * @param values[2] split_by_each - [optional] Whether or not to divide text around each character contained in
299+ * delimiter.
300+ * @returns {Array<string>} containing the split
301+ * @constructor
302+ * TODO: At some point this needs to return a more complex type than Array. Needs to return a type that has a dimension.
303+ */
304+var SPLIT = function (...values) : Array<string> {
305+  ArgsChecker.checkLengthWithin(values, 2, 3);
306+  var text = TypeCaster.firstValueAsString(values[0]);
307+  var delimiter = TypeCaster.firstValueAsString(values[1]);
308+  var splitByEach = false;
309+  if (values.length === 3) {
310+    splitByEach = TypeCaster.firstValueAsBoolean(values[2]);
311+  }
312+  if (splitByEach) {
313+    var result = [text];
314+    for (var i = 0; i < delimiter.length; i++) {
315+      var char = delimiter[i];
316+      var subResult = [];
317+      for (var x = 0; x < result.length; x++) {
318+        subResult = subResult.concat(result[x].split(char));
319+      }
320+      result = subResult;
321+    }
322+    return result.filter(function (val) {
323+      return val.trim() !== "";
324+    });
325+  } else {
326+    return text.split(delimiter);
327+  }
328+};
329+
330+/**
331+ * Appends strings to one another.
332+ * @param values to append to one another. Must contain at least one value
333+ * @returns {string} concatenated string
334+ * @constructor
335+ */
336+var CONCATENATE = function (...values) : string {
337+  ArgsChecker.checkAtLeastLength(values, 1);
338+  var string = '';
339+  for (var i = 0; i < values.length; i++) {
340+    if (values[i] instanceof Array) {
341+      if (values[i].length === 0) {
342+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
343+      }
344+      string += CONCATENATE.apply(this, arguments[i]);
345+    } else {
346+      string += TypeCaster.valueToString(values[i]);
347+    }
348+  }
349+  return string;
350+};
351+
352+export {
353+  ARABIC,
354+  CHAR,
355+  CODE,
356+  SPLIT,
357+  CONCATENATE
358+}
359\ No newline at end of file