Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / util / Format.js
index 49d0622..8146585 100644 (file)
@@ -1,5 +1,5 @@
 /*!
- * Ext JS Library 3.2.2
+ * Ext JS Library 3.3.0
  * Copyright(c) 2006-2010 Ext JS, Inc.
  * licensing@extjs.com
  * http://www.extjs.com/license
@@ -9,11 +9,11 @@
  * Reusable data formatting functions
  * @singleton
  */
-Ext.util.Format = function(){
-    var trimRe = /^\s+|\s+$/g,
-        stripTagsRE = /<\/?[^>]+>/gi,
+Ext.util.Format = function() {
+    var trimRe         = /^\s+|\s+$/g,
+        stripTagsRE    = /<\/?[^>]+>/gi,
         stripScriptsRe = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
-        nl2brRe = /\r?\n/g;
+        nl2brRe        = /\r?\n/g;
 
     return {
         /**
@@ -23,17 +23,17 @@ Ext.util.Format = function(){
          * @param {Boolean} word True to try to find a common work break
          * @return {String} The converted text
          */
-        ellipsis : function(value, len, word){
-            if(value && value.length > len){
-                if(word){
-                    var vs = value.substr(0, len - 2),
+        ellipsis : function(value, len, word) {
+            if (value && value.length > len) {
+                if (word) {
+                    var vs    = value.substr(0, len - 2),
                         index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
-                    if(index == -1 || index < (len - 15)){
+                    if (index == -1 || index < (len - 15)) {
                         return value.substr(0, len - 3) + "...";
-                    }else{
+                    } else {
                         return vs.substr(0, index) + "...";
                     }
-                } else{
+                } else {
                     return value.substr(0, len - 3) + "...";
                 }
             }
@@ -45,7 +45,7 @@ Ext.util.Format = function(){
          * @param {Mixed} value Reference to check
          * @return {Mixed} Empty string if converted, otherwise the original value
          */
-        undef : function(value){
+        undef : function(value) {
             return value !== undefined ? value : "";
         },
 
@@ -55,7 +55,7 @@ Ext.util.Format = function(){
          * @param {String} defaultValue The value to insert of it's undefined (defaults to "")
          * @return {String}
          */
-        defaultValue : function(value, defaultValue){
+        defaultValue : function(value, defaultValue) {
             return value !== undefined && value !== '' ? value : defaultValue;
         },
 
@@ -64,7 +64,7 @@ Ext.util.Format = function(){
          * @param {String} value The string to encode
          * @return {String} The encoded text
          */
-        htmlEncode : function(value){
+        htmlEncode : function(value) {
             return !value ? value : String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
         },
 
@@ -73,7 +73,7 @@ Ext.util.Format = function(){
          * @param {String} value The string to decode
          * @return {String} The decoded text
          */
-        htmlDecode : function(value){
+        htmlDecode : function(value) {
             return !value ? value : String(value).replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&quot;/g, '"').replace(/&amp;/g, "&");
         },
 
@@ -82,7 +82,7 @@ Ext.util.Format = function(){
          * @param {String} value The text to trim
          * @return {String} The trimmed text
          */
-        trim : function(value){
+        trim : function(value) {
             return String(value).replace(trimRe, "");
         },
 
@@ -93,7 +93,7 @@ Ext.util.Format = function(){
          * @param {Number} length The length of the substring
          * @return {String} The substring
          */
-        substr : function(value, start, length){
+        substr : function(value, start, length) {
             return String(value).substr(start, length);
         },
 
@@ -102,7 +102,7 @@ Ext.util.Format = function(){
          * @param {String} value The text to convert
          * @return {String} The converted text
          */
-        lowercase : function(value){
+        lowercase : function(value) {
             return String(value).toLowerCase();
         },
 
@@ -111,7 +111,7 @@ Ext.util.Format = function(){
          * @param {String} value The text to convert
          * @return {String} The converted text
          */
-        uppercase : function(value){
+        uppercase : function(value) {
             return String(value).toUpperCase();
         },
 
@@ -120,17 +120,17 @@ Ext.util.Format = function(){
          * @param {String} value The text to convert
          * @return {String} The converted text
          */
-        capitalize : function(value){
+        capitalize : function(value) {
             return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
         },
 
         // private
-        call : function(value, fn){
-            if(arguments.length > 2){
+        call : function(value, fn) {
+            if (arguments.length > 2) {
                 var args = Array.prototype.slice.call(arguments, 2);
                 args.unshift(value);
                 return eval(fn).apply(window, args);
-            }else{
+            } else {
                 return eval(fn).call(window, value);
             }
         },
@@ -140,7 +140,7 @@ Ext.util.Format = function(){
          * @param {Number/String} value The numeric value to format
          * @return {String} The formatted currency string
          */
-        usMoney : function(v){
+        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);
             v = String(v);
@@ -152,7 +152,7 @@ Ext.util.Format = function(){
                 whole = whole.replace(r, '$1' + ',' + '$2');
             }
             v = whole + sub;
-            if(v.charAt(0) == '-'){
+            if (v.charAt(0) == '-') {
                 return '-$' + v.substr(1);
             }
             return "$" +  v;
@@ -164,11 +164,11 @@ Ext.util.Format = function(){
          * @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){
+        date : function(v, format) {
+            if (!v) {
                 return "";
             }
-            if(!Ext.isDate(v)){
+            if (!Ext.isDate(v)) {
                 v = new Date(Date.parse(v));
             }
             return v.dateFormat(format || "m/d/Y");
@@ -179,8 +179,8 @@ Ext.util.Format = function(){
          * @param {String} format Any valid date format string
          * @return {Function} The date formatting function
          */
-        dateRenderer : function(format){
-            return function(v){
+        dateRenderer : function(format) {
+            return function(v) {
                 return Ext.util.Format.date(v, format);
             };
         },
@@ -190,7 +190,7 @@ Ext.util.Format = function(){
          * @param {Mixed} value The text from which to strip tags
          * @return {String} The stripped text
          */
-        stripTags : function(v){
+        stripTags : function(v) {
             return !v ? v : String(v).replace(stripTagsRE, "");
         },
 
@@ -199,7 +199,7 @@ Ext.util.Format = function(){
          * @param {Mixed} value The text from which to strip script tags
          * @return {String} The stripped text
          */
-        stripScripts : function(v){
+        stripScripts : function(v) {
             return !v ? v : String(v).replace(stripScriptsRe, "");
         },
 
@@ -208,10 +208,10 @@ Ext.util.Format = function(){
          * @param {Number/String} size The numeric value to format
          * @return {String} The formatted file size
          */
-        fileSize : function(size){
-            if(size < 1024) {
+        fileSize : function(size) {
+            if (size < 1024) {
                 return size + " bytes";
-            } else if(size < 1048576) {
+            } else if (size < 1048576) {
                 return (Math.round(((size*10) / 1024))/10) + " KB";
             } else {
                 return (Math.round(((size*10) / 1048576))/10) + " MB";
@@ -226,12 +226,13 @@ Ext.util.Format = function(){
          */
         math : function(){
             var fns = {};
+            
             return function(v, a){
-                if(!fns[a]){
+                if (!fns[a]) {
                     fns[a] = new Function('v', 'return v ' + a + ';');
                 }
                 return fns[a](v);
-            }
+            };
         }(),
 
         /**
@@ -267,34 +268,34 @@ Ext.util.Format = function(){
          * @return {String} The formatted number.
          */
         number: function(v, format) {
-            if(!format){
+            if (!format) {
                 return v;
             }
             v = Ext.num(v, NaN);
-            if (isNaN(v)){
+            if (isNaN(v)) {
                 return '';
             }
             var comma = ',',
-                dec = '.',
-                i18n = false,
-                neg = v < 0;
+                dec   = '.',
+                i18n  = false,
+                neg   = v < 0;
 
             v = Math.abs(v);
-            if(format.substr(format.length - 2) == '/i'){
+            if (format.substr(format.length - 2) == '/i') {
                 format = format.substr(0, format.length - 2);
-                i18n = true;
-                comma = '.';
-                dec = ',';
+                i18n   = true;
+                comma  = '.';
+                dec    = ',';
             }
 
             var hasComma = format.indexOf(comma) != -1,
-                psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
+                psplit   = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
 
-            if(1 < psplit.length){
+            if (1 < psplit.length) {
                 v = v.toFixed(psplit[1].length);
-            }else if(2 < psplit.length){
+            } else if(2 < psplit.length) {
                 throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
-            }else{
+            } else {
                 v = v.toFixed(0);
             }
 
@@ -303,12 +304,18 @@ Ext.util.Format = function(){
             psplit = fnum.split('.');
 
             if (hasComma) {
-                var cnum = psplit[0], parr = [], j = cnum.length, m = Math.floor(j / 3), n = cnum.length % 3 || 3;
+                var cnum = psplit[0], 
+                    parr = [], 
+                    j    = cnum.length, 
+                    m    = Math.floor(j / 3),
+                    n    = cnum.length % 3 || 3,
+                    i;
 
-                for (var i = 0; i < j; i += n) {
+                for (i = 0; i < j; i += n) {
                     if (i != 0) {
                         n = 3;
                     }
+                    
                     parr[parr.length] = cnum.substr(i, n);
                     m -= 1;
                 }
@@ -330,8 +337,8 @@ Ext.util.Format = function(){
          * @param {String} format Any valid number format string for {@link #number}
          * @return {Function} The number formatting function
          */
-        numberRenderer : function(format){
-            return function(v){
+        numberRenderer : function(format) {
+            return function(v) {
                 return Ext.util.Format.number(v, format);
             };
         },
@@ -344,7 +351,7 @@ Ext.util.Format = function(){
          * @param {String} singular The singular form of the word
          * @param {String} plural (optional) The plural form of the word (defaults to the singular with an "s")
          */
-        plural : function(v, s, p){
+        plural : function(v, s, p) {
             return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
         },
 
@@ -353,8 +360,8 @@ Ext.util.Format = function(){
          * @param {String} The string value to format.
          * @return {String} The string with embedded &lt;br/> tags in place of newlines.
          */
-        nl2br : function(v){
+        nl2br : function(v) {
             return Ext.isEmpty(v) ? '' : v.replace(nl2brRe, '<br/>');
         }
-    }
+    };
 }();