/* * Ext JS Library 1.0.1 * Copyright(c) 2006-2007, Ext JS, LLC. * licensing@extjs.com * * http://www.extjs.com/license */ /** * @class Ext.util.Format * Reusable data formatting functions * @singleton */ Ext.util.Format = function(){ var trimRe = /^\s+|\s+$/g; return { /** * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length * @param {String} value The string to truncate * @param {Number} length The maximum length to allow before truncating * @return {String} The converted text */ ellipsis : function(value, len){ if(value && value.length > len){ return value.substr(0, len-3)+"..."; } return value; }, /** * Checks a reference and converts it to empty string if it is undefined * @param {Mixed} value Reference to check * @return {Mixed} Empty string if converted, otherwise the original value */ undef : function(value){ return typeof value != "undefined" ? value : ""; }, /** * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages. * @param {String} value The string to encode * @return {String} The encoded text */ htmlEncode : function(value){ return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/ 2){ var args = Array.prototype.slice.call(arguments, 2); args.unshift(value); return eval(fn).apply(window, args); }else{ return eval(fn).call(window, value); } }, /** * Format a number as US currency * @param {Number/String} value The numeric value to format * @return {String} The formatted currency string */ usMoney : function(v){ v = (Math.round((v-0)*100))/100; v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v); return "$" + v ; }, /** * Parse a value into a formatted date using the specified format pattern. * @param {Mixed} value The value to format * @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y') * @return {String} The formatted date string */ date : function(v, format){ if(!v){ return ""; } if(!(v instanceof Date)){ v = new Date(Date.parse(v)); } return v.dateFormat(format || "m/d/Y"); }, /** * Returns a date rendering function that can be reused to apply a date format multiple times efficiently * @param {String} format Any valid date format string * @return {Function} The date formatting function */ dateRenderer : function(format){ return function(v){ return Ext.util.Format.date(v, format); }; }, // private stripTagsRE : /<\/?[^>]+>/gi, /** * Strips all HTML tags * @param {Mixed} value The text from which to strip tags * @return {String} The stripped text */ stripTags : function(v){ return !v ? v : String(v).replace(this.stripTagsRE, ""); } }; }();