4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
17 * Escapes the passed string for ' and \
18 * @param {String} string The string to escape
19 * @return {String} The escaped string
22 escape : function(string) {
23 return string.replace(/('|\\)/g, "\\$1");
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:
30 var s = String.leftPad('123', 5, '0');
31 // s now contains the string: '00123'
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
39 leftPad : function (val, size, ch) {
40 var result = new String(val);
41 if(ch === null || ch === undefined || ch === '') {
44 while (result.length < size) {
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:
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>'
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
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]);
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.
78 // alternate sort directions
79 sort = sort.toggle('ASC', 'DESC');
81 // instead of conditional logic:
82 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
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
89 String.prototype.toggle = function(value, other){
90 return this == value ? other : value;