]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/string.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / string.js
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 /*=====
6 dojo.string = { 
7         // summary: String utilities for Dojo
8 };
9 =====*/
10
11 dojo.string.pad = function(/*String*/text, /*int*/size, /*String?*/ch, /*boolean?*/end){
12         // summary:
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
20
21         var out = String(text);
22         if(!ch){
23                 ch = '0';
24         }
25         while(out.length < size){
26                 if(end){
27                         out += ch;
28                 }else{
29                         out = ch + out;
30                 }
31         }
32         return out;     // String
33 };
34
35 dojo.string.substitute = function(      /*String*/template, 
36                                                                         /*Object|Array*/map, 
37                                                                         /*Function?*/transform, 
38                                                                         /*Object?*/thisObject){
39         // summary:
40         //              Performs parameterized substitutions on a string. Throws an
41         //              exception if any parameter is unmatched.
42         // description:
43         //              For example,
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"}});
47         //              both return
48         //              |       "File 'foo.html' is not found in directory '/temp'."
49         // template: 
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
53         // transform: 
54         //              a function to process all parameters before substitution takes
55         //              place, e.g. dojo.string.encodeXML
56         // thisObject: 
57         //              where to look for optional format function; default to the global
58         //              namespace
59
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();
65         }); // string
66 };
67
68 dojo.string.trim = function(/*String*/ str){
69         // summary: trims whitespaces from both sides of the string
70         // description:
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);
78                         break;
79                 }
80         }
81         return str;     // String
82 };
83
84 }