commit
message
[TRIM] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-06 03:22:05
stats
8 file(s) changed,
67 insertions(+),
5 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 bae3d13..bb9fb93 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -1369,3 +1369,12 @@
6 @returns {number}
7 @constructor TODO: Looking up units is not efficient at all. We should use an object instead of iterating through an array.
8 ```
9+
10+### TRIM
11+
12+```
13+ Removes leading and trailing spaces in a specified string.
14+@param value - The string or reference to a cell containing a string to be trimmed.
15+@returns {string}
16+@constructor
17+```
18diff --git a/TODO.md b/TODO.md
19index 2d2efd2..2d890cb 100644
20--- a/TODO.md
21+++ b/TODO.md
22@@ -20,6 +20,9 @@ For example the CHOOSE formula can't be parsed: `=CHOOSE(2, [1, 2, 3])`.
23 For example 64 tbs to a qt.
24
25
26+### Fix documentation regular expression, it is butchering URLs.
27+
28+
29 ### Formulas to write
30
31 * ERROR.TYPE
32@@ -143,7 +146,6 @@ For example 64 tbs to a qt.
33 * SUBSTITUTE
34 * T
35 * TEXT
36-* TRIM
37 * UPPER
38 * VALUE
39 * FREQUENCY
40diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
41index 0c33a7a..7e89104 100644
42--- a/dist/Formulas/AllFormulas.js
43+++ b/dist/Formulas/AllFormulas.js
44@@ -121,6 +121,7 @@ exports.CODE = Text_1.CODE;
45 exports.SPLIT = Text_1.SPLIT;
46 exports.CONCATENATE = Text_1.CONCATENATE;
47 exports.CONVERT = Text_1.CONVERT;
48+exports.TRIM = Text_1.TRIM;
49 var Date_1 = require("./Date");
50 exports.DATE = Date_1.DATE;
51 exports.DATEVALUE = Date_1.DATEVALUE;
52diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
53index 2f3bdef..b7b92a3 100644
54--- a/dist/Formulas/Text.js
55+++ b/dist/Formulas/Text.js
56@@ -408,3 +408,15 @@ var CONVERT = function (value, startUnit, endUnit) {
57 return n * from[6] * from_multiplier / (to[6] * to_multiplier);
58 };
59 exports.CONVERT = CONVERT;
60+/**
61+ * Removes leading and trailing spaces in a specified string.
62+ * @param value - The string or reference to a cell containing a string to be trimmed.
63+ * @returns {string}
64+ * @constructor
65+ */
66+var TRIM = function (value) {
67+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "TRIM");
68+ var text = TypeConverter_1.TypeConverter.valueToString(value);
69+ return text.trim();
70+};
71+exports.TRIM = TRIM;
72diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
73index b3d82ce..a3132c7 100644
74--- a/src/Formulas/AllFormulas.ts
75+++ b/src/Formulas/AllFormulas.ts
76@@ -126,7 +126,8 @@ import {
77 CODE,
78 SPLIT,
79 CONCATENATE,
80- CONVERT
81+ CONVERT,
82+ TRIM
83 } from "./Text"
84 import {
85 DATE,
86@@ -301,5 +302,6 @@ export {
87 NE,
88 NA,
89 CHOOSE,
90- GCD
91+ GCD,
92+ TRIM
93 }
94\ No newline at end of file
95diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
96index 93aef44..2670cb2 100644
97--- a/src/Formulas/Text.ts
98+++ b/src/Formulas/Text.ts
99@@ -425,11 +425,26 @@ var CONVERT = function (value, startUnit, endUnit) {
100 return n * from[6] * from_multiplier / (to[6] * to_multiplier);
101 };
102
103+
104+/**
105+ * Removes leading and trailing spaces in a specified string.
106+ * @param value - The string or reference to a cell containing a string to be trimmed.
107+ * @returns {string}
108+ * @constructor
109+ */
110+var TRIM = function (value) {
111+ ArgsChecker.checkLength(arguments, 1, "TRIM");
112+ var text = TypeConverter.valueToString(value);
113+ return text.trim();
114+};
115+
116+
117 export {
118 ARABIC,
119 CHAR,
120 CODE,
121 SPLIT,
122 CONCATENATE,
123- CONVERT
124+ CONVERT,
125+ TRIM
126 }
127\ No newline at end of file
128diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
129index 7086557..e2d36d8 100644
130--- a/tests/Formulas/TextTest.ts
131+++ b/tests/Formulas/TextTest.ts
132@@ -4,7 +4,8 @@ import {
133 CODE,
134 CONCATENATE,
135 SPLIT,
136- CONVERT
137+ CONVERT,
138+ TRIM
139 } from "../../src/Formulas/Text";
140 import * as ERRORS from "../../src/Errors";
141 import {
142@@ -134,3 +135,16 @@ test("ARABIC", function(){
143 ARABIC(10);
144 }, ERRORS.VALUE_ERROR);
145 });
146+
147+
148+test("TRIM", function(){
149+ assertEquals(TRIM(" test "), "test");
150+ assertEquals(TRIM(5), "5");
151+ assertEquals(TRIM(false), "FALSE");
152+ catchAndAssertEquals(function() {
153+ TRIM.apply(this, []);
154+ }, ERRORS.NA_ERROR);
155+ catchAndAssertEquals(function() {
156+ TRIM.apply(this, ["test", 5]);
157+ }, ERRORS.NA_ERROR);
158+});
159diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
160index c845bb1..6e61b5a 100644
161--- a/tests/SheetFormulaTest.ts
162+++ b/tests/SheetFormulaTest.ts
163@@ -163,6 +163,10 @@ test("Sheet COS", function(){
164 assertFormulaEquals("=COS(PI())", -1);
165 });
166
167+test("Sheet TRIM", function(){
168+ assertFormulaEquals("=TRIM(' trim ')", "trim");
169+});
170+
171 test("Sheet COSH", function(){
172 assertFormulaEquals("=COSH(PI())", 11.591953275521522);
173 });