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 //imports['String.js'].load(String);
14 load = false; // declare global for gnome langs.
24 * Escapes the passed string for ' and \
25 * @param {String} string The string to escape
26 * @return {String} The escaped string
29 escape : function(string) {
30 return string.replace(/('|\\)/g, "\\$1");
34 * Pads the left side of a string with a specified character. This is especially useful
35 * for normalizing number and date strings. Example usage:
37 var s = String.leftPad('123', 5, '0');
38 // s now contains the string: '00123'
40 * @param {String} string The original string
41 * @param {Number} size The total length of the output string
42 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
43 * @return {String} The padded string
46 leftPad : function (val, size, ch) {
47 var result = new String(val);
48 if(ch === null || ch === undefined || ch === '') {
51 while (result.length < size) {
57 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
58 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
60 var cls = 'my-class', text = 'Some text';
61 var s = String.format('<div class="{0}">{1}</div>', cls, text);
62 // s now contains the string: '<div class="my-class">Some text</div>'
64 * @p aram {String} string The tokenized string to be formatted
65 * @param {String} value1 The value to replace token {0}
66 * @param {String} value2 Etc...
67 * @return {String} The formatted string
70 format : function(format){
71 var args = Array.prototype.slice.call(arguments, 1);
72 return format.replace(/\{(\d+)\}/g, function(m, i){
79 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
80 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
82 var cls = 'my-class', text = 'Some text';
83 var s = String.format('<div class="{0}">{1}</div>', cls, text);
84 // s now contains the string: '<div class="my-class">Some text</div>'
86 * @param {String} string The tokenized string to be formatted
87 * @param {String} value1 The value to replace token {0}
88 * @param {String} value2 Etc...
89 * @return {String} The formatted string, all arguments will be htmlEncoded.
92 htmlFormat : function(format){
93 var args = Array.prototype.slice.call(arguments, 1);
94 return format.replace(/\{(\d+)\}/g, function(m, i){
95 return this.htmlEncode(args[i]);
100 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
101 * @param {String} value The string to encode
102 * @return {String} The encoded text
104 htmlEncode : function(value){
105 return !value ? value :
106 String(value).replace(/&/g, "&"
107 ).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
114 * Utility function that allows you to easily switch a string between two alternating values. The passed value
115 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
116 * they are already different, the first value passed in is returned. Note that this method returns the new value
117 * but does not change the current string.
119 // alternate sort directions
120 sort = sort.toggle('ASC', 'DESC');
122 // instead of conditional logic:
123 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
125 * @param {String} value The value to compare to the current string
126 * @param {String} other The new value to use if the string already equals the first value passed in
127 * @return {String} The new value
130 var stringPrototype = {
131 toggle : function(value, other){
132 return this == value ? other : value;
135 trim : function (toTrim) {
136 var out = this.ltrim(toTrim);
137 out = out.rtrim(toTrim);
141 ltrim : function (toTrim) {
142 if (this.substr(0, toTrim.length) == toTrim) {
143 return this.slice(toTrim.length);
149 rtrim : function (toTrim) {
150 if (this.substr(this.length - toTrim.length) == toTrim) {
151 return this.slice(0, this.length - toTrim.length);
162 load = function(ar) {
164 imports.lang.copyPropertiesNoOverwrite(string,ar);
165 imports.lang.copyPropertiesNoOverwrite(stringPrototype,ar.prototype);
168 // non imports version.
169 for(i in stringPrototype) {
170 if (!String.prototype[i]) {
171 String.prototype[i] = stringPrototype[i];
176 String[i] = string[i];