103 unmodified lines
Expand all
/**
* Merge param _key_ and _val_ into _params_. Key
* should be a query string key such as 'user[name]',
* and _val_ is it's associated value. The root _params_
* and _val_ is it's associated object. The root _params_
* object is returned.
*
* @param {string} key
* @param {mixed} val
exports.mergeParam = function(key, val, params) {
var orig = params,
keys = key.trim().match(/\w+/g),
array = /\[\]$/.test(key)
$(keys).reduce(queryString.parseQuery(key), function(parts, currentKey, i){
$(keys).reduce(queryString.parseQuery(key), function(parts, key, i){
if (i === keys.length - 1)
if (currentKey in params)
if (params[currentKey] instanceof Array)
params[currentKey].push(val)
else
params[currentKey] = [params[currentKey], val]
if (key in params)
params[key] instanceof Array ?
params[key].push(val) :
params[key] = [params[key], val]
else
if (array)
params[currentKey] = [val]
else
params[currentKey] = val
if (!(currentKey in params)) params[currentKey] = {}
params = params[currentKey]
return parts[currentKey]
params[key] = array ? [val] : val
if (!(key in params)) params[key] = {}
params = params[key]
return parts[key]
})
return orig
}
No newline at end of file
No newline at end of file