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