How to use the path-to-regexp.compile 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 spaceavocado / svelte-router / src / router.js View on Github external
}

      // Generate the regex matcher and params keys
      routes[i].paramKeys = [];
      // Any URL
      if (routes[i].path == '*') {
        routes[i].matcher = /.*/i;
        routes[i].generator = () => '/';
      // Regex based
      } else {
        routes[i].matcher = pathToRegexp(
            routes[i].path,
            routes[i].paramKeys, {
              end: routes[i].children.length == 0,
            });
        routes[i].generator = pathToRegexp.compile(routes[i].path);
      }

      // Process children
      if (routes[i].children.length > 0) {
        this._preprocessRoutes(routes[i].children, routes[i]);
      }
    }
  }
github yqsailor / react-antd-admin / src / utils / request.js View on Github external
let {
    data,
    url,
  } = options;

  const cloneData = lodash.cloneDeep(data);

  try {
    let domin = '';
    if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
      domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0];
      url = url.slice(domin.length);
    }
    const match = pathToRegexp.parse(url);
    url = pathToRegexp.compile(url)(data);
    for (const item of match) {
      // eslint-disable-next-line
      if (item instanceof Object && item.name in cloneData) {
        delete cloneData[item.name];
      }
    }
    url = domin + url;
  } catch (e) {
    message.error(e.message);
  }

  if (fetchType === 'JSONP') {
    return new Promise((resolve, reject) => {
      jsonp(url, {
        param: `${qs.stringify(data)}&callback`,
        name: `jsonp_${new Date().getTime()}`,
github kythe / kythe / kythe / typescript / languageserver / languageserver / src / pathContext.ts View on Github external
function compilePath(path: string): PathMapping {
  // Prefix all the path patterns with / because path-to-regexp requires one
  path = '/' + path;

  const cons = p2r.compile(path);

  // Because the patterns were prefixed with slashes, we remove them on construction
  return {re: p2r(path), cons: (params: Params) => cons(params).substring(1)};
}
github Fiser12 / React-Redux-Typescript-Skeleton / src / core / i18n.ts View on Github external
export const generateLanguage = (locale, location) => {
    const ROUTE = "/:locale/:path*";
    const definePath = compile(ROUTE);
    const routeComponents = PathToRegexp(ROUTE).exec(location.pathname);

    let subPaths = null;
    if (routeComponents && routeComponents[2]) {
        subPaths = routeComponents[2].split("/");
    }

    return definePath({
        locale,
        path: subPaths,
    });
};
github cerebral / url-mapper / compileRoute.js View on Github external
function compileRoute (route, options) {
  var re
  var compiled
  var keys = []
  var querySeparator = options.querySeparator || '?'

  re = pathToRegexp(route, keys)
  keys = keys.map(getKeyName)
  compiled = pathToRegexp.compile(route)

  return {
    parse: function (url) {
      var path = url
      var result = {}

      if (~path.indexOf('#') && !~querySeparator.indexOf('#')) {
        path = path.split('#')[0]
      }

      if (~path.indexOf(querySeparator)) {
        if (options.query) {
          var queryString = '$' + path.slice(path.indexOf(querySeparator) + querySeparator.length)
          result = URLON.parse(queryString)
        }
        path = path.split(querySeparator)[0]
github feross / bitmidi.com / src / lib / router.js View on Github external
routes.forEach(route => {
      const [name, path] = route
      this._compilers[name] = pathToRegexp.compile(path)
    })
  }
github wwwy3y3 / coren / client / ssr / routeParams.js View on Github external
constructor({url, dataProvider = () => Promise.resolve([])}) {
    this.url = url;
    this.toPath = pathToRegexp.compile(this.url);
    this.dataProvider = dataProvider;
  }
github inca / voie / src / state.js View on Github external
spec.path = spec.url;
    }
    this.path = spec.path || '';
    if (this.path.indexOf('/') === 0) {
      this.fullPath = this.path;
    } else {
      const parentPath = this.parentState ? this.parentState.fullPath : '/';
      this.fullPath = parentPath.replace(/\/+$/, '') +
        (this.path ? '/' + this.path : '');
    }
    if (!this.fullPath) {
      this.fullPath = '/';
    }
    this._pathParams = [];
    this._pathRegex = pathToRegexp(this.fullPath, this._pathParams);
    this._pathFormat = pathToRegexp.compile(this.fullPath);
  }
github recruit-tech / agreed / lib / check / extract.js View on Github external
function tryCompilePath(path, values) {
  try {
    return pathToRegexp.compile(path)(values);
  } catch (e) {
    return path.replace(/:/g, "");
  }
}
github poplarjs / poplar / lib / api_method.js View on Github external
ApiMethod.prototype.makeHref = function(query) {
  query = query || {};
  if (!this._toPath) this._toPath = pathToRegexp.compile(this.fullPath());
  return this._toPath(query);
};