Remove Roo dependancy, add Elliot's changes
[gnome.introspection-doc-generator] / String.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 // usage:
12 //imports['String.js'].load(String);
13 if (imports) {
14     load = false; // declare global for gnome langs.
15 }
16 (function () {
17     
18
19     var string =  {
20     
21         /** @scope String */
22         
23         /**
24          * Escapes the passed string for ' and \
25          * @param {String} string The string to escape
26          * @return {String} The escaped string
27          * @static
28          */
29         escape : function(string) {
30             return string.replace(/('|\\)/g, "\\$1");
31         },
32
33         /**
34          * Pads the left side of a string with a specified character.  This is especially useful
35          * for normalizing number and date strings.  Example usage:
36          * <pre><code>
37     var s = String.leftPad('123', 5, '0');
38     // s now contains the string: '00123'
39     </code></pre>
40          * @param {String} string The original string
41          * @param {Number} size The total length of the output string
42          * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
43          * @return {String} The padded string
44          * @static
45          */
46         leftPad : function (val, size, ch) {
47             var result = new String(val);
48             if(ch === null || ch === undefined || ch === '') {
49                 ch = " ";
50             }
51             while (result.length < size) {
52                 result = ch + result;
53             }
54             return result;
55         },
56          /**
57          * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens.  Each
58          * token must be unique, and must increment in the format {0}, {1}, etc.  Example usage:
59          * <pre><code>
60     var cls = 'my-class', text = 'Some text';
61     var s = String.format('<div class="{0}">{1}</div>', cls, text);
62     // s now contains the string: '<div class="my-class">Some text</div>'
63     </code></pre>
64          * @p aram {String} string The tokenized string to be formatted
65          * @param {String} value1 The value to replace token {0}
66          * @param {String} value2 Etc...
67          * @return {String} The formatted string
68          * @static
69          */
70         format : function(format){
71             var args = Array.prototype.slice.call(arguments, 1);
72             return format.replace(/\{(\d+)\}/g, function(m, i){
73                 return args[i];
74             });
75         },
76         
77
78         /**
79          * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens.  Each
80          * token must be unique, and must increment in the format {0}, {1}, etc.  Example usage:
81          * <pre><code>
82     var cls = 'my-class', text = 'Some text';
83     var s = String.format('<div class="{0}">{1}</div>', cls, text);
84     // s now contains the string: '<div class="my-class">Some text</div>'
85     </code></pre>
86          * @param {String} string The tokenized string to be formatted
87          * @param {String} value1 The value to replace token {0}
88          * @param {String} value2 Etc...
89          * @return {String} The formatted string, all arguments will be htmlEncoded.
90          * @static
91          */
92         htmlFormat : function(format){
93             var args = Array.prototype.slice.call(arguments, 1);
94             return format.replace(/\{(\d+)\}/g, function(m, i){
95                 return this.htmlEncode(args[i]);
96             });
97         },
98         
99         /**
100          * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
101          * @param {String} value The string to encode
102          * @return {String} The encoded text
103          */
104         htmlEncode : function(value){
105             return !value ? value : 
106                 String(value).replace(/&/g, "&amp;"
107                     ).replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
108         }
109         
110         
111     };
112
113     /**
114      * Utility function that allows you to easily switch a string between two alternating values.  The passed value
115      * is compared to the current string, and if they are equal, the other value that was passed in is returned.  If
116      * they are already different, the first value passed in is returned.  Note that this method returns the new value
117      * but does not change the current string.
118      * <pre><code>
119     // alternate sort directions
120     sort = sort.toggle('ASC', 'DESC');
121
122     // instead of conditional logic:
123     sort = (sort == 'ASC' ? 'DESC' : 'ASC');
124     </code></pre>
125      * @param {String} value The value to compare to the current string
126      * @param {String} other The new value to use if the string already equals the first value passed in
127      * @return {String} The new value
128      */
129      
130     var stringPrototype = {
131         toggle : function(value, other){
132             return this == value ? other : value;
133         },
134         
135         trim : function (toTrim) {
136             var out = this.ltrim(toTrim);
137             out = out.rtrim(toTrim);
138             return out;
139         },
140         
141         ltrim : function (toTrim) {
142             if (this.substr(0, toTrim.length) == toTrim) {
143                 return this.slice(toTrim.length);
144             }
145             
146             return this;
147         },
148         
149         rtrim : function (toTrim) {
150             if (this.substr(this.length - toTrim.length) == toTrim) {
151                 return this.slice(0, this.length - toTrim.length);
152             }
153        
154             return this;
155         }
156  
157     };
158     
159     
160     
161     if (imports) {
162         load = function(ar) {
163             String = ar;
164             imports.lang.copyPropertiesNoOverwrite(string,ar);
165             imports.lang.copyPropertiesNoOverwrite(stringPrototype,ar.prototype);
166         };
167     } else {
168         // non imports version.
169         for(i in stringPrototype) {
170             if (!String.prototype[i]) {
171                 String.prototype[i] = stringPrototype[i];
172             }
173         }
174         for(i in string) {
175             if (!String[i]) {
176                 String[i] = string[i];
177             }
178         }
179     }
180 })();