spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.SPLIT
author
Ben Vogt <[email protected]>
date
2017-02-16 02:52:43
stats
3 file(s) changed, 72 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index 3c7decf..2240129 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -72,6 +72,7 @@ import {
  6   checkArgumentsAtLeastLength,
  7   checkArgumentsAtWithin,
  8   valueCanCoerceToNumber,
  9+  firstValueAsBoolean,
 10   filterOutStringValues,
 11   CriteriaFunctionFactory,
 12   valueToNumber,
 13@@ -134,7 +135,6 @@ var __COMPLEX = {
 14 };
 15 var FISHER = Formula["FISHER"];
 16 var FISHERINV = Formula["FISHERINV"];
 17-var SPLIT = Formula["SPLIT"];
 18 var SQRTPI = Formula["SQRTPI"];
 19 var SUMPRODUCT = Formula["SUMPRODUCT"];
 20 var SUMX2MY2 = Formula["SUMX2MY2"];
 21@@ -142,6 +142,43 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
 22 var YEARFRAC = Formula["YEARFRAC"];
 23 
 24 
 25+/**
 26+ * Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
 27+ * @param values[0] text - The text to divide.
 28+ * @param values[1] delimiter - The character or characters to use to split text.
 29+ * @param values[2] split_by_each - [optional] Whether or not to divide text around each character contained in
 30+ * delimiter.
 31+ * @returns {Array<string>} containing the split
 32+ * @constructor
 33+ * TODO: At some point this needs to return a more complex type than Array. Needs to return a type that has a dimension.
 34+ */
 35+var SPLIT = function (...values) : Array<string> {
 36+  checkArgumentsAtWithin(values, 2, 3);
 37+  var text = firstValueAsString(values[0]);
 38+  var delimiter = firstValueAsString(values[1]);
 39+  var splitByEach = false;
 40+  if (values.length === 3) {
 41+    splitByEach = firstValueAsBoolean(values[2]);
 42+  }
 43+  if (splitByEach) {
 44+    var result = [text];
 45+    for (var i = 0; i < delimiter.length; i++) {
 46+      var char = delimiter[i];
 47+      var subResult = [];
 48+      for (var x = 0; x < result.length; x++) {
 49+        subResult = subResult.concat(result[x].split(char));
 50+      }
 51+      result = subResult;
 52+    }
 53+    return result.filter(function (val) {
 54+      return val.trim() !== "";
 55+    });
 56+  } else {
 57+    return text.split(delimiter);
 58+  }
 59+};
 60+
 61+
 62 /**
 63  * Returns the sum of the squares of a series of numbers and/or cells.
 64  * @param values  The values or range(s) whose squares to add together.
 65diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 66index c8956fc..f15cbc6 100644
 67--- a/src/RawFormulas/Utils.ts
 68+++ b/src/RawFormulas/Utils.ts
 69@@ -107,6 +107,20 @@ function firstValueAsString(input: any) : string {
 70   return valueToString(input);
 71 }
 72 
 73+/**
 74+ * Takes any input type and will throw a REF_ERROR or coerce it into a string.
 75+ * @param input to attempt to coerce into a string
 76+ * @returns {number} number representation of the input
 77+ */
 78+function firstValueAsBoolean(input: any) : boolean {
 79+  if (input instanceof Array) {
 80+    if (input.length === 0) {
 81+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 82+    }
 83+    return firstValueAsBoolean(input[0]);
 84+  }
 85+  return valueToBoolean(input);
 86+}
 87 
 88 /**
 89  * Converts any value to a number or throws an error if it cannot coerce it to the number type
 90@@ -287,6 +301,7 @@ class CriteriaFunctionFactory {
 91 
 92 export {
 93   stringValuesToZeros,
 94+  firstValueAsBoolean,
 95   filterOutNonNumberValues,
 96   flatten,
 97   valueCanCoerceToNumber,
 98diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 99index da32c19..dec3d88 100644
100--- a/tests/FormulasTest.ts
101+++ b/tests/FormulasTest.ts
102@@ -1184,7 +1184,19 @@ catchAndAssertEquals(function() {
103 assertEquals(SINH([[10, "str"]]), 11013.232874703393);
104 
105 
106-assertArrayEquals(SPLIT("1,2,3", ",", true), [ '1', '2', '3' ]);
107+// Test SPLIT
108+assertArrayEquals(SPLIT("1,2,3", ","), ['1', '2', '3']);
109+assertArrayEquals(SPLIT("little kitty cat", "i"), ['l', 'ttle k', 'tty cat']);
110+assertArrayEquals(SPLIT("father sister berzerker", "er", true), ['fath', ' sist', ' b', 'z', 'k']);
111+assertArrayEquals(SPLIT("father sister berzerker", "er", [true]), ['fath', ' sist', ' b', 'z', 'k']);
112+assertArrayEquals(SPLIT("father  sister   berzerker", "er", true), ['fath', '  sist', '   b', 'z', 'k']);
113+assertArrayEquals(SPLIT(["father sister berzerker"], ["er"], true), ['fath', ' sist', ' b', 'z', 'k']);
114+catchAndAssertEquals(function() {
115+  SPLIT([], "er");
116+}, ERRORS.REF_ERROR);
117+catchAndAssertEquals(function() {
118+  SPLIT("er", "er", true, 10);
119+}, ERRORS.NA_ERROR);
120 
121 
122 // Test SQRT