commit
message
[Text.REPT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-08 16:47:27
stats
8 file(s) changed,
72 insertions(+),
6 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Text.js
src/Formulas/AllFormulas.ts
src/Formulas/Text.ts
tests/Formulas/TextTest.ts
tests/SheetFormulaTest.ts
1diff --git a/DOCS.md b/DOCS.md
2index 478d921..0ca004d 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -2461,3 +2461,12 @@
6 @param position - [OPTIONAL default 1] The position in the text where the search is to start.
7 @constructor
8 ```
9+
10+### REPT
11+
12+```
13+ Repeats a character string by the given number of copies.
14+@param text - The text to be repeated.
15+@param numberOfReps - The number of repetitions
16+@constructor
17+```
18diff --git a/TODO.md b/TODO.md
19index a538d01..6496d7e 100644
20--- a/TODO.md
21+++ b/TODO.md
22@@ -46,7 +46,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
23 * REGEXMATCH - May be difficult considering language differences.
24 * REGEXREPLACE - May be difficult considering language differences.
25 * REPLACE
26-* REPT
27 * SUBSTITUTE
28 * VALUE
29
30diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
31index 0131e70..cfcfdf1 100644
32--- a/dist/Formulas/AllFormulas.js
33+++ b/dist/Formulas/AllFormulas.js
34@@ -233,6 +233,7 @@ exports.LEN = Text_1.LEN;
35 exports.LEFT = Text_1.LEFT;
36 exports.RIGHT = Text_1.RIGHT;
37 exports.SEARCH = Text_1.SEARCH;
38+exports.REPT = Text_1.REPT;
39 var Date_1 = require("./Date");
40 exports.DATE = Date_1.DATE;
41 exports.DATEVALUE = Date_1.DATEVALUE;
42diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
43index 49ef8da..abc96e2 100644
44--- a/dist/Formulas/Text.js
45+++ b/dist/Formulas/Text.js
46@@ -818,3 +818,20 @@ var SEARCH = function (findText, withinText, position) {
47 throw new Errors_1.ValueError("For SEARCH evaluation, cannot find '" + findText + "' inside '" + withinText + "'");
48 };
49 exports.SEARCH = SEARCH;
50+/**
51+ * Repeats a character string by the given number of copies.
52+ * @param text - The text to be repeated.
53+ * @param numberOfReps - The number of repetitions
54+ * @constructor
55+ */
56+var REPT = function (text, numberOfReps) {
57+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "REPT");
58+ text = TypeConverter_1.TypeConverter.firstValueAsString(text);
59+ numberOfReps = TypeConverter_1.TypeConverter.firstValueAsNumber(numberOfReps);
60+ if (numberOfReps < 0) {
61+ throw new Errors_1.ValueError("Formula REPT parameter 2 value is " + numberOfReps
62+ + ", but should be greater than or equal to 0.");
63+ }
64+ return new Array(numberOfReps + 1).join(text);
65+};
66+exports.REPT = REPT;
67diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
68index 21d62ce..1d8209f 100644
69--- a/src/Formulas/AllFormulas.ts
70+++ b/src/Formulas/AllFormulas.ts
71@@ -240,7 +240,8 @@ import {
72 LEN,
73 LEFT,
74 RIGHT,
75- SEARCH
76+ SEARCH,
77+ REPT
78 } from "./Text"
79 import {
80 DATE,
81@@ -541,5 +542,6 @@ export {
82 LEN,
83 LEFT,
84 RIGHT,
85- SEARCH
86+ SEARCH,
87+ REPT
88 }
89\ No newline at end of file
90diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
91index 5270124..0cfef32 100644
92--- a/src/Formulas/Text.ts
93+++ b/src/Formulas/Text.ts
94@@ -850,6 +850,23 @@ let SEARCH = function (findText, withinText, position?) {
95 throw new ValueError("For SEARCH evaluation, cannot find '" + findText + "' inside '" + withinText + "'");
96 };
97
98+/**
99+ * Repeats a character string by the given number of copies.
100+ * @param text - The text to be repeated.
101+ * @param numberOfReps - The number of repetitions
102+ * @constructor
103+ */
104+let REPT = function (text, numberOfReps) {
105+ ArgsChecker.checkLength(arguments, 2, "REPT");
106+ text = TypeConverter.firstValueAsString(text);
107+ numberOfReps = TypeConverter.firstValueAsNumber(numberOfReps);
108+ if (numberOfReps < 0) {
109+ throw new ValueError("Formula REPT parameter 2 value is " + numberOfReps
110+ + ", but should be greater than or equal to 0.");
111+ }
112+ return new Array(numberOfReps + 1).join(text);
113+};
114+
115 export {
116 ARABIC,
117 CHAR,
118@@ -868,5 +885,6 @@ export {
119 LEN,
120 LEFT,
121 RIGHT,
122- SEARCH
123+ SEARCH,
124+ REPT
125 }
126\ No newline at end of file
127diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
128index 05c4c5d..fc9a220 100644
129--- a/tests/Formulas/TextTest.ts
130+++ b/tests/Formulas/TextTest.ts
131@@ -16,7 +16,8 @@ import {
132 LEN,
133 LEFT,
134 RIGHT,
135- SEARCH
136+ SEARCH,
137+ REPT
138 } from "../../src/Formulas/Text";
139 import * as ERRORS from "../../src/Errors";
140 import {
141@@ -467,3 +468,18 @@ test("SEARCH", function(){
142 SEARCH.apply(this, [1, 2, 3, 4]);
143 }, ERRORS.NA_ERROR);
144 });
145+
146+test("REPT", function(){
147+ assertEquals(REPT("s", 0), "");
148+ assertEquals(REPT("s", 1), "s");
149+ assertEquals(REPT("s", 10), "ssssssssss");
150+ catchAndAssertEquals(function() {
151+ REPT("s", -1);
152+ }, ERRORS.VALUE_ERROR);
153+ catchAndAssertEquals(function() {
154+ REPT.apply(this, [1]);
155+ }, ERRORS.NA_ERROR);
156+ catchAndAssertEquals(function() {
157+ REPT.apply(this, [1, 2, 3]);
158+ }, ERRORS.NA_ERROR);
159+});
160diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
161index fbebc50..844ba5d 100644
162--- a/tests/SheetFormulaTest.ts
163+++ b/tests/SheetFormulaTest.ts
164@@ -1069,6 +1069,10 @@ test("Sheet SEARCH", function(){
165 assertFormulaEquals('=SEARCH("soup", "soup?")', 1);
166 });
167
168+test("Sheet REPT", function(){
169+ assertFormulaEquals('=REPT("a", 2)', "aa");
170+});
171+
172 test("Sheet parsing error", function(){
173 assertFormulaEqualsError('= 10e', PARSE_ERROR);
174 assertFormulaEqualsError('= SUM(', PARSE_ERROR);