spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[RANK.EQ] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-30 12:35:38
stats
8 file(s) changed, 103 insertions(+), 8 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 9effb9c..1122db0 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2101,6 +2101,17 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### EQ 
 11+
 12+```
 13+  Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top. If there is more than one entry of the same value in the dataset, the top rank of the entries will be returned. 
 14+@param value - Value to find the rank of. 
 15+@param data - Values or range of the data-set. 
 16+@param isAscending - [OPTIONAL] The type of rank: 0 to rank from the highest, 1 to rank from the lowest. Defaults to 0. 
 17+@returns {number} 
 18+@constructor
 19+```
 20 ## Text
 21 
 22 
 23diff --git a/TODO.md b/TODO.md
 24index 9cc3342..7ade243 100644
 25--- a/TODO.md
 26+++ b/TODO.md
 27@@ -65,7 +65,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 28 * HYPGEOMDIST
 29 * LOGINV
 30 * LOGNORMDIST
 31-* RANK.EQ
 32 * T.INV
 33 * T.INV.2T
 34 * TDIST
 35diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 36index 6b9608d..7d44b26 100644
 37--- a/dist/Formulas/AllFormulas.js
 38+++ b/dist/Formulas/AllFormulas.js
 39@@ -195,6 +195,7 @@ exports.PROB = Statistical_1.PROB;
 40 exports.MODE = Statistical_1.MODE;
 41 exports.RANK = Statistical_1.RANK;
 42 exports.RANK$AVG = Statistical_1.RANK$AVG;
 43+exports.RANK$EQ = Statistical_1.RANK$EQ;
 44 var Text_1 = require("./Text");
 45 exports.ARABIC = Text_1.ARABIC;
 46 exports.CHAR = Text_1.CHAR;
 47@@ -240,6 +241,7 @@ var __COMPLEX = {
 48     "PERCENTRANK.INC": Statistical_1.PERCENTRANK,
 49     "PERCENTRANK.EXC": Statistical_1.PERCENTRANK$EXC,
 50     "ERROR.TYPE": Info_1.ERRORTYPE,
 51-    "RANK.AVG": Statistical_1.RANK$AVG
 52+    "RANK.AVG": Statistical_1.RANK$AVG,
 53+    "RANK.EQ": Statistical_1.RANK$EQ
 54 };
 55 exports.__COMPLEX = __COMPLEX;
 56diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 57index ec2d588..8f5dbaf 100644
 58--- a/dist/Formulas/Statistical.js
 59+++ b/dist/Formulas/Statistical.js
 60@@ -1927,3 +1927,31 @@ var RANK$AVG = function (value, data, isAscending) {
 61     return (count > 1) ? (2 * rangeIndex + count + 1) / 2 : rangeIndex + 1;
 62 };
 63 exports.RANK$AVG = RANK$AVG;
 64+/**
 65+ * Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top. If
 66+ * there is more than one entry of the same value in the dataset, the top rank of the entries will be returned.
 67+ * @param value - Value to find the rank of.
 68+ * @param data - Values or range of the data-set.
 69+ * @param isAscending - [OPTIONAL] The type of rank: 0 to rank from the highest, 1 to rank from the lowest. Defaults to
 70+ * 0.
 71+ * @returns {number}
 72+ * @constructor
 73+ */
 74+var RANK$EQ = function (value, data, isAscending) {
 75+    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 2, 3, "RANK.EQ");
 76+    value = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
 77+    var range = Filter_1.Filter.flattenAndThrow(data).map(TypeConverter_1.TypeConverter.valueToNumber);
 78+    isAscending = (typeof isAscending === 'undefined') ? false : isAscending;
 79+    var sort = (isAscending) ? function (a, b) {
 80+        return a - b;
 81+    } : function (a, b) {
 82+        return b - a;
 83+    };
 84+    range = range.sort(sort);
 85+    var rangeIndex = range.indexOf(value);
 86+    if (rangeIndex === -1) {
 87+        throw new Errors_1.NAError("RANK.EQ can't produce a result because parameter 1 is not in the dataset.");
 88+    }
 89+    return range.indexOf(value) + 1;
 90+};
 91+exports.RANK$EQ = RANK$EQ;
 92diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 93index 9337b08..f882bdd 100644
 94--- a/src/Formulas/AllFormulas.ts
 95+++ b/src/Formulas/AllFormulas.ts
 96@@ -200,7 +200,8 @@ import {
 97   PROB,
 98   MODE,
 99   RANK,
100-  RANK$AVG
101+  RANK$AVG,
102+  RANK$EQ
103 } from "./Statistical";
104 import {
105   ARABIC,
106@@ -250,7 +251,8 @@ var __COMPLEX = {
107   "PERCENTRANK.INC": PERCENTRANK,
108   "PERCENTRANK.EXC": PERCENTRANK$EXC,
109   "ERROR.TYPE": ERRORTYPE,
110-  "RANK.AVG": RANK$AVG
111+  "RANK.AVG": RANK$AVG,
112+  "RANK.EQ": RANK$EQ
113 };
114 
115 export {
116@@ -476,5 +478,6 @@ export {
117   PROB,
118   MODE,
119   RANK,
120-  RANK$AVG
121+  RANK$AVG,
122+  RANK$EQ
123 }
124\ No newline at end of file
125diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
126index 6a55a42..11d6285 100644
127--- a/src/Formulas/Statistical.ts
128+++ b/src/Formulas/Statistical.ts
129@@ -1911,6 +1911,35 @@ var RANK$AVG =  function (value, data, isAscending?) {
130 };
131 
132 
133+/**
134+ * Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top. If
135+ * there is more than one entry of the same value in the dataset, the top rank of the entries will be returned.
136+ * @param value - Value to find the rank of.
137+ * @param data - Values or range of the data-set.
138+ * @param isAscending - [OPTIONAL] The type of rank: 0 to rank from the highest, 1 to rank from the lowest. Defaults to
139+ * 0.
140+ * @returns {number}
141+ * @constructor
142+ */
143+var RANK$EQ =  function (value, data, isAscending?) {
144+  ArgsChecker.checkLengthWithin(arguments, 2, 3, "RANK.EQ");
145+  value = TypeConverter.firstValueAsNumber(value);
146+  var range = Filter.flattenAndThrow(data).map(TypeConverter.valueToNumber);
147+  isAscending = (typeof isAscending === 'undefined') ? false : isAscending;
148+  var sort = (isAscending) ? function (a, b) {
149+    return a - b;
150+  } : function (a, b) {
151+    return b - a;
152+  };
153+  range = range.sort(sort);
154+  var rangeIndex = range.indexOf(value);
155+  if (rangeIndex === -1) {
156+    throw new NAError("RANK.EQ can't produce a result because parameter 1 is not in the dataset.");
157+  }
158+  return range.indexOf(value) + 1;
159+};
160+
161+
162 export {
163   AVERAGE,
164   AVERAGEA,
165@@ -1970,5 +1999,6 @@ export {
166   PROB,
167   MODE,
168   RANK,
169-  RANK$AVG
170+  RANK$AVG,
171+  RANK$EQ
172 }
173\ No newline at end of file
174diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
175index b0e1836..5218046 100644
176--- a/tests/Formulas/StatisticalTest.ts
177+++ b/tests/Formulas/StatisticalTest.ts
178@@ -57,7 +57,8 @@ import {
179   PROB,
180   MODE,
181   RANK,
182-  RANK$AVG
183+  RANK$AVG,
184+  RANK$EQ
185 } from "../../src/Formulas/Statistical";
186 import * as ERRORS from "../../src/Errors";
187 import {
188@@ -1165,4 +1166,23 @@ test("RANK.AVG", function() {
189   catchAndAssertEquals(function() {
190     RANK$AVG.apply(this, [44, [7, 1], true, false]);
191   }, ERRORS.NA_ERROR);
192+});
193+
194+test("RANK.EQ", function() {
195+  assertEquals(RANK$EQ(2, [1, 2, 3, 4, 5, 6, 7, 8, 9], true), 2);
196+  assertEquals(RANK$EQ(4, [2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9], true), 6);
197+  assertEquals(RANK$EQ(2, [1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9], true), 2);
198+  assertEquals(RANK$EQ(3, [2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9], true), 3);
199+  assertEquals(RANK$EQ(3, [1, 3, 4, 5, 6, 7, 8, 9]), 7);
200+  assertEquals(RANK$EQ(3, [1, 3, 4, 5, 6, 7, 8, 9], true), 2);
201+  assertEquals(RANK$EQ(2, [7, 1, 2, 4, 100, 8, 9], true), 2);
202+  catchAndAssertEquals(function() {
203+    RANK$EQ(44, [7, 1]);
204+  }, ERRORS.NA_ERROR);
205+  catchAndAssertEquals(function() {
206+    RANK$EQ.apply(this, [44]);
207+  }, ERRORS.NA_ERROR);
208+  catchAndAssertEquals(function() {
209+    RANK$EQ.apply(this, [44, [7, 1], true, false]);
210+  }, ERRORS.NA_ERROR);
211 });
212\ No newline at end of file
213diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
214index 99cce03..ec0f3cc 100644
215--- a/tests/SheetFormulaTest.ts
216+++ b/tests/SheetFormulaTest.ts
217@@ -938,6 +938,9 @@ test("Sheet RANK.AVG", function(){
218   assertFormulaEquals('=RANK.AVG([2], [1, 2, 3, 4, 5, 6, 7, 8, 9], true)', 2);
219 });
220 
221+test("Sheet RANK.EQ", function(){
222+  assertFormulaEquals('=RANK.EQ([2], [1, 2, 3, 4, 5, 6, 7, 8, 9], true)', 2);
223+});
224 
225 test("Sheet *", function(){
226   assertFormulaEquals('= 10 * 10', 100);