How to use the path-to-regexp 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 herton7362 / dynamic-menu-pro / src / common / router.js View on Github external
Object.keys(routerConfig).forEach(path => {
    // Regular match item name
    // eg.  router /user/:id === /user/chen
    const pathRegexp = pathToRegexp(path);
    const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
    let menuItem = {};
    // If menuKey is not empty
    if (menuKey) {
      menuItem = menuData[menuKey];
    }
    let router = routerConfig[path];
    // If you need to configure complex parameter routing,
    // https://github.com/ant-design/ant-design-pro-site/blob/master/docs/router-and-nav.md#%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%E8%B7%AF%E7%94%B1%E8%8F%9C%E5%8D%95
    // eg . /list/:type/user/info/:id
    router = {
      ...router,
      name: router.name || menuItem.name,
      authority: router.authority || menuItem.authority,
      hideInBreadcrumb: router.hideInBreadcrumb || menuItem.hideInBreadcrumb,
    };
github Dromara / soul-dashboard / src / common / router.js View on Github external
Object.keys(routerConfig).forEach(path => {
    // Regular match item name
    // eg.  router /user/:id === /user/chen
    const pathRegexp = pathToRegexp(path);
    const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
    let menuItem = {};
    // If menuKey is not empty
    if (menuKey) {
      menuItem = menuData[menuKey];
    }
    let router = routerConfig[path];
    // If you need to configure complex parameter routing,
    // https://github.com/ant-design/ant-design-pro-site/blob/master/docs/router-and-nav.md#%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%E8%B7%AF%E7%94%B1%E8%8F%9C%E5%8D%95
    // eg . /list/:type/user/info/:id
    router = {
      ...router,
      name: router.name || menuItem.name,
      authority: router.authority || menuItem.authority,
      hideInBreadcrumb: router.hideInBreadcrumb || menuItem.hideInBreadcrumb,
    };
github thinkjs / pharos / www / static / src / models / perf.js View on Github external
const { pathname, query } = routing;
      let param = {
        ...query,
        site_id: currentSite.id,
        // end_time: moment().format('YYYY-MM-DD'),//最近7天
        // start_time: moment().subtract(7, 'days').format('YYYY-MM-DD')
      }
      // if (!query.site_id && sites.length > 0) {
      //   yield put(routerRedux.push({
      //     pathname,
      //     query: param
      //   }))
      //   return;
      // }
      if (pathname.indexOf('/perf/specific') > -1 && !param.type) {
        const match = pathToRegexp('/perf/specific/:type').exec(location.pathname);  
        if (match) {
          param.type = match[1];
        }
      }
      yield put({ type: 'query', payload: param });
    },
    *query({ payload = {} }, { call, put }) {
github reactioncommerce / reaction / imports / plugins / core / router / client / browserRouter.js View on Github external
// If no matching path is found, fetch the not-found route definition
    if (foundPaths.length === 0 && location.pathname !== "not-found") {
      foundPaths = Router.routes.filter((pathObject) => matchPath("/not-found", {
        path: pathObject.route,
        exact: true
      }));
    }

    // If we have a found path, take the first match
    const foundPath = foundPaths.length && foundPaths[0];
    const params = {};

    // Process the params from the found path definiton
    if (foundPath) {
      const keys = [];
      const re = pathToRegexp(foundPath.route, keys); // Create parser with route regex
      const values = re.exec(location.pathname); // Process values

      // Create params object
      keys.forEach((key, index) => {
        params[key.name] = values[index + 1];
      });
    }

    // Get serach (query) string from current location
    let { search } = location;

    // Remove the ? if it exists at the beginning
    if (typeof search === "string" && search.startsWith("?")) {
      search = search.substr(1);
    }
github ant-design / ant-design-pro-layout / example / src / components / PageHeaderWrapper / Breadcrumb.tsx View on Github external
Object.keys(breadcrumbNameMap).forEach(item => {
      if (pathToRegexp(item).test(url)) {
        breadcrumb = breadcrumbNameMap[item];
      }
    });
  }
github cuiods / WIFIProbe / StatDisplay / src / models / customerFlow / customerFlowInfo.js View on Github external
history.listen(location => {
        const matchFlow = pathToRegexp('/customerFlow').exec(location.pathname);
        if(matchFlow) {
          const currentDate = new Date();
          const currentHour = parseInt(currentDate.valueOf()/(1000*60*60));//距离现在最近的整点对应的小时数
          const beforeHour = currentHour-5*24;//获取距离最近整点前5天的数据
          console.log("currentHour :"+currentHour+";beforehour:"+beforeHour);
          dispatch({
            type: 'getFlow',
            payload: {probeId:"1s12sz",startHour:beforeHour,startRange:5,threshold:"DAY"}
          });
          dispatch({
            type: 'getDetail',
            payload: {hour:currentHour-1,probeId:"1s12sz"}
          });
          dispatch({
            type: 'getProbeOptions',
            payload: {page:0, size: 10}
github Banou26 / HNK / dist / esm-oz.js View on Github external
} = {}) => routes.map(route => Array.isArray(route.path) ? route.path.map(path => _objectSpread({}, route, {
  regex: pathToRegexp(path, [], {
    end: false
  }),
  resolve: ((toPath, params) => toPath(params)).bind(undefined, compile(path))
})) : _objectSpread({}, route, {
  regex: pathToRegexp(route.path, [], {
github cam-inc / viron / s / core / router.js View on Github external
resolveCurrentPath(pattern) {
    const keys = [];
    const regexp = pathToRegexp(pattern, keys);
    const pathname = this.getCurrentLocation().pathname;
    const params = {};
    try {
      const list = regexp.exec(pathname).slice(1);
      forEach(keys, (v, i) => {
        params[v.name] = list[i];
      });
    } catch(e) {
      throw new Error(`couldn't parse. pattern was "${pattern}" and pathname was "${pathname}"`);
    }

    return params;
  }
github webiny / webiny-js / packages / webiny-react-router / src / utils / matchPath.js View on Github external
const compilePath = (pattern, options) => {
    if (pattern === "*") {
        pattern = "(.*)";
    }

    const cacheKey = `${options.end}${options.strict}${options.sensitive}`;
    const cache = patternCache[cacheKey] || (patternCache[cacheKey] = {});

    if (cache[pattern]) return cache[pattern];

    const keys = [];
    const re = pathToRegexp(pattern, keys, options);
    const compiledPattern = { re, keys };

    if (cacheCount < cacheLimit) {
        cache[pattern] = compiledPattern;
        cacheCount++;
    }

    return compiledPattern;
};
github cibuci / cibuci-dva / src / models / topic / index.js View on Github external
return history.listen(({ pathname }) => {
        const match = pathToRegexp('/topic/:itemId').exec(pathname);
        if (match) {
          const id = match[1];
          if (id !== 'add') {
            dispatch({ type: 'fetchItem', payload: { id } });
            dispatch({ type: 'fetchComments', payload: { id } });
          }
        }
      });
    },