spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[CHOOSE] fixing small error, enabling test
author
Ben Vogt <[email protected]>
date
2017-08-12 19:57:21
stats
4 file(s) changed, 19 insertions(+), 16 deletions(-)
files
DOCS.md
dist/Formulas/Lookup.js
src/Formulas/Lookup.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index e7b77cf..50bf791 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -787,7 +787,7 @@
  6   Returns an element from a list of choices based on index. 
  7 @param index - Which choice to return. 
  8 @param values -  Array of potential value to return. Required. May be a reference to a cell or an individual value. 
  9-@constructor TODO: This does not currently work with the parser. See TODO.md for more information.
 10+@constructor
 11 ```
 12 ## Math
 13 
 14diff --git a/dist/Formulas/Lookup.js b/dist/Formulas/Lookup.js
 15index ee3cfb9..50d095c 100644
 16--- a/dist/Formulas/Lookup.js
 17+++ b/dist/Formulas/Lookup.js
 18@@ -3,24 +3,25 @@ exports.__esModule = true;
 19 var ArgsChecker_1 = require("../Utilities/ArgsChecker");
 20 var Errors_1 = require("../Errors");
 21 var TypeConverter_1 = require("../Utilities/TypeConverter");
 22+var Filter_1 = require("../Utilities/Filter");
 23 /**
 24  * Returns an element from a list of choices based on index.
 25  * @param index - Which choice to return.
 26  * @param values -  Array of potential value to return. Required. May be a reference to a cell or an individual value.
 27  * @constructor
 28- * TODO: This does not currently work with the parser. See TODO.md for more information.
 29  */
 30 var CHOOSE = function (index) {
 31     var values = [];
 32     for (var _i = 1; _i < arguments.length; _i++) {
 33         values[_i - 1] = arguments[_i];
 34     }
 35-    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 2, "NA");
 36+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 2, "CHOOSE");
 37     var i = Math.floor(TypeConverter_1.TypeConverter.firstValueAsNumber(index));
 38-    if (i < 1 || i > values.length) {
 39+    var data = Filter_1.Filter.flattenAndThrow(values);
 40+    if (i < 1 || i > data.length) {
 41         throw new Errors_1.NumError("Function CHOOSE parameter 1 value is " + i + ". Valid values are between 1 and "
 42-            + (values.length) + " inclusive.");
 43+            + (data.length) + " inclusive.");
 44     }
 45-    return values[i - 1];
 46+    return data[i - 1];
 47 };
 48 exports.CHOOSE = CHOOSE;
 49diff --git a/src/Formulas/Lookup.ts b/src/Formulas/Lookup.ts
 50index 683ed9e..bfda929 100644
 51--- a/src/Formulas/Lookup.ts
 52+++ b/src/Formulas/Lookup.ts
 53@@ -7,6 +7,9 @@ import {
 54 import {
 55   TypeConverter
 56 } from "../Utilities/TypeConverter";
 57+import {
 58+  Filter
 59+} from "../Utilities/Filter";
 60 
 61 
 62 /**
 63@@ -14,16 +17,16 @@ import {
 64  * @param index - Which choice to return.
 65  * @param values -  Array of potential value to return. Required. May be a reference to a cell or an individual value.
 66  * @constructor
 67- * TODO: This does not currently work with the parser. See TODO.md for more information.
 68  */
 69 var CHOOSE = function (index, ...values) {
 70-  ArgsChecker.checkAtLeastLength(arguments, 2, "NA");
 71+  ArgsChecker.checkAtLeastLength(arguments, 2, "CHOOSE");
 72   var i = Math.floor(TypeConverter.firstValueAsNumber(index));
 73-  if (i < 1 || i > values.length) {
 74+  var data = Filter.flattenAndThrow(values);
 75+  if (i < 1 || i > data.length) {
 76     throw new NumError("Function CHOOSE parameter 1 value is " + i + ". Valid values are between 1 and "
 77-        + (values.length) + " inclusive.");
 78+        + (data.length) + " inclusive.");
 79   }
 80-  return values[i-1];
 81+  return data[i-1];
 82 };
 83 
 84 
 85diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
 86index f85b935..5ce48f0 100644
 87--- a/tests/SheetFormulaTest.ts
 88+++ b/tests/SheetFormulaTest.ts
 89@@ -163,10 +163,9 @@ test("Sheet CORREL", function(){
 90   assertFormulaEquals('=CORREL([9, 5],[10, 4])', 1);
 91 });
 92 
 93-// TODO: Formula will not parse because lexer does not expect array values to occur after a comma
 94-// test("Sheet CHOOSE", function(){
 95-//   assertFormulaEquals('=CHOOSE(2, [1, 2, 3])', 2);
 96-// });
 97+test("Sheet CHOOSE", function(){
 98+  assertFormulaEquals('=CHOOSE([2], [1, 2, 3])', 2);
 99+});
100 
101 test("Sheet COS", function(){
102   assertFormulaEquals("=COS(PI())", -1);