1 /** 2 * @namespace Formatting and conversion utilities. 3 */ 4 Jelo.Format = function() { 5 /** @scope Jelo.Format */ 6 return { 7 /** 8 * Converts a string from CSS hyphenated to Javascript camelCase. 9 * 10 * <pre><code> 11 // example: 12 var property = Jelo.Format.toCamel("margin-left"); 13 alert(property); // alerts "marginLeft" 14 </code></pre> 15 */ 16 toCamel : function(str) { 17 return str.replace(/-(.)/g, function(m, l) { 18 return l.toUpperCase(); 19 }); 20 }, 21 22 /** 23 * @deprecated v1.02: Too long a name, replaced by 24 * {@link Jelo.Format.toCamel} 25 */ 26 hyphenatedToCamelCase : function(str) { 27 return Jelo.Format.toCamel(str); 28 }, 29 30 /** 31 * Mainly used by internal Jelo functions. If a hash 32 * mark (#) appears at the beginning of the string, it 33 * will be stripped. 34 * 35 * @param {String} hex A value such as "#0080FF" 36 * @returns {String} A value such as "0080FF" 37 */ 38 cutHexHash : function(h) { 39 return (h.charAt(0) == "#") ? h.substring(1) : h; 40 }, 41 /** 42 * Returns the "red" value from a CSS-style hex string. 43 * 44 * @param {String} hex A value such as "#9966CC" 45 * @returns {String} For the above example, "99" 46 */ 47 hexToR : function(h) { 48 return parseInt(Jelo.Format.cutHexHash(h).substring(0, 2), 16); 49 }, 50 /** 51 * Returns the "green" value from a CSS-style hex string. 52 * 53 * @param {String} hex A value such as "#9966CC" 54 * @returns {String} For the above example, "66" 55 */ 56 hexToG : function(h) { 57 return parseInt(Jelo.Format.cutHexHash(h).substring(2, 4), 16); 58 }, 59 /** 60 * Returns the "blue" value from a CSS-style hex string. 61 * 62 * @param {String} hex A value such as "#9966CC" 63 * @returns {String} For the above example, "CC" 64 */ 65 hexToB : function(h) { 66 return parseInt(Jelo.Format.cutHexHash(h).substring(4, 6), 16); 67 }, 68 /** 69 * Splits a CSS-style hex value into a array of Red, Green, and Blue values. 70 * 71 * @param {String} hex A value such as "#9966CC" 72 * @returns {Array} For the above example, ["99", "66", "CC"] 73 */ 74 hexToRGB : function(h) { 75 var r = this.hexToR(h); 76 var g = this.hexToG(h); 77 var b = this.hexToB(h); 78 return [r, g, b]; 79 }, 80 81 /** 82 * Converts a CSS RGB string to an array of RGB values. Mainly useful to 83 * internal Jelo functions. 84 * 85 * @param {String} hex A value such as "rgb(0, 128, 255)" 86 * @returns {Array} For the above example, [0, 128, 255] 87 */ 88 rgbStringToArray : function(s) { 89 if (s.isString) { 90 try { 91 var sub = s.split(/\D/g); 92 var sub2 = []; 93 for (var i = 0; i < sub.length; i++) { 94 if (sub[i]) { 95 sub2[sub2.length] = parseInt(sub[i], 10); 96 } 97 } 98 return sub2; 99 } catch (e) { 100 throw new Error("Jelo.Format.rgbStringToArray: Invalid input " + s); 101 } 102 } else { 103 return []; 104 } 105 }, 106 107 /** 108 * Converts a CSS RGB string to a CSS hex string. Mainly useful to 109 * internal Jelo functions. 110 * 111 * @param {String} hex A value such as "rgb(0, 128, 255)" 112 * @returns {Array} For the above example, "#0080FF" 113 */ 114 rgbToHex : function(s) { 115 if (s.isString) { 116 try { 117 function toHex(n) { 118 var chr = "0123456789ABCDEF"; 119 if (n === null) { 120 return "00"; 121 } 122 n = parseInt(n, 10); 123 if (isNaN(n) || !n) { 124 return "00"; 125 } 126 n = Math.max(0, n); 127 n = Math.min(n, 255); 128 n = Math.round(n); 129 return chr.charAt((n - n % 16) / 16) + chr.charAt(n % 16); 130 } 131 var a = Jelo.Format.rgbStringToArray(s); 132 return "#" + toHex(a[0]) + toHex(a[1]) + toHex(a[2]); 133 } catch (e) { 134 throw new Error("Jelo.Format.rgbStringToHex: Invalid input " + s); 135 } 136 } 137 }, 138 139 /** 140 * Port of http://php.net/urldecode by http://kevin.vanzonneveld.net 141 * 142 * @see <a href="http://php.net/urldecode">PHP documentation</a> 143 * @see <a href="http://kevin.vanzonneveld.net">Original port</a> 144 * @param {String} str 145 * @returns {String} 146 */ 147 urldecode : function(str) { 148 var /* histogram */h = {}; 149 var ret = str.toString(); 150 function rep(s, r, str) { 151 var tmp_arr = []; 152 tmp_arr = str.split(s); 153 return tmp_arr.join(r); 154 } 155 h["'"] = '%27'; 156 h['('] = '%28'; 157 h[')'] = '%29'; 158 h['*'] = '%2A'; 159 h['~'] = '%7E'; 160 h['!'] = '%21'; 161 h['%20'] = '+'; 162 for (var r in h) { 163 if (h.hasOwnProperty(s)) { 164 s = h[r]; // Switch order when decoding 165 ret = rep(s, r, ret); // Custom replace. No regexing 166 } 167 } 168 return decodeURIComponent(ret); 169 }, 170 171 /** 172 * Port of http://php.net/urlencode by http://kevin.vanzonneveld.net 173 * 174 * @see <a href="http://php.net/urldecode">PHP documentation</a> 175 * @see <a href="http://kevin.vanzonneveld.net">Original port</a> 176 * @param {String} str 177 * @returns {String} 178 */ 179 urlencode : function(str) { 180 var /* histogram */h = {}, tmp_arr = []; 181 var ret = str.toString(); 182 function rep(s, r, str) { 183 var tmp_arr = []; 184 tmp_arr = str.split(s); 185 return tmp_arr.join(r); 186 } 187 h["'"] = '%27'; 188 h['('] = '%28'; 189 h[')'] = '%29'; 190 h['*'] = '%2A'; 191 h['~'] = '%7E'; 192 h['!'] = '%21'; 193 h['%20'] = '+'; 194 ret = encodeURIComponent(ret); 195 for (var s in h) { 196 if (h.hasOwnProperty(s)) { 197 r = h[s]; // Switch order when encoding 198 ret = rep(s, r, ret); // Custom replace. No regexing 199 } 200 } 201 return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) { 202 return "%" + m2.toUpperCase(); 203 }); 204 }, 205 206 /** 207 * Alias for {@link Jelo.Format.urldecode} 208 */ 209 urlDecode : function(str) { 210 return Jelo.Format.urldecode(str); 211 }, 212 213 /** 214 * Alias for {@link Jelo.Format.urlencode} 215 */ 216 urlEncode : function(str) { 217 return Jelo.Format.urlencode(str); 218 }, 219 220 /** 221 * Converts a valid JSON string to a native Object. 222 * 223 * @param {String} json A JSON-encoded string, e.g. '{ name : "John", age: "28" }' 224 * @returns {Object} The object represented by valid input, or {} for invalid input. 225 */ 226 fromJSON : function(str) { 227 try { 228 var crockford = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/; 229 var replace = /"(\\.|[^"\\])*"/g; 230 return !(crockford.test(o.replace(replace, ''))) && eval('(' + o + ')'); 231 } catch (e) { 232 return {}; 233 } 234 } 235 }; 236 }(); 237