[ Index ] |
|
Code source de PRADO 3.0.6 |
1 /** 2 * @class String extensions 3 */ 4 Object.extend(String.prototype, { 5 /** 6 * @param {String} "left" to pad the string on the left, "right" to pad right. 7 * @param {Number} minimum string length. 8 * @param {String} character(s) to pad 9 * @return {String} padded character(s) on the left or right to satisfy minimum string length 10 */ 11 12 pad : function(side, len, chr) { 13 if (!chr) chr = ' '; 14 var s = this; 15 var left = side.toLowerCase()=='left'; 16 while (s.length<len) s = left? chr + s : s + chr; 17 return s; 18 }, 19 20 /** 21 * @param {Number} minimum string length. 22 * @param {String} character(s) to pad 23 * @return {String} padded character(s) on the left to satisfy minimum string length 24 */ 25 padLeft : function(len, chr) { 26 return this.pad('left',len,chr); 27 }, 28 29 /** 30 * @param {Number} minimum string length. 31 * @param {String} character(s) to pad 32 * @return {String} padded character(s) on the right to satisfy minimum string length 33 */ 34 padRight : function(len, chr) { 35 return this.pad('right',len,chr); 36 }, 37 38 /** 39 * @param {Number} minimum string length. 40 * @return {String} append zeros to the left to satisfy minimum string length. 41 */ 42 zerofill : function(len) { 43 return this.padLeft(len,'0'); 44 }, 45 46 /** 47 * @return {String} removed white spaces from both ends. 48 */ 49 trim : function() { 50 return this.replace(/^\s+|\s+$/g,''); 51 }, 52 53 /** 54 * @return {String} removed white spaces from the left end. 55 */ 56 trimLeft : function() { 57 return this.replace(/^\s+/,''); 58 }, 59 60 /** 61 * @return {String} removed white spaces from the right end. 62 */ 63 trimRight : function() { 64 return this.replace(/\s+$/,''); 65 }, 66 67 /** 68 * Convert period separated function names into a function reference. 69 * e.g. "Prado.AJAX.Callback.Action.setValue".toFunction() will return 70 * the actual function Prado.AJAX.Callback.Action.setValue() 71 * @return {Function} the corresponding function represented by the string. 72 */ 73 toFunction : function() 74 { 75 var commands = this.split(/\./); 76 var command = window; 77 commands.each(function(action) 78 { 79 if(command[new String(action)]) 80 command=command[new String(action)]; 81 }); 82 if(typeof(command) == "function") 83 return command; 84 else 85 { 86 if(typeof Logger != "undefined") 87 Logger.error("Missing function", this); 88 89 throw new Error ("Missing function '"+this+"'"); 90 } 91 }, 92 93 /** 94 * Convert a string into integer, returns null if not integer. 95 * @return {Number} null if string does not represent an integer. 96 */ 97 toInteger : function() 98 { 99 var exp = /^\s*[-\+]?\d+\s*$/; 100 if (this.match(exp) == null) 101 return null; 102 var num = parseInt(this, 10); 103 return (isNaN(num) ? null : num); 104 }, 105 106 /** 107 * Convert a string into a double/float value. <b>Internationalization 108 * is not supported</b> 109 * @param {String} the decimal character 110 * @return {Double} null if string does not represent a float value 111 */ 112 toDouble : function(decimalchar) 113 { 114 if(this.length <= 0) return null; 115 decimalchar = decimalchar || "."; 116 var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$"); 117 var m = this.match(exp); 118 119 if (m == null) 120 return null; 121 m[1] = m[1] || ""; 122 m[2] = m[2] || "0"; 123 m[4] = m[4] || "0"; 124 125 var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4]; 126 var num = parseFloat(cleanInput); 127 return (isNaN(num) ? null : num); 128 }, 129 130 /** 131 * Convert strings that represent a currency value (e.g. a float with grouping 132 * characters) to float. E.g. "10,000.50" will become "10000.50". The number 133 * of dicimal digits, grouping and decimal characters can be specified. 134 * <i>The currency input format is <b>very</b> strict, null will be returned if 135 * the pattern does not match</i>. 136 * @param {String} the grouping character, default is "," 137 * @param {Number} number of decimal digits 138 * @param {String} the decimal character, default is "." 139 * @type {Double} the currency value as float. 140 */ 141 toCurrency : function(groupchar, digits, decimalchar) 142 { 143 groupchar = groupchar || ","; 144 decimalchar = decimalchar || "."; 145 digits = typeof(digits) == "undefined" ? 2 : digits; 146 147 var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)" 148 + ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "") 149 + "\\s*$"); 150 var m = this.match(exp); 151 if (m == null) 152 return null; 153 var intermed = m[2] + m[5] ; 154 var cleanInput = m[1] + intermed.replace( 155 new RegExp("(\\" + groupchar + ")", "g"), "") 156 + ((digits > 0) ? "." + m[7] : ""); 157 var num = parseFloat(cleanInput); 158 return (isNaN(num) ? null : num); 159 }, 160 161 /** 162 * Converts the string to a date by finding values that matches the 163 * date format pattern. 164 * @param string date format pattern, e.g. MM-dd-yyyy 165 * @return {Date} the date extracted from the string 166 */ 167 toDate : function(format) 168 { 169 return Date.SimpleParse(this, format); 170 } 171 });
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |