-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.gs
99 lines (80 loc) · 2.49 KB
/
StringUtils.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* V1.1.1
* https://developers.google.com/apps-script/reference/
* https://sites.google.com/site/scriptsexamples/custom-methods/gsunit
*
* Could change logging to https://github.com/peterherrmann/BetterLog
*
* Created by craigneasbey (https://github.com/craigneasbey/GoogleScripts/tree/master/Tennis)
*/
/**
* StringUtils functions cannot be added to a StringUtils object as LoadConfig
* uses them. Global configuration must be declared first.
*/
/**
* Checks if variable is defined
*/
function isEmpty(val) {
if(typeof val !== 'undefined') {
if(val !== null) {
return false;
}
}
return true;
}
/**
* Checks if a string is defined and is empty
*/
function isEmptyStr(str) {
if(!isEmpty(str)) {
if(str !== "") {
return false;
}
}
return true;
}
/**
* Check the value, if undefined or empty, return the default
*
* http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function
*/
function defaultFor(value, defaultValue) {
return typeof value !== 'undefined' && value !== '' ? value : defaultValue;
}
/**
* Tests
*/
function test_string_suite() {
test_isEmptyStr();
test_isEmpty();
test_defaultFor();
}
function test_isEmptyStr() {
Logger.log(assertEquals('No string ', true, isEmptyStr()));
Logger.log(assertEquals('Null string ', true, isEmptyStr(null)));
Logger.log(assertEquals('Empty string ', true, isEmptyStr("")));
Logger.log(assertEquals('Not Empty string ', false, isEmptyStr("test")));
}
function test_isEmpty() {
Logger.log(assertEquals('No string ', true, isEmpty()));
Logger.log(assertEquals('Null string ', true, isEmpty(null)));
Logger.log(assertEquals('Empty string ', false, isEmpty("")));
Logger.log(assertEquals('Not Empty string ', false, isEmpty("test")));
}
function test_defaultFor() {
var value;
var defaultValue = 'test undefined';
var expected = 'test undefined';
var actual = defaultFor(value, defaultValue);
Logger.log(assertEquals('Undefined', expected, actual));
value = '';
defaultValue = 'test defined';
expected = 'test defined';
actual = defaultFor(value, defaultValue);
Logger.log(assertEquals('Defined', expected, actual));
value = 'This is a test';
defaultValue = 'test text';
expected = 'This is a test';
actual = defaultFor(value, defaultValue);
Logger.log(assertEquals('This is a test', expected, actual));
}