Skip to content
Myr

utils: added pathRegexp()

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

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 54c3d5c · Browse files · 2 files+48 added−40 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+48 added−40 removed

  • other:+48 added−40 removed

2 files+48 added−40 removed

modified:lib/router/route.js+8 added-40 removed
modified:lib/utils.js+40 added
211 unmodified lines
212 .replace(/"/g, '&quot;')213 .replace(/</g, '&lt;')214 .replace(/>/g, '&gt;');215};216217218/**219 * Normalize the given path string,220 * returning a regular expression.221 *222 * An empty array should be passed,223 * which will contain the placeholder224 * key names. For example "/user/:id" will225 * then contain ["id"].226 *227 * @param {String|RegExp|Array} path228 * @param {Array} keys229 * @param {Boolean} sensitive230 * @param {Boolean} strict231 * @return {RegExp}232 * @api private233 */234235exports.pathRegexp = function(path, keys, sensitive, strict) {236 if (path instanceof RegExp) return path;237 if (path instanceof Array) 238 path = '(' + path.join('|') + ')';239 path = path240 .concat(strict ? '' : '/?')241 .replace(/\/\(/g, '(?:/')242 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){243 keys.push({ name: key, optional: !! optional });244 slash = slash || '';245 return ''246 + (optional ? '' : slash)247 + '(?:'248 + (optional ? slash : '')249 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'250 + (optional || '');251 })252 .replace(/([\/.])/g, '\\$1')253 .replace(/\*/g, '(.*)');254 return new RegExp('^' + path + '$', sensitive ? '' : 'i');255}