How to use the path-to-regexp.tokensToFunction 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 gaoding-inc / v-model / src / model-base.js View on Github external
static extend(url, actions, staticProps, options) {
        const Model = class Model extends this {};

        const urlTokens = pathToRegexp.parse(url);

        // optional last token
        const urlLastToken = urlTokens[urlTokens.length - 1];
        if(typeof urlLastToken === 'object') {
            urlLastToken.optional = true;
        }

        Model.url = pathToRegexp.tokensToFunction(urlTokens);
        Model.urlTokens = urlTokens;
        Model.options = options;

        // staic props
        assign(Model, staticProps);

        // actions
        forEach(actions, (action, name) => {
            Model.addAction(name, action);
        });

        return Model;
    }
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 d-band / koa-mapper / src / layer.js View on Github external
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 = () => {
github kriasoft / universal-router / src / generateUrls.js View on Github external
}
    }

    let regexp = cache.get(route.fullPath)
    if (!regexp) {
      let fullPath = ''
      let rt = route
      while (rt) {
        const path = Array.isArray(rt.path) ? rt.path[0] : rt.path
        if (path) {
          fullPath = path + fullPath
        }
        rt = rt.parent
      }
      const tokens = pathToRegexp.parse(fullPath, options)
      const toPath = pathToRegexp.tokensToFunction(tokens, options)
      const keys = Object.create(null)
      for (let i = 0; i < tokens.length; i++) {
        if (typeof tokens[i] !== 'string') {
          keys[tokens[i].name] = true
        }
      }
      regexp = { toPath, keys }
      cache.set(fullPath, regexp)
      route.fullPath = fullPath
    }

    let url = router.baseUrl + regexp.toPath(params, options) || '/'

    if (options.stringifyQueryParams && params) {
      const queryParams = {}
      const keys = Object.keys(params)