commit
message
[Text.PROPER] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-14 15:49:44
stats
7 file(s) changed,
46 insertions(+),
6 deletions(-)
files
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/TODO.md b/TODO.md
2index 05a283f..d130719 100644
3--- a/TODO.md
4+++ b/TODO.md
5@@ -43,7 +43,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
6
7
8 ### Easy formulas to write
9-* PROPER
10 * REGEXEXTRACT - May be difficult considering language differences.
11 * REGEXMATCH - May be difficult considering language differences.
12 * REGEXREPLACE - May be difficult considering language differences.
13diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
14index 9a25942..c45d66e 100644
15--- a/dist/Formulas/AllFormulas.js
16+++ b/dist/Formulas/AllFormulas.js
17@@ -237,6 +237,7 @@ exports.REPT = Text_1.REPT;
18 exports.VALUE = Text_1.VALUE;
19 exports.CLEAN = Text_1.CLEAN;
20 exports.MID = Text_1.MID;
21+exports.PROPER = Text_1.PROPER;
22 var Date_1 = require("./Date");
23 exports.DATE = Date_1.DATE;
24 exports.DATEVALUE = Date_1.DATEVALUE;
25diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
26index e3d1aa0..e45322b 100644
27--- a/dist/Formulas/Text.js
28+++ b/dist/Formulas/Text.js
29@@ -888,3 +888,11 @@ var MID = function (text, start, number) {
30 return text.substring(start - 1, start + number - 1);
31 };
32 exports.MID = MID;
33+var PROPER = function (text) {
34+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "PROPER");
35+ text = TypeConverter_1.TypeConverter.firstValueAsString(text);
36+ return text.replace(/\w\S*/g, function (txt) {
37+ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
38+ });
39+};
40+exports.PROPER = PROPER;
41diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
42index d620625..e266c8a 100644
43--- a/src/Formulas/AllFormulas.ts
44+++ b/src/Formulas/AllFormulas.ts
45@@ -244,7 +244,8 @@ import {
46 REPT,
47 VALUE,
48 CLEAN,
49- MID
50+ MID,
51+ PROPER
52 } from "./Text"
53 import {
54 DATE,
55@@ -549,5 +550,6 @@ export {
56 REPT,
57 VALUE,
58 CLEAN,
59- MID
60+ MID,
61+ PROPER
62 }
63\ No newline at end of file
64diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
65index 4de1a97..957d506 100644
66--- a/src/Formulas/Text.ts
67+++ b/src/Formulas/Text.ts
68@@ -920,6 +920,15 @@ let MID = function (text, start, number) {
69 return text.substring(start - 1, start + number - 1);
70 };
71
72+
73+let PROPER = function (text) {
74+ ArgsChecker.checkLength(arguments, 1, "PROPER");
75+ text = TypeConverter.firstValueAsString(text);
76+ return text.replace(/\w\S*/g, function(txt) {
77+ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
78+ });
79+};
80+
81 export {
82 ARABIC,
83 CHAR,
84@@ -942,5 +951,6 @@ export {
85 REPT,
86 VALUE,
87 CLEAN,
88- MID
89+ MID,
90+ PROPER
91 }
92\ No newline at end of file
93diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
94index e46266a..f5c98e5 100644
95--- a/tests/Formulas/TextTest.ts
96+++ b/tests/Formulas/TextTest.ts
97@@ -20,7 +20,8 @@ import {
98 REPT,
99 VALUE,
100 CLEAN,
101- MID
102+ MID,
103+ PROPER
104 } from "../../src/Formulas/Text";
105 import * as ERRORS from "../../src/Errors";
106 import {
107@@ -538,3 +539,18 @@ test("MID", function(){
108 MID.apply(this, [1, 2]);
109 }, ERRORS.NA_ERROR);
110 });
111+
112+test("PROPER", function(){
113+ assertEquals(PROPER("hey there"), "Hey There");
114+ assertEquals(PROPER("hEY tHERE"), "Hey There");
115+ assertEquals(PROPER("there once was a man, and he lived on the moon."), "There Once Was A Man, And He Lived On The Moon.");
116+ assertEquals(PROPER("my name is h.s. thompson"), "My Name Is H.s. Thompson");
117+ assertEquals(PROPER(true), "True");
118+ assertEquals(PROPER(10), "10");
119+ catchAndAssertEquals(function () {
120+ PROPER.apply(this, []);
121+ }, ERRORS.NA_ERROR);
122+ catchAndAssertEquals(function () {
123+ PROPER.apply(this, [1, 2]);
124+ }, ERRORS.NA_ERROR);
125+});
126diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
127index 899d77c..65d1f1d 100644
128--- a/tests/SheetFormulaTest.ts
129+++ b/tests/SheetFormulaTest.ts
130@@ -1085,6 +1085,10 @@ test("Sheet MID", function(){
131 assertFormulaEquals('=MID("hey there", 5, 4)', "ther");
132 });
133
134+test("Sheet PROPER", function(){
135+ assertFormulaEquals('=PROPER("hey there")', "Hey There");
136+});
137+
138 test("Sheet parsing error", function(){
139 assertFormulaEqualsError('= 10e', PARSE_ERROR);
140 assertFormulaEqualsError('= SUM(', PARSE_ERROR);