Skip to content

Instantly share code, notes, and snippets.

@guybrush
Created July 25, 2010 05:48
Show Gist options
  • Save guybrush/489344 to your computer and use it in GitHub Desktop.
Save guybrush/489344 to your computer and use it in GitHub Desktop.
obj2url
(function(exports, undefined){
/**
* obj2url() takes an object as argument and generates
* a querystring. pretty much like $.param() but without $
*
* @param Object json-object
* @param String current querystring-part
* @return String encoded querystring
*/
var obj2url = exports.obj2url = function(obj, temp) {
var uristrings = [],
add = function(nextObj, i){
var nextTemp = temp
? (/\[\]$/.test(temp)) // prevent double-encoding
? temp
: temp+'['+i+']'
: i;
uristrings.push(typeof nextObj === 'object'
? obj2url(nextObj, nextTemp)
: (Object.prototype.toString.call(nextObj) === '[object Function]')
? encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj())
: encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj));
};
if (Object.prototype.toString.call(obj) === '[object Array]')
for (var i = 0, len = obj.length; i < len; ++i) add(obj[i], i);
else if ((obj !== undefined) && (obj !== null) && (typeof obj === "object"))
for (var i in obj) add(obj[i], i);
else
uristrings.push(encodeURIComponent(temp) + '=' + encodeURIComponent(obj));
return uristrings.join('&').replace(/%20/g, '+');
};
})((function() {
if (typeof exports === 'undefined') {
return window;
} else {
return exports;
}
})());
(function(exports, undefined){
/**
* this is slightly faster than the first obj2url()
* running the function 10000 times with:
* {a:'b',c:'d',e:['f',{g:'h',i:['j',{k:'l',m:'n'}],o:undefined,p:true,q:false}]}
* this takes ~700ms, the first obj2url takes ~1000ms
*
* @param Object json-object
* @param String current querystring-part
* @return String encoded querystring
*/
var obj2url = exports.obj2url = function(obj, temp) {
var uristrings = [];
if (Object.prototype.toString.call(obj) === '[object Array]') {
for (var i = 0, len = obj.length; i<len; ++i) {
var nextObj = obj[i],
nextTemp = temp
? (/\[\]$/.test(temp)) // prevent double-encoding
? temp
: temp+'['+i+']'
: i;
uristrings.push(typeof nextObj === 'object'
? obj2url(nextObj, nextTemp)
: (Object.prototype.toString.call(nextObj) === '[object Function]')
? encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj())
: encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj));
}
} else if ((obj !== undefined) && (obj !== null) && (typeof obj === "object")) {
for (var i in obj) {
var nextObj = obj[i],
nextTemp = temp
? (/\[\]$/.test(temp)) // prevent double-encoding
? temp
: temp+'['+i+']'
: i;
uristrings.push(typeof nextObj === 'object'
? obj2url(nextObj, nextTemp)
: (Object.prototype.toString.call(nextObj) === '[object Function]')
? encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj())
: encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj));
}
} else {
uristrings.push(encodeURIComponent(temp)+'='+encodeURIComponent(obj));
}
return uristrings.join('&').replace(/%20/g, "+");
};
})((function() {
if (typeof exports === 'undefined') {
return window;
} else {
return exports;
}
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment