From eb94667ec8a774360d4bae997d06a066c608bba4 Mon Sep 17 00:00:00 2001 From: visionmedia Date: Wed, 3 Feb 2010 19:26:09 -0800 Subject: [PATCH] Refactored mergeParam() --- lib/express/utils.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/express/utils.js b/lib/express/utils.js index 0a16f1ad89..bd4cd277c1 100644 --- a/lib/express/utils.js +++ b/lib/express/utils.js @@ -105,7 +105,7 @@ exports.escapeRegexp = function(string, chars) { /** * 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 @@ -118,21 +118,17 @@ 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