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 // Seed.include('String.js');
13 XObject = imports.XObject.XObject;
16 String = XObject.extend(String,
22 * Escapes the passed string for ' and \
23 * @param {String} string The string to escape
24 * @return {String} The escaped string
27 escape : function(string) {
28 return string.replace(/('|\\)/g, "\\$1");
32 * Pads the left side of a string with a specified character. This is especially useful
33 * for normalizing number and date strings. Example usage:
35 var s = String.leftPad('123', 5, '0');
36 // s now contains the string: '00123'
38 * @param {String} string The original string
39 * @param {Number} size The total length of the output string
40 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
41 * @return {String} The padded string
44 leftPad : function (val, size, ch) {
45 var result = new String(val);
46 if(ch === null || ch === undefined || ch === '') {
49 while (result.length < size) {
55 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
56 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
58 var cls = 'my-class', text = 'Some text';
59 var s = String.format('<div class="{0}">{1}</div>', cls, text);
60 // s now contains the string: '<div class="my-class">Some text</div>'
62 * @p aram {String} string The tokenized string to be formatted
63 * @param {String} value1 The value to replace token {0}
64 * @param {String} value2 Etc...
65 * @return {String} The formatted string
68 format : function(format){
69 var args = Array.prototype.slice.call(arguments, 1);
70 return format.replace(/\{(\d+)\}/g, function(m, i){
77 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
78 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
80 var cls = 'my-class', text = 'Some text';
81 var s = String.format('<div class="{0}">{1}</div>', cls, text);
82 // s now contains the string: '<div class="my-class">Some text</div>'
84 * @param {String} string The tokenized string to be formatted
85 * @param {String} value1 The value to replace token {0}
86 * @param {String} value2 Etc...
87 * @return {String} The formatted string, all arguments will be htmlEncoded.
90 htmlFormat : function(format){
91 var args = Array.prototype.slice.call(arguments, 1);
92 return format.replace(/\{(\d+)\}/g, function(m, i){
93 return this.htmlEncode(args[i]);
98 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
99 * @param {String} value The string to encode
100 * @return {String} The encoded text
102 htmlEncode : function(value){
103 return !value ? value :
104 String(value).replace(/&/g, "&"
105 ).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
112 XObject.extend(String.prototype, {
115 * Utility function that allows you to easily switch a string between two alternating values. The passed value
116 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
117 * they are already different, the first value passed in is returned. Note that this method returns the new value
118 * but does not change the current string.
120 // alternate sort directions
121 sort = sort.toggle('ASC', 'DESC');
123 // instead of conditional logic:
124 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
126 * @param {String} value The value to compare to the current string
127 * @param {String} other The new value to use if the string already equals the first value passed in
128 * @return {String} The new value
131 toggle : function(value, other){
132 return this == value ? other : value;
135 * trim a string from the beginning and end
136 * This is NOT whitespace strip, it is prefix/suffix stripping
138 * @param {String} string to strip.
140 trim : function (toTrim) {
141 var out = this.ltrim(toTrim);
142 out = out.rtrim(toTrim);
146 * trim a string from the beginning
147 * This is NOT whitespace strip, it is prefix stripping
149 * @param {String} string to remove if it exists.
151 ltrim : function (toTrim) {
152 if (this.substr(0, toTrim.length) == toTrim) {
153 return this.slice(toTrim.length);
159 * trim a string from the end
160 * This is NOT whitespace strip, it is suffix stripping
162 * @param {String} string to remove if it exists.
164 rtrim : function (toTrim) {
165 if (this.substr(this.length - toTrim.length) == toTrim) {
166 return this.slice(0, this.length - toTrim.length);