string_wev8.js
6.55 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.string");
dojo.require("dojo.lang");
/**
* Trim whitespace from 'str'. If 'wh' > 0,
* only trim from start, if 'wh' < 0, only trim
* from end, otherwise trim both ends
*/
dojo.string.trim = function(str, wh){
if(!dojo.lang.isString(str)){ return str; }
if(!str.length){ return str; }
if(wh > 0) {
return str.replace(/^\s+/, "");
} else if(wh < 0) {
return str.replace(/\s+$/, "");
} else {
return str.replace(/^\s+|\s+$/g, "");
}
}
/**
* Trim whitespace at the beginning of 'str'
*/
dojo.string.trimStart = function(str) {
return dojo.string.trim(str, 1);
}
/**
* Trim whitespace at the end of 'str'
*/
dojo.string.trimEnd = function(str) {
return dojo.string.trim(str, -1);
}
/**
* Parameterized string function
* str - formatted string with %{values} to be replaces
* pairs - object of name: "value" value pairs
* killExtra - remove all remaining %{values} after pairs are inserted
*/
dojo.string.paramString = function(str, pairs, killExtra) {
for(var name in pairs) {
var re = new RegExp("\\%\\{" + name + "\\}", "g");
str = str.replace(re, pairs[name]);
}
if(killExtra) { str = str.replace(/%\{([^\}\s]+)\}/g, ""); }
return str;
}
/** Uppercases the first letter of each word */
dojo.string.capitalize = function (str) {
if (!dojo.lang.isString(str)) { return ""; }
if (arguments.length == 0) { str = this; }
var words = str.split(' ');
var retval = "";
var len = words.length;
for (var i=0; i<len; i++) {
var word = words[i];
word = word.charAt(0).toUpperCase() + word.substring(1, word.length);
retval += word;
if (i < len-1)
retval += " ";
}
return new String(retval);
}
/**
* Return true if the entire string is whitespace characters
*/
dojo.string.isBlank = function (str) {
if(!dojo.lang.isString(str)) { return true; }
return (dojo.string.trim(str).length == 0);
}
dojo.string.encodeAscii = function(str) {
if(!dojo.lang.isString(str)) { return str; }
var ret = "";
var value = escape(str);
var match, re = /%u([0-9A-F]{4})/i;
while((match = value.match(re))) {
var num = Number("0x"+match[1]);
var newVal = escape("&#" + num + ";");
ret += value.substring(0, match.index) + newVal;
value = value.substring(match.index+match[0].length);
}
ret += value.replace(/\+/g, "%2B");
return ret;
}
// TODO: make an HTML version
dojo.string.summary = function(str, len) {
if(!len || str.length <= len) {
return str;
} else {
return str.substring(0, len).replace(/\.+$/, "") + "...";
}
}
dojo.string.escape = function(type, str) {
switch(type.toLowerCase()) {
case "xml":
case "html":
case "xhtml":
return dojo.string.escapeXml(str);
case "sql":
return dojo.string.escapeSql(str);
case "regexp":
case "regex":
return dojo.string.escapeRegExp(str);
case "javascript":
case "jscript":
case "js":
return dojo.string.escapeJavaScript(str);
case "ascii":
// so it's encode, but it seems useful
return dojo.string.encodeAscii(str);
default:
return str;
}
}
dojo.string.escapeXml = function(str) {
return str.replace(/&/gm, "&").replace(/</gm, "<")
.replace(/>/gm, ">").replace(/"/gm, """).replace(/'/gm, "'");
}
dojo.string.escapeSql = function(str) {
return str.replace(/'/gm, "''");
}
dojo.string.escapeRegExp = function(str) {
return str.replace(/\\/gm, "\\\\").replace(/([\f\b\n\t\r])/gm, "\\$1");
}
dojo.string.escapeJavaScript = function(str) {
return str.replace(/(["'\f\b\n\t\r])/gm, "\\$1");
}
/**
* Return 'str' repeated 'count' times, optionally
* placing 'separator' between each rep
*/
dojo.string.repeat = function(str, count, separator) {
var out = "";
for(var i = 0; i < count; i++) {
out += str;
if(separator && i < count - 1) {
out += separator;
}
}
return out;
}
/**
* Returns true if 'str' ends with 'end'
*/
dojo.string.endsWith = function(str, end, ignoreCase) {
if(ignoreCase) {
str = str.toLowerCase();
end = end.toLowerCase();
}
return str.lastIndexOf(end) == str.length - end.length;
}
/**
* Returns true if 'str' ends with any of the arguments[2 -> n]
*/
dojo.string.endsWithAny = function(str /* , ... */) {
for(var i = 1; i < arguments.length; i++) {
if(dojo.string.endsWith(str, arguments[i])) {
return true;
}
}
return false;
}
/**
* Returns true if 'str' starts with 'start'
*/
dojo.string.startsWith = function(str, start, ignoreCase) {
if(ignoreCase) {
str = str.toLowerCase();
start = start.toLowerCase();
}
return str.indexOf(start) == 0;
}
/**
* Returns true if 'str' starts with any of the arguments[2 -> n]
*/
dojo.string.startsWithAny = function(str /* , ... */) {
for(var i = 1; i < arguments.length; i++) {
if(dojo.string.startsWith(str, arguments[i])) {
return true;
}
}
return false;
}
/**
* Returns true if 'str' starts with any of the arguments 2 -> n
*/
dojo.string.has = function(str /* , ... */) {
for(var i = 1; i < arguments.length; i++) {
if(str.indexOf(arguments[i] > -1)) {
return true;
}
}
return false;
}
/**
* Pad 'str' to guarantee that it is at least 'len' length
* with the character 'c' at either the start (dir=1) or
* end (dir=-1) of the string
*/
dojo.string.pad = function(str, len/*=2*/, c/*='0'*/, dir/*=1*/) {
var out = String(str);
if(!c) {
c = '0';
}
if(!dir) {
dir = 1;
}
while(out.length < len) {
if(dir > 0) {
out = c + out;
} else {
out += c;
}
}
return out;
}
/** same as dojo.string.pad(str, len, c, 1) */
dojo.string.padLeft = function(str, len, c) {
return dojo.string.pad(str, len, c, 1);
}
/** same as dojo.string.pad(str, len, c, -1) */
dojo.string.padRight = function(str, len, c) {
return dojo.string.pad(str, len, c, -1);
}
// do we even want to offer this? is it worth it?
dojo.string.addToPrototype = function() {
for(var method in dojo.string) {
if(dojo.lang.isFunction(dojo.string[method])) {
var func = (function() {
var meth = method;
switch(meth) {
case "addToPrototype":
return null;
break;
case "escape":
return function(type) {
return dojo.string.escape(type, this);
}
break;
default:
return function() {
var args = [this];
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
dojo.debug(args);
return dojo.string[meth].apply(dojo.string, args);
}
}
})();
if(func) { String.prototype[method] = func; }
}
}
}