spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.CODE written and tested.
author
Ben Vogt <[email protected]>
date
2017-02-01 23:33:38
stats
3 file(s) changed, 51 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 b0ddbf8..334d3a6 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -32,7 +32,7 @@ import {
  6   TRUE,
  7   NOT
  8 } from "./Logical"
  9-import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber, checkArgumentsLength, firstValueAsNumber} from "./Utils";
 10+import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber, checkArgumentsLength, firstValueAsNumber, firstValueAsString} from "./Utils";
 11 import { CellError } from "../Errors"
 12 import * as ERRORS from "../Errors"
 13 
 14@@ -58,7 +58,18 @@ var CEILING = Formula["CEILING"];
 15 var CEILINGMATH = Formula["CEILINGMATH"];
 16 var CEILINGPRECISE = Formula["CEILINGPRECISE"];
 17 var CHAR = Formula["CHAR"];
 18-var CODE = Formula["CODE"];
 19+
 20+/**
 21+ * Returns the numeric Unicode map value of the first character in the string provided.
 22+ * @param values[0] The string whose first character's Unicode map value will be returned.
 23+ * @returns {number} number of the first character's Unicode value
 24+ * @constructor
 25+ */
 26+var CODE = function (...values) : number {
 27+  checkArgumentsLength(values, 1);
 28+  var text = firstValueAsString(values[0]);
 29+  return text.charCodeAt(0);
 30+};
 31 var COMBIN = Formula["COMBIN"];
 32 var COMBINA = Formula["COMBINA"];
 33 var COMPLEX = Formula["COMPLEX"];
 34diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 35index e973a69..aa3768d 100644
 36--- a/src/RawFormulas/Utils.ts
 37+++ b/src/RawFormulas/Utils.ts
 38@@ -51,7 +51,7 @@ function valueToString(value: any) : string {
 39   } else if (typeof value === "boolean") {
 40     return value ? "TRUE" : "FALSE";
 41   } else if (value instanceof Array) {
 42-    return valueToString(value[0]);
 43+    return valueToString(value[0]); // TODO: Take this out. It's stupid. We should handle arrays at a different level.
 44   }
 45 }
 46 
 47@@ -70,6 +70,21 @@ function firstValueAsNumber(input: any) : number {
 48   return valueToNumber(input);
 49 }
 50 
 51+/**
 52+ * Takes any input type and will throw a REF_ERROR or coerce it into a string.
 53+ * @param input to attempt to coerce into a string
 54+ * @returns {number} number representation of the input
 55+ */
 56+function firstValueAsString(input: any) : string {
 57+  if (input instanceof Array) {
 58+    if (input.length === 0) {
 59+      throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 60+    }
 61+    return firstValueAsString(input[0]);
 62+  }
 63+  return valueToString(input);
 64+}
 65+
 66 
 67 /**
 68  * Converts any value to a number or throws an error if it cannot coerce it to the number type
 69@@ -138,6 +153,7 @@ export {
 70   valueToNumber,
 71   valueToString,
 72   firstValueAsNumber,
 73+  firstValueAsString,
 74   filterOutStringValues,
 75   checkArgumentsAtLeastLength,
 76   checkArgumentsLength
 77diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 78index 1489069..70db752 100644
 79--- a/tests/FormulasTest.ts
 80+++ b/tests/FormulasTest.ts
 81@@ -296,7 +296,26 @@ assertEquals(CEILINGPRECISE(1001.112131), 1002);
 82 
 83 assertEquals(CHAR(97), "a");
 84 
 85+
 86+// Test CODE
 87 assertEquals(CODE('a'), 97);
 88+assertEquals(CODE('aa'), 97);
 89+assertEquals(CODE('aM'), 97);
 90+assertEquals(CODE('#'), 35);
 91+assertEquals(CODE(false), 70);
 92+assertEquals(CODE(true), 84);
 93+catchAndAssertEquals(function() {
 94+  CODE();
 95+}, ERRORS.NA_ERROR);
 96+catchAndAssertEquals(function() {
 97+  CODE("a", "m");
 98+}, ERRORS.NA_ERROR);
 99+catchAndAssertEquals(function() {
100+  CODE("");
101+}, ERRORS.VALUE_ERROR);
102+assertEquals(CODE(['a']), 97);
103+assertEquals(CODE([['a'], 'p']), 97);
104+
105 
106 assertEquals(COMBIN(4, 2), 6);
107