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 const XObject = imports.XObject.XObject;
17 XObject.extend(String,
23 * Escapes the passed string for ' and \
24 * @param {String} string The string to escape
25 * @return {String} The escaped string
28 escape : function(string) {
29 return string.replace(/('|\\)/g, "\\$1");
33 * Pads the left side of a string with a specified character. This is especially useful
34 * for normalizing number and date strings. Example usage:
36 var s = String.leftPad('123', 5, '0');
37 // s now contains the string: '00123'
39 * @param {String} string The original string
40 * @param {Number} size The total length of the output string
41 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
42 * @return {String} The padded string
45 leftPad : function (val, size, ch) {
46 var result = new String(val);
47 if(ch === null || ch === undefined || ch === '') {
50 while (result.length < size) {
56 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
57 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
59 var cls = 'my-class', text = 'Some text';
60 var s = String.format('<div class="{0}">{1}</div>', cls, text);
61 // s now contains the string: '<div class="my-class">Some text</div>'
63 * @p aram {String} string The tokenized string to be formatted
64 * @param {String} value1 The value to replace token {0}
65 * @param {String} value2 Etc...
66 * @return {String} The formatted string
69 format : function(format){
70 var args = Array.prototype.slice.call(arguments, 1);
71 return format.replace(/\{(\d+)\}/g, function(m, i){
78 * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
79 * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
81 var cls = 'my-class', text = 'Some text';
82 var s = String.format('<div class="{0}">{1}</div>', cls, text);
83 // s now contains the string: '<div class="my-class">Some text</div>'
85 * @param {String} string The tokenized string to be formatted
86 * @param {String} value1 The value to replace token {0}
87 * @param {String} value2 Etc...
88 * @return {String} The formatted string, all arguments will be htmlEncoded.
91 htmlFormat : function(format){
92 var args = Array.prototype.slice.call(arguments, 1);
93 return format.replace(/\{(\d+)\}/g, function(m, i){
94 return this.htmlEncode(args[i]);
99 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
100 * @param {String} value The string to encode
101 * @return {String} The encoded text
103 htmlEncode : function(value){
104 return !value ? value :
105 String(value).replace(/&/g, "&"
106 ).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 XObject.extend(String.prototype, {
132 toggle : function(value, other){
133 return this == value ? other : value;
136 trim : function (toTrim) {
137 var out = this.ltrim(toTrim);
138 out = out.rtrim(toTrim);
142 ltrim : function (toTrim) {
143 if (this.substr(0, toTrim.length) == toTrim) {
144 return this.slice(toTrim.length);
150 rtrim : function (toTrim) {
151 if (this.substr(this.length - toTrim.length) == toTrim) {
152 return this.slice(0, this.length - toTrim.length);