Last active
November 14, 2019 09:58
-
-
Save yakirn/831e5e7fcdfb50fc6c94 to your computer and use it in GitHub Desktop.
A modified version of serialize and param methods from zeptos, that depends on _ as a global var (tested on [email protected])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* the following serialize and param methods are a version of zepto's | |
* https://github.com/madrobby/zepto/blob/601372ac4e3f98d502c707bf841589fbc48a3a7d/src/ajax.js#L344-L370 | |
* modified to use _ (tested on [email protected], should also work in underscore) | |
* to use with github's fetch: | |
fetch(urrl, { | |
method: 'post', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
body: params({key1: "value1", key2: "value2"}) | |
}) | |
*/ | |
function serialize(params, obj, traditional, scope){ | |
var type, array = _.isArray(obj), hash = _.isObject(obj) | |
_.each(obj, function(value, key) { | |
type = typeof value | |
if (scope) key = traditional ? scope : | |
scope + '[' + (hash || type == 'object' || type == 'array' ? key : '') + ']' | |
// handle data in serializeArray() format | |
if (!scope && array) params.add(value.name, value.value) | |
// recurse into nested objects | |
else if (type == "array" || (!traditional && type == "object")) | |
serialize(params, value, traditional, key) | |
else params.add(key, value) | |
}) | |
} | |
function param(obj, traditional){ | |
var params = [] | |
params.add = function(key, value) { | |
if (_.isFunction(value)) value = value() | |
if (value == null) value = "" | |
this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)) | |
} | |
serialize(params, obj, traditional) | |
return params.join('&').replace(/%20/g, '+') | |
} |
Hey @sandrosc
Honestly it’s an old script that I don’t use anymore. I remember I didn’t Need to mimic the old behavior exactly as it was, But not much besides that, so I can’t comment concretely on what you’re saying.
Anyway thanks for sharing what you found.
ah yes, was more a comment for people which might come here later from google ^^. Thanks for your response :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure how much it hurts and how relevant this is, but I've been looking into this a bit and noticed a bug:
you wrote
type = typeof value
(wastype = $.type(value)
). Later you check whethertype == "array"
, which will never betrue
, sincetypeof [] == 'object'
.So I took another look at the
$.type
implementation (https://api.jquery.com/jquery.type/) and it turns out that a lot of edge cases would need to be changed if one were to reproduce the exact behavior of the old implementation. For exampletypeof new Number(3) == 'object'
but$.type(new Number(3) == 'number'
. This solution would therefore, unlike the old one, try to serializenew Number(3)
because(!traditional && type == 'object'))
is true.