4 * A collection of useful static methods to deal with strings
9 trimRegex: /^[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]+|[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]+$/g,
11 formatRe: /\{(\d+)\}/g,
12 escapeRegexRe: /([-.*+?^${}()|[\]\/\\])/g,
15 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
16 * @param {String} value The string to encode
17 * @return {String} The encoded text
20 htmlEncode: (function() {
26 }, keys = [], p, regex;
32 regex = new RegExp('(' + keys.join('|') + ')', 'g');
34 return function(value) {
35 return (!value) ? value : String(value).replace(regex, function(match, capture) {
36 return entities[capture];
42 * Convert certain characters (&, <, >, and ') from their HTML character equivalents.
43 * @param {String} value The string to decode
44 * @return {String} The decoded text
47 htmlDecode: (function() {
53 }, keys = [], p, regex;
59 regex = new RegExp('(' + keys.join('|') + '|&#[0-9]{1,5};' + ')', 'g');
61 return function(value) {
62 return (!value) ? value : String(value).replace(regex, function(match, capture) {
63 if (capture in entities) {
64 return entities[capture];
66 return String.fromCharCode(parseInt(capture.substr(2), 10));
73 * Appends content to the query string of a URL, handling logic for whether to place
74 * a question mark or ampersand.
75 * @param {String} url The URL to append to.
76 * @param {String} string The content to append to the URL.
77 * @return (String) The resulting URL
79 urlAppend : function(url, string) {
80 if (!Ext.isEmpty(string)) {
81 return url + (url.indexOf('?') === -1 ? '?' : '&') + string;
88 * Trims whitespace from either end of a string, leaving spaces within the string intact. Example:
91 alert('-' + s + '-'); //alerts "- foo bar -"
92 alert('-' + Ext.String.trim(s) + '-'); //alerts "-foo bar-"
94 * @param {String} string The string to escape
95 * @return {String} The trimmed string
97 trim: function(string) {
98 return string.replace(Ext.String.trimRegex, "");
102 * Capitalize the given string
103 * @param {String} string
106 capitalize: function(string) {
107 return string.charAt(0).toUpperCase() + string.substr(1);
111 * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
112 * @param {String} value The string to truncate
113 * @param {Number} length The maximum length to allow before truncating
114 * @param {Boolean} word True to try to find a common word break
115 * @return {String} The converted text
117 ellipsis: function(value, len, word) {
118 if (value && value.length > len) {
120 var vs = value.substr(0, len - 2),
121 index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
122 if (index !== -1 && index >= (len - 15)) {
123 return vs.substr(0, index) + "...";
126 return value.substr(0, len - 3) + "...";
132 * Escapes the passed string for use in a regular expression
133 * @param {String} string
136 escapeRegex: function(string) {
137 return string.replace(Ext.String.escapeRegexRe, "\\$1");
141 * Escapes the passed string for ' and \
142 * @param {String} string The string to escape
143 * @return {String} The escaped string
145 escape: function(string) {
146 return string.replace(Ext.String.escapeRe, "\\$1");
150 * Utility function that allows you to easily switch a string between two alternating values. The passed value
151 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
152 * they are already different, the first value passed in is returned. Note that this method returns the new value
153 * but does not change the current string.
155 // alternate sort directions
156 sort = Ext.String.toggle(sort, 'ASC', 'DESC');
158 // instead of conditional logic:
159 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
161 * @param {String} string The current string
162 * @param {String} value The value to compare to the current string
163 * @param {String} other The new value to use if the string already equals the first value passed in
164 * @return {String} The new value
166 toggle: function(string, value, other) {
167 return string === value ? other : value;
171 * Pads the left side of a string with a specified character. This is especially useful
172 * for normalizing number and date strings. Example usage:
175 var s = Ext.String.leftPad('123', 5, '0');
176 // s now contains the string: '00123'
178 * @param {String} string The original string
179 * @param {Number} size The total length of the output string
180 * @param {String} character (optional) The character with which to pad the original string (defaults to empty string " ")
181 * @return {String} The padded string
183 leftPad: function(string, size, character) {
184 var result = String(string);
185 character = character || " ";
186 while (result.length < size) {
187 result = character + result;
193 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
194 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
196 var cls = 'my-class', text = 'Some text';
197 var s = Ext.String.format('<div class="{0}">{1}</div>', cls, text);
198 // s now contains the string: '<div class="my-class">Some text</div>'
200 * @param {String} string The tokenized string to be formatted
201 * @param {String} value1 The value to replace token {0}
202 * @param {String} value2 Etc...
203 * @return {String} The formatted string
205 format: function(format) {
206 var args = Ext.Array.toArray(arguments, 1);
207 return format.replace(Ext.String.formatRe, function(m, i) {