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 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, """);
113 * Utility function that allows you to easily switch a string between two alternating values. The passed value
114 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
115 * they are already different, the first value passed in is returned. Note that this method returns the new value
116 * but does not change the current string.
118 // alternate sort directions
119 sort = sort.toggle('ASC', 'DESC');
121 // instead of conditional logic:
122 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
124 * @param {String} value The value to compare to the current string
125 * @param {String} other The new value to use if the string already equals the first value passed in
126 * @return {String} The new value
129 XObject.extend(String.prototype, {
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);