]>
Commit | Line | Data |
---|---|---|
2f01fe57 AD |
1 | /* |
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. | |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. | |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo.string"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo.string"] = true; | |
2f01fe57 | 10 | dojo.provide("dojo.string"); |
a089699c AD |
11 | |
12 | /*===== | |
13 | dojo.string = { | |
14 | // summary: String utilities for Dojo | |
2f01fe57 | 15 | }; |
a089699c AD |
16 | =====*/ |
17 | ||
18 | dojo.string.rep = function(/*String*/str, /*Integer*/num){ | |
19 | // summary: | |
20 | // Efficiently replicate a string `n` times. | |
21 | // str: | |
22 | // the string to replicate | |
23 | // num: | |
24 | // number of times to replicate the string | |
25 | ||
26 | if(num <= 0 || !str){ return ""; } | |
27 | ||
28 | var buf = []; | |
29 | for(;;){ | |
30 | if(num & 1){ | |
31 | buf.push(str); | |
32 | } | |
33 | if(!(num >>= 1)){ break; } | |
34 | str += str; | |
35 | } | |
36 | return buf.join(""); // String | |
2f01fe57 | 37 | }; |
a089699c AD |
38 | |
39 | dojo.string.pad = function(/*String*/text, /*Integer*/size, /*String?*/ch, /*Boolean?*/end){ | |
40 | // summary: | |
41 | // Pad a string to guarantee that it is at least `size` length by | |
42 | // filling with the character `ch` at either the start or end of the | |
43 | // string. Pads at the start, by default. | |
44 | // text: | |
45 | // the string to pad | |
46 | // size: | |
47 | // length to provide padding | |
48 | // ch: | |
49 | // character to pad, defaults to '0' | |
50 | // end: | |
51 | // adds padding at the end if true, otherwise pads at start | |
52 | // example: | |
53 | // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++". | |
54 | // | dojo.string.pad("Dojo", 10, "+", true); | |
55 | ||
56 | if(!ch){ | |
57 | ch = '0'; | |
58 | } | |
59 | var out = String(text), | |
60 | pad = dojo.string.rep(ch, Math.ceil((size - out.length) / ch.length)); | |
61 | return end ? out + pad : pad + out; // String | |
2f01fe57 | 62 | }; |
a089699c AD |
63 | |
64 | dojo.string.substitute = function( /*String*/ template, | |
65 | /*Object|Array*/map, | |
66 | /*Function?*/ transform, | |
67 | /*Object?*/ thisObject){ | |
68 | // summary: | |
69 | // Performs parameterized substitutions on a string. Throws an | |
70 | // exception if any parameter is unmatched. | |
71 | // template: | |
72 | // a string with expressions in the form `${key}` to be replaced or | |
73 | // `${key:format}` which specifies a format function. keys are case-sensitive. | |
74 | // map: | |
75 | // hash to search for substitutions | |
76 | // transform: | |
77 | // a function to process all parameters before substitution takes | |
78 | // place, e.g. mylib.encodeXML | |
79 | // thisObject: | |
80 | // where to look for optional format function; default to the global | |
81 | // namespace | |
82 | // example: | |
83 | // Substitutes two expressions in a string from an Array or Object | |
84 | // | // returns "File 'foo.html' is not found in directory '/temp'." | |
85 | // | // by providing substitution data in an Array | |
86 | // | dojo.string.substitute( | |
87 | // | "File '${0}' is not found in directory '${1}'.", | |
88 | // | ["foo.html","/temp"] | |
89 | // | ); | |
90 | // | | |
91 | // | // also returns "File 'foo.html' is not found in directory '/temp'." | |
92 | // | // but provides substitution data in an Object structure. Dotted | |
93 | // | // notation may be used to traverse the structure. | |
94 | // | dojo.string.substitute( | |
95 | // | "File '${name}' is not found in directory '${info.dir}'.", | |
96 | // | { name: "foo.html", info: { dir: "/temp" } } | |
97 | // | ); | |
98 | // example: | |
99 | // Use a transform function to modify the values: | |
100 | // | // returns "file 'foo.html' is not found in directory '/temp'." | |
101 | // | dojo.string.substitute( | |
102 | // | "${0} is not found in ${1}.", | |
103 | // | ["foo.html","/temp"], | |
104 | // | function(str){ | |
105 | // | // try to figure out the type | |
106 | // | var prefix = (str.charAt(0) == "/") ? "directory": "file"; | |
107 | // | return prefix + " '" + str + "'"; | |
108 | // | } | |
109 | // | ); | |
110 | // example: | |
111 | // Use a formatter | |
112 | // | // returns "thinger -- howdy" | |
113 | // | dojo.string.substitute( | |
114 | // | "${0:postfix}", ["thinger"], null, { | |
115 | // | postfix: function(value, key){ | |
116 | // | return value + " -- howdy"; | |
117 | // | } | |
118 | // | } | |
119 | // | ); | |
120 | ||
121 | thisObject = thisObject || dojo.global; | |
122 | transform = transform ? | |
123 | dojo.hitch(thisObject, transform) : function(v){ return v; }; | |
124 | ||
125 | return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, | |
126 | function(match, key, format){ | |
127 | var value = dojo.getObject(key, false, map); | |
128 | if(format){ | |
129 | value = dojo.getObject(format, false, thisObject).call(thisObject, value, key); | |
130 | } | |
131 | return transform(value, key).toString(); | |
132 | }); // String | |
2f01fe57 | 133 | }; |
a089699c AD |
134 | |
135 | /*===== | |
136 | dojo.string.trim = function(str){ | |
137 | // summary: | |
138 | // Trims whitespace from both sides of the string | |
139 | // str: String | |
140 | // String to be trimmed | |
141 | // returns: String | |
142 | // Returns the trimmed string | |
143 | // description: | |
144 | // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript). | |
145 | // The short yet performant version of this function is dojo.trim(), | |
146 | // which is part of Dojo base. Uses String.prototype.trim instead, if available. | |
147 | return ""; // String | |
2f01fe57 | 148 | } |
a089699c AD |
149 | =====*/ |
150 | ||
151 | dojo.string.trim = String.prototype.trim ? | |
152 | dojo.trim : // aliasing to the native function | |
153 | function(str){ | |
154 | str = str.replace(/^\s+/, ''); | |
155 | for(var i = str.length - 1; i >= 0; i--){ | |
156 | if(/\S/.test(str.charAt(i))){ | |
157 | str = str.substring(0, i + 1); | |
158 | break; | |
159 | } | |
160 | } | |
161 | return str; | |
162 | }; | |
163 | ||
2f01fe57 | 164 | } |