2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From Express core: (MIT License)
|
|
|
|
*
|
|
|
|
* Normalize the given path string,
|
|
|
|
* returning a regular expression.
|
|
|
|
*
|
|
|
|
* An empty array should be passed,
|
|
|
|
* which will contain the placeholder
|
|
|
|
* key names. For example "/user/:id" will
|
|
|
|
* then contain ["id"].
|
|
|
|
*
|
|
|
|
* @param {String|RegExp|Array} path
|
|
|
|
* @param {Array} keys
|
|
|
|
* @param {Boolean} sensitive
|
|
|
|
* @param {Boolean} strict
|
|
|
|
* @return {RegExp}
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2018-05-04 17:36:50 +02:00
|
|
|
/* eslint-disable prefer-template */
|
|
|
|
/* eslint-disable no-useless-escape */
|
2020-10-27 11:27:17 +01:00
|
|
|
const pathRegexp = (path, keys, sensitive, strict) => {
|
2016-03-18 11:12:50 +01:00
|
|
|
if (toString.call(path) === '[object RegExp]') {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
if (Array.isArray(path)) {
|
|
|
|
path = '(' + path.join('|') + ')';
|
|
|
|
}
|
|
|
|
path = path
|
|
|
|
.concat(strict ? '' : '/?')
|
|
|
|
.replace(/\/\(/g, '(?:/')
|
2020-10-27 11:27:17 +01:00
|
|
|
.replace(
|
|
|
|
/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g,
|
|
|
|
(_, slash, format, key, capture, optional, star) => {
|
|
|
|
keys.push({
|
|
|
|
name: key,
|
|
|
|
optional: !!optional,
|
|
|
|
});
|
|
|
|
slash = slash || '';
|
|
|
|
return (
|
|
|
|
'' +
|
|
|
|
(optional ? '' : slash) +
|
|
|
|
'(?:' +
|
|
|
|
(optional ? slash : '') +
|
|
|
|
(format || '') +
|
|
|
|
(capture || (format && '([^/.]+?)') || '([^/]+?)') +
|
|
|
|
')' +
|
|
|
|
(optional || '') +
|
|
|
|
(star ? '(/*)?' : '')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
2016-03-18 11:12:50 +01:00
|
|
|
.replace(/([\/.])/g, '\\$1')
|
|
|
|
.replace(/\*/g, '(.*)');
|
|
|
|
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
|
|
|
|
};
|
2020-10-27 11:27:17 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
pathRegexp,
|
|
|
|
};
|