generate gjs documentation, run under gjs
[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
12 // usage:
13 // Seed.include('String.js');
14
15 const XObject = imports.XObject.XObject;
16
17
18 XObject.extend(String,
19     {
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         /**
58          * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens.  Each
59          * token must be unique, and must increment in the format {0}, {1}, etc.  Example usage:
60          * <pre><code>
61     var cls = 'my-class', text = 'Some text';
62     var s = String.format('<div class="{0}">{1}</div>', cls, text);
63     // s now contains the string: '<div class="my-class">Some text</div>'
64     </code></pre>
65          * @p aram {String} string The tokenized string to be formatted
66          * @param {String} value1 The value to replace token {0}
67          * @param {String} value2 Etc...
68          * @return {String} The formatted string
69          * @static
70          */
71         format : function(format){
72             var args = Array.prototype.slice.call(arguments, 1);
73             return format.replace(/\{(\d+)\}/g, function(m, i){
74                 return args[i];
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      * 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 });