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">
13 //String = imports.String.String;
14 XObject = imports.XObject.XObject;
16 String = XObject.extend(String,
18 // Seed.include('String.js');
19 XObject = imports.XObject.XObject;
22 XObject.extend(String,
23 >>>>>>> 134619d3049edce9b27810483e8011e5dfb783e0
29 * Escapes the passed string for ' and \
30 * @param {String} string The string to escape
31 * @return {String} The escaped string
34 escape : function(string) {
35 return string.replace(/('|\\)/g, "\\$1");
39 * Pads the left side of a string with a specified character. This is especially useful
40 * for normalizing number and date strings. Example usage:
42 var s = String.leftPad('123', 5, '0');
43 // s now contains the string: '00123'
45 * @param {String} string The original string
46 * @param {Number} size The total length of the output string
47 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
48 * @return {String} The padded string
51 leftPad : function (val, size, ch) {
52 var result = new String(val);
53 if(ch === null || ch === undefined || ch === '') {
56 while (result.length < size) {
62 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
63 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
65 var cls = 'my-class', text = 'Some text';
66 var s = String.format('<div class="{0}">{1}</div>', cls, text);
67 // s now contains the string: '<div class="my-class">Some text</div>'
69 * @p aram {String} string The tokenized string to be formatted
70 * @param {String} value1 The value to replace token {0}
71 * @param {String} value2 Etc...
72 * @return {String} The formatted string
75 format : function(format){
76 var args = Array.prototype.slice.call(arguments, 1);
77 return format.replace(/\{(\d+)\}/g, function(m, i){
84 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
85 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
87 var cls = 'my-class', text = 'Some text';
88 var s = String.format('<div class="{0}">{1}</div>', cls, text);
89 // s now contains the string: '<div class="my-class">Some text</div>'
91 * @param {String} string The tokenized string to be formatted
92 * @param {String} value1 The value to replace token {0}
93 * @param {String} value2 Etc...
94 * @return {String} The formatted string, all arguments will be htmlEncoded.
97 htmlFormat : function(format){
98 var args = Array.prototype.slice.call(arguments, 1);
99 return format.replace(/\{(\d+)\}/g, function(m, i){
100 return this.htmlEncode(args[i]);
105 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
106 * @param {String} value The string to encode
107 * @return {String} The encoded text
109 htmlEncode : function(value){
110 return !value ? value :
111 String(value).replace(/&/g, "&"
112 ).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
120 * Utility function that allows you to easily switch a string between two alternating values. The passed value
121 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
122 * they are already different, the first value passed in is returned. Note that this method returns the new value
123 * but does not change the current string.
125 // alternate sort directions
126 sort = sort.toggle('ASC', 'DESC');
128 // instead of conditional logic:
129 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
131 * @param {String} value The value to compare to the current string
132 * @param {String} other The new value to use if the string already equals the first value passed in
133 * @return {String} The new value
136 XObject.extend(String.prototype, {
138 toggle : function(value, other){
139 return this == value ? other : value;
142 trim : function (toTrim) {
143 var out = this.ltrim(toTrim);
144 out = out.rtrim(toTrim);
148 ltrim : function (toTrim) {
149 if (this.substr(0, toTrim.length) == toTrim) {
150 return this.slice(toTrim.length);
156 rtrim : function (toTrim) {
157 if (this.substr(this.length - toTrim.length) == toTrim) {
158 return this.slice(0, this.length - toTrim.length);