spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[LARGE] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-25 17:10:07
stats
8 file(s) changed, 98 insertions(+), 4 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Statistical.js
src/Formulas/AllFormulas.ts
src/Formulas/Statistical.ts
tests/Formulas/StatisticalTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 5b81072..ad837e3 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1536,6 +1536,15 @@
  6 @param n - N in 'Nth'. 
  7 @constructor
  8 ```
  9+
 10+### LARGE 
 11+
 12+```
 13+  Returns the Nth largest value in the range, ignoring text values. 
 14+@param range -  Range or data-set to consider. 
 15+@param n - N in 'Nth'. 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index 686a62a..ebb86f6 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -70,7 +70,6 @@ For example 64 tbs to a qt.
 26 * HYPGEOMDIST
 27 * INTERCEPT
 28 * KURT
 29-* LARGE
 30 * LOGINV
 31 * LOGNORMDIST
 32 * MODE
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 5b424e2..953b403 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -139,6 +139,7 @@ exports.TRIMMEAN = Statistical_1.TRIMMEAN;
 38 exports.SLOPE = Statistical_1.SLOPE;
 39 exports.STANDARDIZE = Statistical_1.STANDARDIZE;
 40 exports.SMALL = Statistical_1.SMALL;
 41+exports.LARGE = Statistical_1.LARGE;
 42 var Text_1 = require("./Text");
 43 exports.ARABIC = Text_1.ARABIC;
 44 exports.CHAR = Text_1.CHAR;
 45diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 46index ad69cdf..bc9ef79 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -768,6 +768,7 @@ exports.SLOPE = SLOPE;
 50  * @constructor
 51  */
 52 var STANDARDIZE = function (value, meanValue, std) {
 53+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 3, "STANDARDIZE");
 54     value = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
 55     meanValue = TypeConverter_1.TypeConverter.firstValueAsNumber(meanValue);
 56     std = TypeConverter_1.TypeConverter.firstValueAsNumber(std);
 57@@ -784,6 +785,7 @@ exports.STANDARDIZE = STANDARDIZE;
 58  * @constructor
 59  */
 60 var SMALL = function (range, n) {
 61+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "SMALL");
 62     var data = Filter_1.Filter.flattenAndThrow(range).filter(function (value) {
 63         return typeof value != "string";
 64     }).map(function (value) {
 65@@ -797,3 +799,24 @@ var SMALL = function (range, n) {
 66     return data[n - 1];
 67 };
 68 exports.SMALL = SMALL;
 69+/**
 70+ * Returns the Nth largest value in the range, ignoring text values.
 71+ * @param range -  Range or data-set to consider.
 72+ * @param n - N in 'Nth'.
 73+ * @constructor
 74+ */
 75+var LARGE = function (range, n) {
 76+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "LARGE");
 77+    var data = Filter_1.Filter.flattenAndThrow(range).filter(function (value) {
 78+        return typeof value != "string";
 79+    }).map(function (value) {
 80+        return TypeConverter_1.TypeConverter.valueToNumber(value);
 81+    }).sort(function (a, b) {
 82+        return b - a;
 83+    });
 84+    if (n > data.length || n < 1) {
 85+        throw new Errors_1.NumError("Function LARGE parameter 2 value " + n + " is out of range.");
 86+    }
 87+    return data[n - 1];
 88+};
 89+exports.LARGE = LARGE;
 90diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 91index d8e1399..be65348 100644
 92--- a/src/Formulas/AllFormulas.ts
 93+++ b/src/Formulas/AllFormulas.ts
 94@@ -144,7 +144,8 @@ import {
 95   TRIMMEAN,
 96   SLOPE,
 97   STANDARDIZE,
 98-  SMALL
 99+  SMALL,
100+  LARGE
101 } from "./Statistical";
102 import {
103   ARABIC,
104@@ -357,5 +358,6 @@ export {
105   LOWER,
106   UPPER,
107   STANDARDIZE,
108-  SMALL
109+  SMALL,
110+  LARGE
111 }
112\ No newline at end of file
113diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
114index f923c01..2ee9500 100644
115--- a/src/Formulas/Statistical.ts
116+++ b/src/Formulas/Statistical.ts
117@@ -749,6 +749,7 @@ var SLOPE = function (rangeY, rangeX) {
118  * @constructor
119  */
120 var STANDARDIZE = function (value, meanValue, std) {
121+  ArgsChecker.checkLength(arguments, 3, "STANDARDIZE");
122   value = TypeConverter.firstValueAsNumber(value);
123   meanValue = TypeConverter.firstValueAsNumber(meanValue);
124   std = TypeConverter.firstValueAsNumber(std);
125@@ -766,6 +767,7 @@ var STANDARDIZE = function (value, meanValue, std) {
126  * @constructor
127  */
128 var SMALL =  function (range, n) {
129+  ArgsChecker.checkLength(arguments, 2, "SMALL");
130   var data = Filter.flattenAndThrow(range).filter(function (value) {
131     return typeof value != "string";
132   }).map(function (value) {
133@@ -780,6 +782,28 @@ var SMALL =  function (range, n) {
134 };
135 
136 
137+/**
138+ * Returns the Nth largest value in the range, ignoring text values.
139+ * @param range -  Range or data-set to consider.
140+ * @param n - N in 'Nth'.
141+ * @constructor
142+ */
143+var LARGE =  function (range, n) {
144+  ArgsChecker.checkLength(arguments, 2, "LARGE");
145+  var data = Filter.flattenAndThrow(range).filter(function (value) {
146+    return typeof value != "string";
147+  }).map(function (value) {
148+    return TypeConverter.valueToNumber(value);
149+  }).sort(function (a, b) {
150+    return b - a;
151+  });
152+  if (n > data.length || n < 1) {
153+    throw new NumError("Function LARGE parameter 2 value " + n + " is out of range.");
154+  }
155+  return data[n - 1];
156+};
157+
158+
159 export {
160   AVERAGE,
161   AVERAGEA,
162@@ -809,5 +833,6 @@ export {
163   TRIMMEAN,
164   SLOPE,
165   STANDARDIZE,
166-  SMALL
167+  SMALL,
168+  LARGE
169 }
170\ No newline at end of file
171diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
172index a6f37b5..f05a438 100644
173--- a/tests/Formulas/StatisticalTest.ts
174+++ b/tests/Formulas/StatisticalTest.ts
175@@ -27,7 +27,8 @@ import {
176   TRIMMEAN,
177   SLOPE,
178   STANDARDIZE,
179-  SMALL
180+  SMALL,
181+  LARGE
182 } from "../../src/Formulas/Statistical";
183 import * as ERRORS from "../../src/Errors";
184 import {
185@@ -602,6 +603,12 @@ test("STANDARDIZE", function() {
186   catchAndAssertEquals(function() {
187     STANDARDIZE(44, 2.1, -10);
188   }, ERRORS.NUM_ERROR);
189+  catchAndAssertEquals(function() {
190+    STANDARDIZE.apply(this, [4, 3, 4, 4]);
191+  }, ERRORS.NA_ERROR);
192+  catchAndAssertEquals(function() {
193+    STANDARDIZE.apply(this, [4, 3]);
194+  }, ERRORS.NA_ERROR);
195 });
196 
197 test("SMALL", function() {
198@@ -614,4 +621,22 @@ test("SMALL", function() {
199   catchAndAssertEquals(function() {
200     SMALL([44, 2.1], 3);
201   }, ERRORS.NUM_ERROR);
202+  catchAndAssertEquals(function() {
203+    SMALL.apply(this, [[44, 2.1], 3, 4]);
204+  }, ERRORS.NA_ERROR);
205+});
206+
207+test("LARGE", function() {
208+  assertEquals(LARGE([2, 12, 22, 1, 0.1, 44, "77", "hello"], 2), 22);
209+  assertEquals(LARGE([2, 12, 22, 1, 0.1, 44, "77", "hello"], 3), 12);
210+  assertEquals(LARGE([2, 12, 22, 1, 0.1, 44, "77", "hello"], 4), 2);
211+  catchAndAssertEquals(function() {
212+    LARGE([44, 2.1, "str"], 3);
213+  }, ERRORS.NUM_ERROR);
214+  catchAndAssertEquals(function() {
215+    LARGE([44, 2.1], 3);
216+  }, ERRORS.NUM_ERROR);
217+  catchAndAssertEquals(function() {
218+    LARGE.apply(this, [[44, 2.1], 3, 4]);
219+  }, ERRORS.NA_ERROR);
220 });
221\ No newline at end of file
222diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
223index 2664924..a07d65f 100644
224--- a/tests/SheetFormulaTest.ts
225+++ b/tests/SheetFormulaTest.ts
226@@ -711,6 +711,10 @@ test("Sheet SMALL", function(){
227   assertFormulaEquals('=SMALL([1, 2], 2)', 2);
228 });
229 
230+test("Sheet LARGE", function(){
231+  assertFormulaEquals('=LARGE([1, 2], 2)', 1);
232+});
233+
234 test("Sheet *", function(){
235   assertFormulaEquals('= 10 * 10', 100);
236   assertFormulaEquals('= 10 * 0', 0);