2150 |
mathias |
1 |
if(!dojo._hasResource["dojo.string"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojo.string"] = true;
|
|
|
3 |
dojo.provide("dojo.string");
|
|
|
4 |
|
|
|
5 |
dojo.string.pad = function(/*String*/text, /*int*/size, /*String?*/ch, /*boolean?*/end){
|
|
|
6 |
// summary:
|
|
|
7 |
// Pad a string to guarantee that it is at least 'size' length by
|
|
|
8 |
// filling with the character 'c' at either the start or end of the
|
|
|
9 |
// string. Pads at the start, by default.
|
|
|
10 |
// text: the string to pad
|
|
|
11 |
// size: length to provide padding
|
|
|
12 |
// ch: character to pad, defaults to '0'
|
|
|
13 |
// end: adds padding at the end if true, otherwise pads at start
|
|
|
14 |
|
|
|
15 |
var out = String(text);
|
|
|
16 |
if(!ch){
|
|
|
17 |
ch = '0';
|
|
|
18 |
}
|
|
|
19 |
while(out.length < size){
|
|
|
20 |
if(end){
|
|
|
21 |
out += ch;
|
|
|
22 |
}else{
|
|
|
23 |
out = ch + out;
|
|
|
24 |
}
|
|
|
25 |
}
|
|
|
26 |
return out; // String
|
|
|
27 |
};
|
|
|
28 |
|
|
|
29 |
dojo.string.substitute = function( /*String*/template,
|
|
|
30 |
/*Object or Array*/map,
|
|
|
31 |
/*Function?*/transform,
|
|
|
32 |
/*Object?*/thisObject){
|
|
|
33 |
// summary:
|
|
|
34 |
// Performs parameterized substitutions on a string. Throws an
|
|
|
35 |
// exception if any parameter is unmatched.
|
|
|
36 |
// description:
|
|
|
37 |
// For example,
|
|
|
38 |
// | dojo.string.substitute("File '${0}' is not found in directory '${1}'.",["foo.html","/temp"]);
|
|
|
39 |
// | dojo.string.substitute("File '${name}' is not found in directory '${info.dir}'.",{name: "foo.html", info: {dir: "/temp"}});
|
|
|
40 |
// both return
|
|
|
41 |
// "File 'foo.html' is not found in directory '/temp'."
|
|
|
42 |
// template:
|
|
|
43 |
// a string with expressions in the form ${key} to be replaced or
|
|
|
44 |
// ${key:format} which specifies a format function. NOTE syntax has
|
|
|
45 |
// changed from %{key}
|
|
|
46 |
// map: where to look for substitutions
|
|
|
47 |
// transform:
|
|
|
48 |
// a function to process all parameters before substitution takes
|
|
|
49 |
// place, e.g. dojo.string.encodeXML
|
|
|
50 |
// thisObject:
|
|
|
51 |
// where to look for optional format function; default to the global
|
|
|
52 |
// namespace
|
|
|
53 |
|
|
|
54 |
return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match, key, format){
|
|
|
55 |
var value = dojo.getObject(key,false,map);
|
|
|
56 |
if(format){ value = dojo.getObject(format,false,thisObject)(value);}
|
|
|
57 |
if(transform){ value = transform(value, key); }
|
|
|
58 |
return value.toString();
|
|
|
59 |
}); // string
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
dojo.string.trim = function(/*String*/ str){
|
|
|
63 |
// summary: trims whitespaces from both sides of the string
|
|
|
64 |
// description:
|
|
|
65 |
// This version of trim() was taken from Steven Levithan's blog:
|
|
|
66 |
// http://blog.stevenlevithan.com/archives/faster-trim-javascript.
|
|
|
67 |
// The short yet good-performing version of this function is
|
|
|
68 |
// dojo.trim(), which is part of the base.
|
|
|
69 |
str = str.replace(/^\s+/, '');
|
|
|
70 |
for(var i = str.length - 1; i > 0; i--){
|
|
|
71 |
if(/\S/.test(str.charAt(i))){
|
|
|
72 |
str = str.substring(0, i + 1);
|
|
|
73 |
break;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
return str; // String
|
|
|
77 |
};
|
|
|
78 |
|
|
|
79 |
}
|