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