1 define("dojo/string", [
2 "./_base/kernel", // kernel.global
4 ], function(kernel, lang){
11 // String utilities for Dojo
13 lang.setObject("dojo.string", string);
15 string.rep = function(/*String*/str, /*Integer*/num){
17 // Efficiently replicate a string `n` times.
19 // the string to replicate
21 // number of times to replicate the string
23 if(num <= 0 || !str){ return ""; }
30 if(!(num >>= 1)){ break; }
33 return buf.join(""); // String
36 string.pad = function(/*String*/text, /*Integer*/size, /*String?*/ch, /*Boolean?*/end){
38 // Pad a string to guarantee that it is at least `size` length by
39 // filling with the character `ch` at either the start or end of the
40 // string. Pads at the start, by default.
44 // length to provide padding
46 // character to pad, defaults to '0'
48 // adds padding at the end if true, otherwise pads at start
50 // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++".
51 // | string.pad("Dojo", 10, "+", true);
56 var out = String(text),
57 pad = string.rep(ch, Math.ceil((size - out.length) / ch.length));
58 return end ? out + pad : pad + out; // String
61 string.substitute = function( /*String*/ template,
63 /*Function?*/ transform,
64 /*Object?*/ thisObject){
66 // Performs parameterized substitutions on a string. Throws an
67 // exception if any parameter is unmatched.
69 // a string with expressions in the form `${key}` to be replaced or
70 // `${key:format}` which specifies a format function. keys are case-sensitive.
72 // hash to search for substitutions
74 // a function to process all parameters before substitution takes
75 // place, e.g. mylib.encodeXML
77 // where to look for optional format function; default to the global
80 // Substitutes two expressions in a string from an Array or Object
81 // | // returns "File 'foo.html' is not found in directory '/temp'."
82 // | // by providing substitution data in an Array
83 // | string.substitute(
84 // | "File '${0}' is not found in directory '${1}'.",
85 // | ["foo.html","/temp"]
88 // | // also returns "File 'foo.html' is not found in directory '/temp'."
89 // | // but provides substitution data in an Object structure. Dotted
90 // | // notation may be used to traverse the structure.
91 // | string.substitute(
92 // | "File '${name}' is not found in directory '${info.dir}'.",
93 // | { name: "foo.html", info: { dir: "/temp" } }
96 // Use a transform function to modify the values:
97 // | // returns "file 'foo.html' is not found in directory '/temp'."
98 // | string.substitute(
99 // | "${0} is not found in ${1}.",
100 // | ["foo.html","/temp"],
102 // | // try to figure out the type
103 // | var prefix = (str.charAt(0) == "/") ? "directory": "file";
104 // | return prefix + " '" + str + "'";
109 // | // returns "thinger -- howdy"
110 // | string.substitute(
111 // | "${0:postfix}", ["thinger"], null, {
112 // | postfix: function(value, key){
113 // | return value + " -- howdy";
118 thisObject = thisObject || kernel.global;
119 transform = transform ?
120 lang.hitch(thisObject, transform) : function(v){ return v; };
122 return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g,
123 function(match, key, format){
124 var value = lang.getObject(key, false, map);
126 value = lang.getObject(format, false, thisObject).call(thisObject, value, key);
128 return transform(value, key).toString();
132 string.trim = String.prototype.trim ?
133 lang.trim : // aliasing to the native function
135 str = str.replace(/^\s+/, '');
136 for(var i = str.length - 1; i >= 0; i--){
137 if(/\S/.test(str.charAt(i))){
138 str = str.substring(0, i + 1);
146 string.trim = function(str){
148 // Trims whitespace from both sides of the string
150 // String to be trimmed
152 // Returns the trimmed string
154 // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
155 // The short yet performant version of this function is dojo.trim(),
156 // which is part of Dojo base. Uses String.prototype.trim instead, if available.