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