initial import
[roojs1] / 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 Roo.applyIf(String, {
13     
14     /** @scope String */
15     
16     /**
17      * Escapes the passed string for ' and \
18      * @param {String} string The string to escape
19      * @return {String} The escaped string
20      * @static
21      */
22     escape : function(string) {
23         return string.replace(/('|\\)/g, "\\$1");
24     },
25
26     /**
27      * Pads the left side of a string with a specified character.  This is especially useful
28      * for normalizing number and date strings.  Example usage:
29      * <pre><code>
30 var s = String.leftPad('123', 5, '0');
31 // s now contains the string: '00123'
32 </code></pre>
33      * @param {String} string The original string
34      * @param {Number} size The total length of the output string
35      * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
36      * @return {String} The padded string
37      * @static
38      */
39     leftPad : function (val, size, ch) {
40         var result = new String(val);
41         if(ch === null || ch === undefined || ch === '') {
42             ch = " ";
43         }
44         while (result.length < size) {
45             result = ch + result;
46         }
47         return result;
48     },
49
50     /**
51      * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens.  Each
52      * token must be unique, and must increment in the format {0}, {1}, etc.  Example usage:
53      * <pre><code>
54 var cls = 'my-class', text = 'Some text';
55 var s = String.format('<div class="{0}">{1}</div>', cls, text);
56 // s now contains the string: '<div class="my-class">Some text</div>'
57 </code></pre>
58      * @param {String} string The tokenized string to be formatted
59      * @param {String} value1 The value to replace token {0}
60      * @param {String} value2 Etc...
61      * @return {String} The formatted string
62      * @static
63      */
64     format : function(format){
65         var args = Array.prototype.slice.call(arguments, 1);
66         return format.replace(/\{(\d+)\}/g, function(m, i){
67             return Roo.util.Format.htmlEncode(args[i]);
68         });
69     }
70 });
71
72 /**
73  * Utility function that allows you to easily switch a string between two alternating values.  The passed value
74  * is compared to the current string, and if they are equal, the other value that was passed in is returned.  If
75  * they are already different, the first value passed in is returned.  Note that this method returns the new value
76  * but does not change the current string.
77  * <pre><code>
78 // alternate sort directions
79 sort = sort.toggle('ASC', 'DESC');
80
81 // instead of conditional logic:
82 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
83 </code></pre>
84  * @param {String} value The value to compare to the current string
85  * @param {String} other The new value to use if the string already equals the first value passed in
86  * @return {String} The new value
87  */
88  
89 String.prototype.toggle = function(value, other){
90     return this == value ? other : value;
91 };