commit
message
[RANK] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-30 12:05:59
stats
8 file(s) changed,
93 insertions(+),
6 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 a7842f6..66067ae 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -2079,6 +2079,17 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### RANK
11+
12+```
13+ Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top.
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 85605ed..a1bf6b5 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
32 * RANK.AVG
33 * RANK.EQ
34 * T.INV
35diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
36index 232bd48..90b6b95 100644
37--- a/dist/Formulas/AllFormulas.js
38+++ b/dist/Formulas/AllFormulas.js
39@@ -193,6 +193,7 @@ exports.SKEW = Statistical_1.SKEW;
40 exports.STEYX = Statistical_1.STEYX;
41 exports.PROB = Statistical_1.PROB;
42 exports.MODE = Statistical_1.MODE;
43+exports.RANK = Statistical_1.RANK;
44 var Text_1 = require("./Text");
45 exports.ARABIC = Text_1.ARABIC;
46 exports.CHAR = Text_1.CHAR;
47diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
48index 651d5a2..b8b1cef 100644
49--- a/dist/Formulas/Statistical.js
50+++ b/dist/Formulas/Statistical.js
51@@ -1862,3 +1862,30 @@ var MODE = function () {
52 return maxItems[0];
53 };
54 exports.MODE = MODE;
55+/**
56+ * Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top.
57+ * @param value - Value to find the rank of.
58+ * @param data - Values or range of the data-set.
59+ * @param isAscending - [OPTIONAL] The type of rank: 0 to rank from the highest, 1 to rank from the lowest. Defaults to
60+ * 0.
61+ * @returns {number}
62+ * @constructor
63+ */
64+var RANK = function (value, data, isAscending) {
65+ ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 2, 3, "RANK");
66+ value = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
67+ var range = Filter_1.Filter.flattenAndThrow(data).map(TypeConverter_1.TypeConverter.valueToNumber);
68+ isAscending = (typeof isAscending === 'undefined') ? false : isAscending;
69+ var sort = (isAscending) ? function (a, b) {
70+ return a - b;
71+ } : function (a, b) {
72+ return b - a;
73+ };
74+ range = range.sort(sort);
75+ var rangeIndex = range.indexOf(value);
76+ if (rangeIndex === -1) {
77+ throw new Errors_1.NAError("RANK can't produce a result because parameter 1 is not in the dataset.");
78+ }
79+ return range.indexOf(value) + 1;
80+};
81+exports.RANK = RANK;
82diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
83index 2a033c8..fbffeff 100644
84--- a/src/Formulas/AllFormulas.ts
85+++ b/src/Formulas/AllFormulas.ts
86@@ -198,7 +198,8 @@ import {
87 SKEW,
88 STEYX,
89 PROB,
90- MODE
91+ MODE,
92+ RANK
93 } from "./Statistical";
94 import {
95 ARABIC,
96@@ -471,5 +472,6 @@ export {
97 SKEW,
98 STEYX,
99 PROB,
100- MODE
101+ MODE,
102+ RANK
103 }
104\ No newline at end of file
105diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
106index 6abae9b..5b12993 100644
107--- a/src/Formulas/Statistical.ts
108+++ b/src/Formulas/Statistical.ts
109@@ -1842,6 +1842,34 @@ var MODE = function (...values) {
110 };
111
112
113+/**
114+ * Returns the position of a given entry in the entire list, measured either from top to bottom or bottom to top.
115+ * @param value - Value to find the rank of.
116+ * @param data - Values or range of the data-set.
117+ * @param isAscending - [OPTIONAL] The type of rank: 0 to rank from the highest, 1 to rank from the lowest. Defaults to
118+ * 0.
119+ * @returns {number}
120+ * @constructor
121+ */
122+var RANK = function (value, data, isAscending?) {
123+ ArgsChecker.checkLengthWithin(arguments, 2, 3, "RANK");
124+ value = TypeConverter.firstValueAsNumber(value);
125+ var range = Filter.flattenAndThrow(data).map(TypeConverter.valueToNumber);
126+ isAscending = (typeof isAscending === 'undefined') ? false : isAscending;
127+ var sort = (isAscending) ? function (a, b) {
128+ return a - b;
129+ } : function (a, b) {
130+ return b - a;
131+ };
132+ range = range.sort(sort);
133+ var rangeIndex = range.indexOf(value);
134+ if (rangeIndex === -1) {
135+ throw new NAError("RANK can't produce a result because parameter 1 is not in the dataset.");
136+ }
137+ return range.indexOf(value) + 1;
138+};
139+
140+
141 export {
142 AVERAGE,
143 AVERAGEA,
144@@ -1899,5 +1927,6 @@ export {
145 SKEW,
146 STEYX,
147 PROB,
148- MODE
149+ MODE,
150+ RANK
151 }
152\ No newline at end of file
153diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
154index 1b446a5..d11c21a 100644
155--- a/tests/Formulas/StatisticalTest.ts
156+++ b/tests/Formulas/StatisticalTest.ts
157@@ -55,7 +55,8 @@ import {
158 SKEW,
159 STEYX,
160 PROB,
161- MODE
162+ MODE,
163+ RANK
164 } from "../../src/Formulas/Statistical";
165 import * as ERRORS from "../../src/Errors";
166 import {
167@@ -1128,4 +1129,18 @@ test("MODE", function() {
168 catchAndAssertEquals(function() {
169 MODE();
170 }, ERRORS.NA_ERROR);
171+});
172+
173+test("RANK", function() {
174+ assertEquals(RANK(2, [1, 2, 3, 4, 5, 6, 7, 8, 9], true), 2);
175+ assertEquals(RANK(2, [7, 1, 2, 4, 100, 8, 9], true), 2);
176+ catchAndAssertEquals(function() {
177+ RANK(44, [7, 1]);
178+ }, ERRORS.NA_ERROR);
179+ catchAndAssertEquals(function() {
180+ RANK.apply(this, [44]);
181+ }, ERRORS.NA_ERROR);
182+ catchAndAssertEquals(function() {
183+ RANK.apply(this, [44, [7, 1], true, false]);
184+ }, ERRORS.NA_ERROR);
185 });
186\ No newline at end of file
187diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
188index 9c5419f..cda030d 100644
189--- a/tests/SheetFormulaTest.ts
190+++ b/tests/SheetFormulaTest.ts
191@@ -930,6 +930,10 @@ test("Sheet MODE", function(){
192 assertFormulaEquals('=MODE(1, 6, 7, 7, 8)', 7);
193 });
194
195+test("Sheet RANK", function(){
196+ assertFormulaEquals('=RANK([2], [1, 2, 3, 4, 5, 6, 7, 8, 9], true)', 2);
197+});
198+
199 test("Sheet *", function(){
200 assertFormulaEquals('= 10 * 10', 100);
201 assertFormulaEquals('= 10 * 0', 0);