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