/ Published in: JavaScript
URL: http://pjotor.com
A simple prototype function to make objects out of url query strings. (bonus jQuery plugin)
Expand |
Embed | Plain Text
//prototype String.prototype.mapStr = function(objDev, keyDev){ objDev = objDev || ";", keyDev = keyDev || ":"; if(this.indexOf(keyDev) === -1) return this; var d = this.split(objDev); var r = {}; var key, val; for(par in d){ val = d[par].split(keyDev); key = val.splice(0, 1).join().replace(/^\s\s*/, ''); val = val.join(keyDev).replace(/^\s\s*/, ''); if (val.length > 1) { r[key.camelCase()] = val; } } return r; } String.prototype.camelCase = function(){ return this.replace(/-([a-z])/ig, function (all, letter) { return letter.toUpperCase(); }); } //jQuery plugin // a little string mapper (string to object) ;(function ($) { $.map.str = function (s, objDev, keyDev) { objDev = objDev || ";", keyDev = keyDev || ":"; if (s.indexOf(keyDev) === -1) return s; var d = s.split(objDev); var r = {}; var key, val; for(par in d) { val = d[par].split(keyDev); key = $.trim(val.splice(0, 1).join()); val = $.trim(val.join(keyDev)); if (val.length > 1) { r[$.camelCase(key)] = val; } } return r; } })(jQuery); ;(function ($) { // CamelCase object keys (ripped from jQuery since it seems to come and go in core) if ( !$.isFunction($.camelCase) ) { $.camelCase = function (string) { return string.replace(/-([a-z])/ig, function (all, letter) { return letter.toUpperCase(); }); } } })(jQuery);
You need to login to post a comment.
