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">
13 * @class Roo.util.Format
14 * Reusable data formatting functions
17 Roo.util.Format = function(){
18 var trimRe = /^\s+|\s+$/g;
21 * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
22 * @param {String} value The string to truncate
23 * @param {Number} length The maximum length to allow before truncating
24 * @return {String} The converted text
26 ellipsis : function(value, len){
27 if(value && value.length > len){
28 return value.substr(0, len-3)+"...";
34 * Checks a reference and converts it to empty string if it is undefined
35 * @param {Mixed} value Reference to check
36 * @return {Mixed} Empty string if converted, otherwise the original value
38 undef : function(value){
39 return typeof value != "undefined" ? value : "";
43 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
44 * @param {String} value The string to encode
45 * @return {String} The encoded text
47 htmlEncode : function(value){
48 return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
52 * Convert certain characters (&, <, >, and ') from their HTML character equivalents.
53 * @param {String} value The string to decode
54 * @return {String} The decoded text
56 htmlDecode : function(value){
57 return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"');
61 * Trims any whitespace from either side of a string
62 * @param {String} value The text to trim
63 * @return {String} The trimmed text
65 trim : function(value){
66 return String(value).replace(trimRe, "");
70 * Returns a substring from within an original string
71 * @param {String} value The original text
72 * @param {Number} start The start index of the substring
73 * @param {Number} length The length of the substring
74 * @return {String} The substring
76 substr : function(value, start, length){
77 return String(value).substr(start, length);
81 * Converts a string to all lower case letters
82 * @param {String} value The text to convert
83 * @return {String} The converted text
85 lowercase : function(value){
86 return String(value).toLowerCase();
90 * Converts a string to all upper case letters
91 * @param {String} value The text to convert
92 * @return {String} The converted text
94 uppercase : function(value){
95 return String(value).toUpperCase();
99 * Converts the first character only of a string to upper case
100 * @param {String} value The text to convert
101 * @return {String} The converted text
103 capitalize : function(value){
104 return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
108 call : function(value, fn){
109 if(arguments.length > 2){
110 var args = Array.prototype.slice.call(arguments, 2);
113 return /** eval:var:value */ eval(fn).apply(window, args);
115 /** eval:var:value */
116 return /** eval:var:value */ eval(fn).call(window, value);
122 * safer version of Math.toFixed..??/
123 * @param {Number/String} value The numeric value to format
124 * @param {Number/String} value Decimal places
125 * @return {String} The formatted currency string
127 toFixed : function(v, n)
129 // why not use to fixed - precision is buggered???
130 var fact = Math.pow(10,n);
131 v = (Math.round((v-0)*fact))/fact;
132 var z = (''+fact).substring(1);
133 if (v == Math.floor(v)) {
134 return Math.floor(v) + '.' + z;
136 // now just padd decimals..
137 var ps = String(v).split('.');
138 var r = (ps[1] + z).substring(0,z.length);
139 return ps[0] + '.' + r;
143 * Parse a value into a formatted date using the specified format pattern.
144 * @param {Mixed} value The value to format
145 * @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
146 * @return {String} The formatted date string
148 date : function(v, format){
152 if(!(v instanceof Date)){
153 v = new Date(Date.parse(v));
155 return v.dateFormat(format || "m/d/Y");
159 * Returns a date rendering function that can be reused to apply a date format multiple times efficiently
160 * @param {String} format Any valid date format string
161 * @return {Function} The date formatting function
163 dateRenderer : function(format){
165 return Roo.util.Format.date(v, format);
170 stripTagsRE : /<\/?[^>]+>/gi,
173 * Strips all HTML tags
174 * @param {Mixed} value The text from which to strip tags
175 * @return {String} The stripped text
177 stripTags : function(v){
178 return !v ? v : String(v).replace(this.stripTagsRE, "");