How to use the path-to-regexp.tokensToRegExp function in path-to-regexp

To help you get started, we’ve selected a few path-to-regexp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rill-js / rill / src / index.ts View on Github external
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;
}
github d-band / koa-mapper / src / layer.js View on Github external
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;
github FrankerFaceZ / FrankerFaceZ / src / utilities / compat / fine-router.js View on Github external
}
			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');
		}
	}
}
github lukeed / matchit / bench / index.js View on Github external
		data.ptokens.map(x => pathRegex.tokensToRegExp(x)).filter(rgx => rgx.exec('/bar/baz'));
	})
github vuchan / vue-mfe / src / utils / path.js View on Github external
    const regexps = keys.map((key) => tokensToRegExp(parse(key)))
    let i = 0
github ehmicky / autoserver / src / rpc / router / routes.js View on Github external
const parseRoute = function(route) {
  const tokens = parse(route)
  const regexp = tokensToRegExp(tokens)
  const variables = getVariables({ tokens })

  return { regexp, variables }
}
github vuchan / vue-mfe / src / utils / match.js View on Github external
    const regexps = keys.map((key) => tokensToRegExp(parse(key)))
    let i = 0