This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathextras.js
More file actions
237 lines (210 loc) · 6.41 KB
/
Copy pathextras.js
File metadata and controls
237 lines (210 loc) · 6.41 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
/*
Copyright (c) 2004-2006, 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.extras");
dojo.require("dojo.string.common");
dojo.require("dojo.lang");
/**
* Performs parameterized substitutions on a string. For example,
* dojo.string.substituteParams("File '%{0}' is not found in directory '%{1}'.","foo.html","/temp");
* returns
* "File 'foo.html' is not found in directory '/temp'."
*
* @param template the original string template with %{values} to be replaced
* @param hash name/value pairs (type object) to provide substitutions. Alternatively, substitutions may be
* included as arguments 1..n to this function, corresponding to template parameters 0..n-1
* @return the completed string. Throws an exception if any parameter is unmatched
*/
//TODO: use ${} substitution syntax instead, like widgets do?
dojo.string.substituteParams = function(template /*string */, hash /* object - optional or ... */) {
var map = (typeof hash == 'object') ? hash : dojo.lang.toArray(arguments, 1);
return template.replace(/\%\{(\w+)\}/g, function(match, key){
return map[key] || dojo.raise("Substitution not found: " + key);
});
};
/**
* 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) {
dojo.deprecated("dojo.string.paramString",
"use dojo.string.substituteParams instead", "0.4");
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(' ');
for(var i=0; i<words.length; i++){
words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
}
return words.join(" ");
}
/**
* 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;
}
dojo.string.escape = function(type, str) {
var args = dojo.lang.toArray(arguments, 1);
switch(type.toLowerCase()) {
case "xml":
case "html":
case "xhtml":
return dojo.string.escapeXml.apply(this, args);
case "sql":
return dojo.string.escapeSql.apply(this, args);
case "regexp":
case "regex":
return dojo.string.escapeRegExp.apply(this, args);
case "javascript":
case "jscript":
case "js":
return dojo.string.escapeJavaScript.apply(this, args);
case "ascii":
// so it's encode, but it seems useful
return dojo.string.encodeAscii.apply(this, args);
default:
return str;
}
}
dojo.string.escapeXml = function(str, noSingleQuotes) {
str = str.replace(/&/gm, "&").replace(/</gm, "<")
.replace(/>/gm, ">").replace(/"/gm, """);
if(!noSingleQuotes) { str = str.replace(/'/gm, "'"); }
return str;
}
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");
}
dojo.string.escapeString = function(str){
return ('"' + str.replace(/(["\\])/g, '\\$1') + '"'
).replace(/[\f]/g, "\\f"
).replace(/[\b]/g, "\\b"
).replace(/[\n]/g, "\\n"
).replace(/[\t]/g, "\\t"
).replace(/[\r]/g, "\\r");
}
// 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(/\.+$/, "") + "...";
}
}
/**
* Returns true if 'str' ends with 'end'
*/
dojo.string.endsWith = function(str, end, ignoreCase) {
if(ignoreCase) {
str = str.toLowerCase();
end = end.toLowerCase();
}
if((str.length - end.length) < 0){
return false;
}
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' contains 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;
}
dojo.string.normalizeNewlines = function (text,newlineChar) {
if (newlineChar == "\n") {
text = text.replace(/\r\n/g, "\n");
text = text.replace(/\r/g, "\n");
} else if (newlineChar == "\r") {
text = text.replace(/\r\n/g, "\r");
text = text.replace(/\n/g, "\r");
} else {
text = text.replace(/([^\r])\n/g, "$1\r\n");
text = text.replace(/\r([^\n])/g, "\r\n$1");
}
return text;
}
dojo.string.splitEscaped = function (str,charac) {
var components = [];
for (var i = 0, prevcomma = 0; i < str.length; i++) {
if (str.charAt(i) == '\\') { i++; continue; }
if (str.charAt(i) == charac) {
components.push(str.substring(prevcomma, i));
prevcomma = i + 1;
}
}
components.push(str.substr(prevcomma));
return components;
}