Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function toReg(pathname: string, keys: any[], options: any): RegExp {
// First parse path into tokens.
const tokens = parse(pathname);
// Find the last token (checking for splat params).
const splat: any = tokens[tokens.length - 1];
// Check if the last token is a splat and make it optional.
if (splat && splat.asterisk) {
splat.optional = true;
}
// Convert the tokens to a regexp.
const re = tokensToRegExp(tokens, options);
// Assign keys to from regexp.
(re as any).keys = keys;
for (let i = 0, len = tokens.length; i < len; i++) {
if (typeof tokens[i] === "object") {
keys.push(tokens[i]);
}
}
return re;
}
setPrefix(prefix) {
if (this.path) {
this.path = prefix + this.path;
if (/.+\/$/.test(this.path)) {
this.path = this.path.replace(/\/$/, '');
}
debug('defined route %s %s', this.methods, this.path);
const tokens = ptr.parse(this.path, this.opts);
this.pathKeys = [];
this.regexp = ptr.tokensToRegExp(tokens, this.pathKeys, this.opts);
this.url = (params, options = {}) => {
let replace = {};
if (Array.isArray(params)) {
this.pathKeys.forEach((key, i) => {
replace[key.name] = params[i];
});
} else {
replace = params;
}
const toPath = ptr.tokensToFunction(tokens);
const base = toPath(replace, options);
return toURI(base, options.query);
};
this.pathTemplate = () => {
const obj = this.pathKeys.reduce((memo, k) => {
memo[k.name] = k.name;
}
return;
}
const parts = parse(path),
score = parts.reduce((total, val) => total + (
typeof val === 'string' ?
val.split('/').length - 1 :
0
), 0),
route = this.routes[name] = {
name,
parts,
score,
domain,
regex: tokensToRegExp(parts),
url: tokensToFunction(parts)
}
this.__routes.push(route);
if ( process ) {
this.__routes.sort((a,b) => b.score - a.score);
if ( this.location )
this._pickRoute();
this.emit(':updated-routes');
}
}
}
data.ptokens.map(x => pathRegex.tokensToRegExp(x)).filter(rgx => rgx.exec('/bar/baz'));
})
const regexps = keys.map((key) => tokensToRegExp(parse(key)))
let i = 0
const parseRoute = function(route) {
const tokens = parse(route)
const regexp = tokensToRegExp(tokens)
const variables = getVariables({ tokens })
return { regexp, variables }
}
const regexps = keys.map((key) => tokensToRegExp(parse(key)))
let i = 0