e7272dc9a8552204ebfe0ab9495f7b99af263f96
[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     function unicodeClean() {
72         return this.replace(/[\s\S]/g,
73             function(character) {
74                 if (character.charCodeAt()< 256) {
75                   return character;
76                }
77                try {
78                     encodeURIComponent(character);
79                } catch(e) { 
80                   return '';
81                }
82                return character;
83             }
84         );
85     }
86   
87     
88 });
89
90 /**
91  * Utility function that allows you to easily switch a string between two alternating values.  The passed value
92  * is compared to the current string, and if they are equal, the other value that was passed in is returned.  If
93  * they are already different, the first value passed in is returned.  Note that this method returns the new value
94  * but does not change the current string.
95  * <pre><code>
96 // alternate sort directions
97 sort = sort.toggle('ASC', 'DESC');
98
99 // instead of conditional logic:
100 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
101 </code></pre>
102  * @param {String} value The value to compare to the current string
103  * @param {String} other The new value to use if the string already equals the first value passed in
104  * @return {String} The new value
105  */
106  
107 String.prototype.toggle = function(value, other){
108     return this == value ? other : value;
109 };