spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[SMALL] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-24 00:16:50
stats
8 file(s) changed, 77 insertions(+), 5 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 adf5a9d..5b81072 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1527,6 +1527,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### SMALL 
 11+
 12+```
 13+  Returns the Nth smallest 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 31b3d31..686a62a 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -90,7 +90,6 @@ For example 64 tbs to a qt.
 26 * RANK.EQ
 27 * RSQ
 28 * SKEW
 29-* SMALL
 30 * STEYX
 31 * T.INV
 32 * T.INV.2T
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 1f7851d..5b424e2 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -138,6 +138,7 @@ exports.STDEVPA = Statistical_1.STDEVPA;
 38 exports.TRIMMEAN = Statistical_1.TRIMMEAN;
 39 exports.SLOPE = Statistical_1.SLOPE;
 40 exports.STANDARDIZE = Statistical_1.STANDARDIZE;
 41+exports.SMALL = Statistical_1.SMALL;
 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 cd07590..ad69cdf 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -777,3 +777,23 @@ var STANDARDIZE = function (value, meanValue, std) {
 50     return (value - meanValue) / std;
 51 };
 52 exports.STANDARDIZE = STANDARDIZE;
 53+/**
 54+ * Returns the Nth smallest value in the range, ignoring text values.
 55+ * @param range -  Range or data-set to consider.
 56+ * @param n - N in 'Nth'.
 57+ * @constructor
 58+ */
 59+var SMALL = function (range, n) {
 60+    var data = Filter_1.Filter.flattenAndThrow(range).filter(function (value) {
 61+        return typeof value != "string";
 62+    }).map(function (value) {
 63+        return TypeConverter_1.TypeConverter.valueToNumber(value);
 64+    }).sort(function (a, b) {
 65+        return a - b;
 66+    });
 67+    if (n > data.length || n < 1) {
 68+        throw new Errors_1.NumError("Function SMALL parameter 2 value " + n + " is out of range.");
 69+    }
 70+    return data[n - 1];
 71+};
 72+exports.SMALL = SMALL;
 73diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 74index 1fb3e28..d8e1399 100644
 75--- a/src/Formulas/AllFormulas.ts
 76+++ b/src/Formulas/AllFormulas.ts
 77@@ -143,7 +143,8 @@ import {
 78   STDEVPA,
 79   TRIMMEAN,
 80   SLOPE,
 81-  STANDARDIZE
 82+  STANDARDIZE,
 83+  SMALL
 84 } from "./Statistical";
 85 import {
 86   ARABIC,
 87@@ -355,5 +356,6 @@ export {
 88   SLOPE,
 89   LOWER,
 90   UPPER,
 91-  STANDARDIZE
 92+  STANDARDIZE,
 93+  SMALL
 94 }
 95\ No newline at end of file
 96diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
 97index 04c6208..f923c01 100644
 98--- a/src/Formulas/Statistical.ts
 99+++ b/src/Formulas/Statistical.ts
100@@ -759,6 +759,27 @@ var STANDARDIZE = function (value, meanValue, std) {
101 };
102 
103 
104+/**
105+ * Returns the Nth smallest value in the range, ignoring text values.
106+ * @param range -  Range or data-set to consider.
107+ * @param n - N in 'Nth'.
108+ * @constructor
109+ */
110+var SMALL =  function (range, n) {
111+  var data = Filter.flattenAndThrow(range).filter(function (value) {
112+    return typeof value != "string";
113+  }).map(function (value) {
114+    return TypeConverter.valueToNumber(value);
115+  }).sort(function (a, b) {
116+    return a - b;
117+  });
118+  if (n > data.length || n < 1) {
119+    throw new NumError("Function SMALL parameter 2 value " + n + " is out of range.");
120+  }
121+  return data[n - 1];
122+};
123+
124+
125 export {
126   AVERAGE,
127   AVERAGEA,
128@@ -787,5 +808,6 @@ export {
129   STDEVPA,
130   TRIMMEAN,
131   SLOPE,
132-  STANDARDIZE
133+  STANDARDIZE,
134+  SMALL
135 }
136\ No newline at end of file
137diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
138index 62ca8bf..a6f37b5 100644
139--- a/tests/Formulas/StatisticalTest.ts
140+++ b/tests/Formulas/StatisticalTest.ts
141@@ -26,7 +26,8 @@ import {
142   STDEVPA,
143   TRIMMEAN,
144   SLOPE,
145-  STANDARDIZE
146+  STANDARDIZE,
147+  SMALL
148 } from "../../src/Formulas/Statistical";
149 import * as ERRORS from "../../src/Errors";
150 import {
151@@ -601,4 +602,16 @@ test("STANDARDIZE", function() {
152   catchAndAssertEquals(function() {
153     STANDARDIZE(44, 2.1, -10);
154   }, ERRORS.NUM_ERROR);
155+});
156+
157+test("SMALL", function() {
158+  assertEquals(SMALL([2, 12, 22, 1, 0.1, 44, "77", "hello"], 3), 2);
159+  assertEquals(SMALL([2, 12, 22, 1, 0.1, 44, "77", "hello"], 4), 12);
160+  assertEquals(SMALL([2, 12, 22, 1, 0.1, 44, "77", "hello"], 5), 22);
161+  catchAndAssertEquals(function() {
162+    SMALL([44, 2.1, "str"], 3);
163+  }, ERRORS.NUM_ERROR);
164+  catchAndAssertEquals(function() {
165+    SMALL([44, 2.1], 3);
166+  }, ERRORS.NUM_ERROR);
167 });
168\ No newline at end of file
169diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
170index ca7569f..2664924 100644
171--- a/tests/SheetFormulaTest.ts
172+++ b/tests/SheetFormulaTest.ts
173@@ -707,6 +707,10 @@ test("Sheet STANDARDIZE", function(){
174   assertFormulaEquals('=STANDARDIZE(10, 2, 1)', 8);
175 });
176 
177+test("Sheet SMALL", function(){
178+  assertFormulaEquals('=SMALL([1, 2], 2)', 2);
179+});
180+
181 test("Sheet *", function(){
182   assertFormulaEquals('= 10 * 10', 100);
183   assertFormulaEquals('= 10 * 0', 0);