4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-String'>/**
19 </span> * @class Ext.String
21 * A collection of useful static methods to deal with strings
26 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,
28 formatRe: /\{(\d+)\}/g,
29 escapeRegexRe: /([-.*+?^${}()|[\]\/\\])/g,
31 <span id='Ext-String-method-htmlEncode'> /**
32 </span> * Convert certain characters (&, <, >, and ") to their HTML character equivalents for literal display in web pages.
33 * @param {String} value The string to encode
34 * @return {String} The encoded text
37 htmlEncode: (function() {
42 '"': '&quot;'
43 }, keys = [], p, regex;
49 regex = new RegExp('(' + keys.join('|') + ')', 'g');
51 return function(value) {
52 return (!value) ? value : String(value).replace(regex, function(match, capture) {
53 return entities[capture];
58 <span id='Ext-String-method-htmlDecode'> /**
59 </span> * Convert certain characters (&, <, >, and ") from their HTML character equivalents.
60 * @param {String} value The string to decode
61 * @return {String} The decoded text
64 htmlDecode: (function() {
69 '&quot;': '"'
70 }, keys = [], p, regex;
76 regex = new RegExp('(' + keys.join('|') + '|&#[0-9]{1,5};' + ')', 'g');
78 return function(value) {
79 return (!value) ? value : String(value).replace(regex, function(match, capture) {
80 if (capture in entities) {
81 return entities[capture];
83 return String.fromCharCode(parseInt(capture.substr(2), 10));
89 <span id='Ext-String-method-urlAppend'> /**
90 </span> * Appends content to the query string of a URL, handling logic for whether to place
91 * a question mark or ampersand.
92 * @param {String} url The URL to append to.
93 * @param {String} string The content to append to the URL.
94 * @return (String) The resulting URL
96 urlAppend : function(url, string) {
97 if (!Ext.isEmpty(string)) {
98 return url + (url.indexOf('?') === -1 ? '?' : '&') + string;
104 <span id='Ext-String-method-trim'> /**
105 </span> * Trims whitespace from either end of a string, leaving spaces within the string intact. Example:
108 alert('-' + s + '-'); //alerts "- foo bar -"
109 alert('-' + Ext.String.trim(s) + '-'); //alerts "-foo bar-"
111 * @param {String} string The string to escape
112 * @return {String} The trimmed string
114 trim: function(string) {
115 return string.replace(Ext.String.trimRegex, "");
118 <span id='Ext-String-method-capitalize'> /**
119 </span> * Capitalize the given string
120 * @param {String} string
123 capitalize: function(string) {
124 return string.charAt(0).toUpperCase() + string.substr(1);
127 <span id='Ext-String-method-ellipsis'> /**
128 </span> * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
129 * @param {String} value The string to truncate
130 * @param {Number} length The maximum length to allow before truncating
131 * @param {Boolean} word True to try to find a common word break
132 * @return {String} The converted text
134 ellipsis: function(value, len, word) {
135 if (value && value.length > len) {
137 var vs = value.substr(0, len - 2),
138 index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
139 if (index !== -1 && index >= (len - 15)) {
140 return vs.substr(0, index) + "...";
143 return value.substr(0, len - 3) + "...";
148 <span id='Ext-String-method-escapeRegex'> /**
149 </span> * Escapes the passed string for use in a regular expression
150 * @param {String} string
153 escapeRegex: function(string) {
154 return string.replace(Ext.String.escapeRegexRe, "\\$1");
157 <span id='Ext-String-method-escape'> /**
158 </span> * Escapes the passed string for ' and \
159 * @param {String} string The string to escape
160 * @return {String} The escaped string
162 escape: function(string) {
163 return string.replace(Ext.String.escapeRe, "\\$1");
166 <span id='Ext-String-method-toggle'> /**
167 </span> * Utility function that allows you to easily switch a string between two alternating values. The passed value
168 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
169 * they are already different, the first value passed in is returned. Note that this method returns the new value
170 * but does not change the current string.
171 * <pre><code>
172 // alternate sort directions
173 sort = Ext.String.toggle(sort, 'ASC', 'DESC');
175 // instead of conditional logic:
176 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
177 </code></pre>
178 * @param {String} string The current string
179 * @param {String} value The value to compare to the current string
180 * @param {String} other The new value to use if the string already equals the first value passed in
181 * @return {String} The new value
183 toggle: function(string, value, other) {
184 return string === value ? other : value;
187 <span id='Ext-String-method-leftPad'> /**
188 </span> * Pads the left side of a string with a specified character. This is especially useful
189 * for normalizing number and date strings. Example usage:
191 * <pre><code>
192 var s = Ext.String.leftPad('123', 5, '0');
193 // s now contains the string: '00123'
194 </code></pre>
195 * @param {String} string The original string
196 * @param {Number} size The total length of the output string
197 * @param {String} character (optional) The character with which to pad the original string (defaults to empty string " ")
198 * @return {String} The padded string
200 leftPad: function(string, size, character) {
201 var result = String(string);
202 character = character || " ";
203 while (result.length < size) {
204 result = character + result;
209 <span id='Ext-String-method-format'> /**
210 </span> * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
211 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
212 * <pre><code>
213 var cls = 'my-class', text = 'Some text';
214 var s = Ext.String.format('&lt;div class="{0}">{1}&lt;/div>', cls, text);
215 // s now contains the string: '&lt;div class="my-class">Some text&lt;/div>'
216 </code></pre>
217 * @param {String} string The tokenized string to be formatted
218 * @param {String} value1 The value to replace token {0}
219 * @param {String} value2 Etc...
220 * @return {String} The formatted string
222 format: function(format) {
223 var args = Ext.Array.toArray(arguments, 1);
224 return format.replace(Ext.String.formatRe, function(m, i) {
229 <span id='Ext-String-method-repeat'> /**
230 </span> * Returns a string with a specified number of repititions a given string pattern.
231 * The pattern be separated by a different string.
233 * var s = Ext.String.repeat('---', 4); // = '------------'
234 * var t = Ext.String.repeat('--', 3, '/'); // = '--/--/--'
236 * @param {String} pattern The pattern to repeat.
237 * @param {Number} count The number of times to repeat the pattern (may be 0).
238 * @param {String} sep An option string to separate each pattern.
240 repeat: function(pattern, count, sep) {
241 for (var buf = [], i = count; i--; ) {
244 return buf.join(sep || '');