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">
12 //String = imports.String.String;
13 XObject = imports.XObject.XObject;
15 String = XObject.extend(String,
21 * Escapes the passed string for ' and \
22 * @param {String} string The string to escape
23 * @return {String} The escaped string
26 escape : function(string) {
27 return string.replace(/('|\\)/g, "\\$1");
31 * Pads the left side of a string with a specified character. This is especially useful
32 * for normalizing number and date strings. Example usage:
34 var s = String.leftPad('123', 5, '0');
35 // s now contains the string: '00123'
37 * @param {String} string The original string
38 * @param {Number} size The total length of the output string
39 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
40 * @return {String} The padded string
43 leftPad : function (val, size, ch) {
44 var result = new String(val);
45 if(ch === null || ch === undefined || ch === '') {
48 while (result.length < size) {
54 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
55 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
57 var cls = 'my-class', text = 'Some text';
58 var s = String.format('<div class="{0}">{1}</div>', cls, text);
59 // s now contains the string: '<div class="my-class">Some text</div>'
61 * @p aram {String} string The tokenized string to be formatted
62 * @param {String} value1 The value to replace token {0}
63 * @param {String} value2 Etc...
64 * @return {String} The formatted string
67 format : function(format){
68 var args = Array.prototype.slice.call(arguments, 1);
69 return format.replace(/\{(\d+)\}/g, function(m, i){
76 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
77 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
79 var cls = 'my-class', text = 'Some text';
80 var s = String.format('<div class="{0}">{1}</div>', cls, text);
81 // s now contains the string: '<div class="my-class">Some text</div>'
83 * @param {String} string The tokenized string to be formatted
84 * @param {String} value1 The value to replace token {0}
85 * @param {String} value2 Etc...
86 * @return {String} The formatted string, all arguments will be htmlEncoded.
89 htmlFormat : function(format){
90 var args = Array.prototype.slice.call(arguments, 1);
91 return format.replace(/\{(\d+)\}/g, function(m, i){
92 return this.htmlEncode(args[i]);
97 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
98 * @param {String} value The string to encode
99 * @return {String} The encoded text
101 htmlEncode : function(value){
102 return !value ? value :
103 String(value).replace(/&/g, "&"
104 ).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
112 * Utility function that allows you to easily switch a string between two alternating values. The passed value
113 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
114 * they are already different, the first value passed in is returned. Note that this method returns the new value
115 * but does not change the current string.
117 // alternate sort directions
118 sort = sort.toggle('ASC', 'DESC');
120 // instead of conditional logic:
121 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
123 * @param {String} value The value to compare to the current string
124 * @param {String} other The new value to use if the string already equals the first value passed in
125 * @return {String} The new value
128 XObject.extend(String.prototype, {
130 toggle : function(value, other){
131 return this == value ? other : value;
134 trim : function (toTrim) {
135 var out = this.ltrim(toTrim);
136 out = out.rtrim(toTrim);
140 ltrim : function (toTrim) {
141 if (this.substr(0, toTrim.length) == toTrim) {
142 return this.slice(toTrim.length);
148 rtrim : function (toTrim) {
149 if (this.substr(this.length - toTrim.length) == toTrim) {
150 return this.slice(0, this.length - toTrim.length);