Skip to content

Instantly share code, notes, and snippets.

@foysavas
Created August 16, 2012 11:59
Show Gist options
  • Save foysavas/3369638 to your computer and use it in GitHub Desktop.
Save foysavas/3369638 to your computer and use it in GitHub Desktop.
_params.js
_params = function(params,encoded) {
var __hasProp = {}.hasOwnProperty;
maybeEncode = function(k){ return (encoded ? encodeURIComponent(k) : k); }
var pairs, proc;
pairs = [];
(proc = function(object, prefix) {
var el, i, key, value, _results;
if (object == null) object = params;
if (prefix == null) prefix = null;
_results = [];
for (key in object) {
if (!__hasProp.call(object, key)) continue;
value = object[key];
if (value instanceof Array) {
_results.push((function() {
var _len, _results2;
_results2 = [];
for (i = 0, _len = value.length; i < _len; i++) {
el = value[i];
_results2.push(proc(el, maybeEncode(prefix != null ? "" + prefix + "[" + key + "][]" : "" + key + "[]")));
}
return _results2;
})());
} else if (value instanceof Object) {
if (prefix != null) {
prefix += "[" + key + "]";
} else {
prefix = key;
}
_results.push(proc(value, prefix));
} else {
_results.push(pairs.push(prefix != null ? maybeEncode("" + prefix + "[" + key + "]")+"="+maybeEncode(value) : maybeEncode("" + key) + "=" + maybeEncode(value)));
}
}
return _results;
})();
return pairs.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment