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