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");
7 // summary: String utilities for Dojo
11 dojo.string.pad = function(/*String*/text, /*int*/size, /*String?*/ch, /*boolean?*/end){
13 // Pad a string to guarantee that it is at least `size` length by
14 // filling with the character `ch` at either the start or end of the
15 // string. Pads at the start, by default.
16 // text: the string to pad
17 // size: length to provide padding
18 // ch: character to pad, defaults to '0'
19 // end: adds padding at the end if true, otherwise pads at start
21 var out = String(text);
25 while(out.length < size){
35 dojo.string.substitute = function( /*String*/template,
37 /*Function?*/transform,
38 /*Object?*/thisObject){
40 // Performs parameterized substitutions on a string. Throws an
41 // exception if any parameter is unmatched.
44 // | dojo.string.substitute("File '${0}' is not found in directory '${1}'.",["foo.html","/temp"]);
45 // | dojo.string.substitute("File '${name}' is not found in directory '${info.dir}'.",
46 // | {name: "foo.html", info: {dir: "/temp"}});
48 // | "File 'foo.html' is not found in directory '/temp'."
50 // a string with expressions in the form `${key}` to be replaced or
51 // `${key:format}` which specifies a format function.
52 // map: hash to search for substitutions
54 // a function to process all parameters before substitution takes
55 // place, e.g. dojo.string.encodeXML
57 // where to look for optional format function; default to the global
60 return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match, key, format){
61 var value = dojo.getObject(key,false,map);
62 if(format){ value = dojo.getObject(format,false,thisObject)(value);}
63 if(transform){ value = transform(value, key); }
64 return value.toString();
68 dojo.string.trim = function(/*String*/ str){
69 // summary: trims whitespaces from both sides of the string
71 // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
72 // The short yet performant version of this function is
73 // dojo.trim(), which is part of Dojo base.
74 str = str.replace(/^\s+/, '');
75 for(var i = str.length - 1; i > 0; i--){
76 if(/\S/.test(str.charAt(i))){
77 str = str.substring(0, i + 1);