3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.util.Format
11
* Reusable data formatting functions
14
Ext.util.Format = function(){
15
var trimRe = /^\s+|\s+$/g;
18
* Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
19
* @param {String} value The string to truncate
20
* @param {Number} length The maximum length to allow before truncating
21
* @return {String} The converted text
23
ellipsis : function(value, len){
24
if(value && value.length > len){
25
return value.substr(0, len-3)+"...";
31
* Checks a reference and converts it to empty string if it is undefined
32
* @param {Mixed} value Reference to check
33
* @return {Mixed} Empty string if converted, otherwise the original value
35
undef : function(value){
36
return value !== undefined ? value : "";
40
* Checks a reference and converts it to the default value if it's empty
41
* @param {Mixed} value Reference to check
42
* @param {String} defaultValue The value to insert of it's undefined (defaults to "")
45
defaultValue : function(value, defaultValue){
46
return value !== undefined && value !== '' ? value : defaultValue;
50
* Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
51
* @param {String} value The string to encode
52
* @return {String} The encoded text
54
htmlEncode : function(value){
55
return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
59
* Convert certain characters (&, <, >, and ') from their HTML character equivalents.
60
* @param {String} value The string to decode
61
* @return {String} The decoded text
63
htmlDecode : function(value){
64
return !value ? value : String(value).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"').replace(/&/g, "&");
68
* Trims any whitespace from either side of a string
69
* @param {String} value The text to trim
70
* @return {String} The trimmed text
72
trim : function(value){
73
return String(value).replace(trimRe, "");
77
* Returns a substring from within an original string
78
* @param {String} value The original text
79
* @param {Number} start The start index of the substring
80
* @param {Number} length The length of the substring
81
* @return {String} The substring
83
substr : function(value, start, length){
84
return String(value).substr(start, length);
88
* Converts a string to all lower case letters
89
* @param {String} value The text to convert
90
* @return {String} The converted text
92
lowercase : function(value){
93
return String(value).toLowerCase();
97
* Converts a string to all upper case letters
98
* @param {String} value The text to convert
99
* @return {String} The converted text
101
uppercase : function(value){
102
return String(value).toUpperCase();
106
* Converts the first character only of a string to upper case
107
* @param {String} value The text to convert
108
* @return {String} The converted text
110
capitalize : function(value){
111
return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
115
call : function(value, fn){
116
if(arguments.length > 2){
117
var args = Array.prototype.slice.call(arguments, 2);
119
return eval(fn).apply(window, args);
121
return eval(fn).call(window, value);
126
* Format a number as US currency
127
* @param {Number/String} value The numeric value to format
128
* @return {String} The formatted currency string
130
usMoney : function(v){
131
v = (Math.round((v-0)*100))/100;
132
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
134
var ps = v.split('.');
136
var sub = ps[1] ? '.'+ ps[1] : '.00';
137
var r = /(\d+)(\d{3})/;
138
while (r.test(whole)) {
139
whole = whole.replace(r, '$1' + ',' + '$2');
142
if(v.charAt(0) == '-'){
143
return '-$' + v.substr(1);
149
* Parse a value into a formatted date using the specified format pattern.
150
* @param {String/Date} value The value to format (Strings must conform to the format expected by the javascript Date object's <a href="http://www.w3schools.com/jsref/jsref_parse.asp">parse()</a> method)
151
* @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
152
* @return {String} The formatted date string
154
date : function(v, format){
159
v = new Date(Date.parse(v));
161
return v.dateFormat(format || "m/d/Y");
165
* Returns a date rendering function that can be reused to apply a date format multiple times efficiently
166
* @param {String} format Any valid date format string
167
* @return {Function} The date formatting function
169
dateRenderer : function(format){
171
return Ext.util.Format.date(v, format);
176
stripTagsRE : /<\/?[^>]+>/gi,
179
* Strips all HTML tags
180
* @param {Mixed} value The text from which to strip tags
181
* @return {String} The stripped text
183
stripTags : function(v){
184
return !v ? v : String(v).replace(this.stripTagsRE, "");
187
stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
190
* Strips all script tags
191
* @param {Mixed} value The text from which to strip script tags
192
* @return {String} The stripped text
194
stripScripts : function(v){
195
return !v ? v : String(v).replace(this.stripScriptsRe, "");
199
* Simple format for a file size (xxx bytes, xxx KB, xxx MB)
200
* @param {Number/String} size The numeric value to format
201
* @return {String} The formatted file size
203
fileSize : function(size){
205
return size + " bytes";
206
} else if(size < 1048576) {
207
return (Math.round(((size*10) / 1024))/10) + " KB";
209
return (Math.round(((size*10) / 1048576))/10) + " MB";
215
return function(v, a){
217
fns[a] = new Function('v', 'return v ' + a + ';');
224
return v === undefined || v === null ? '' : v.replace(/\n/g, '<br/>');
b'\\ No newline at end of file'