Skip to content
Myr

Added parseParams() and specs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent fc7dc55 · Browse files · 2 files+65 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−0 removed

  • tests:+21 added−0 removed
  • other:+44 added−0 removed

2 files+65 added−0 removed

modified:lib/express/helpers.js+44 added
202 unmodified lines
203204exports.escapeRegexp = function(string, chars) {205 var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')206 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')207}208209/**210 * Parse nested _params_.211 *212 * @param {hash} params213 * @return {hash}214 * @see parseParams() 215 * @api private216 */217218function parseNestedParams(params) {219 for (var key in params)220 if (parts = key.split('['))221 if (parts.length > 1)222 for (var i = 0, prop = params, len = parts.length; i < len; ++i) {223 var name = parts[i].replace(']', '')224 if (i == len - 1) {225 prop[name] = params[key]226 prop = params, delete params[key]227 }228 else {229 prop = prop[name] = prop[name] || {}230 }231 }232 233 return params234}235236/**237 * Parse params _string_ into a nested hash.238 *239 * @param {string} string240 * @return {hash}241 * @api public242 */243244exports.parseParams = function(string) {245 var params = {}, pairs = string.split('&'), pair246 for (var i = 0, len = pairs.length; i < len; ++i) {247 pair = pairs[i].split('=')248 params[pair[0]] = pair[1]249 }250 return parseNestedParams(params)251}
modified:spec/spec.core.helpers.js+21 added