UNPKG

@vve/react-router

Version:

react router for react-helper & with keep-alive

71 lines (70 loc) 2.15 kB
import _toArray from "@babel/runtime/helpers/toArray"; import pathToRegexp from 'path-to-regexp'; var cache = {}; var cacheLimit = 10000; var cacheCount = 0; function compilePath(path, options) { var cacheKey = `${options.end}${options.strict}${options.sensitive}`; var pathCache = cache[cacheKey] || (cache[cacheKey] = {}); if (pathCache[path]) return pathCache[path]; var keys = []; var regexp = pathToRegexp(path, keys, options); var result = { regexp, keys }; if (cacheCount < cacheLimit) { pathCache[path] = result; cacheCount += 1; } return result; } /** * Public API for matching a URL pathname to a path. */ function matchPath(pathname) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (typeof options === 'string') options = { path: options }; var _options = options, path = _options.path, _options$exact = _options.exact, exact = _options$exact === void 0 ? false : _options$exact, _options$strict = _options.strict, strict = _options$strict === void 0 ? false : _options$strict, _options$sensitive = _options.sensitive, sensitive = _options$sensitive === void 0 ? false : _options$sensitive; var paths = [].concat(path); return paths.reduce((matched, p) => { if (matched) return matched; var _compilePath = compilePath(p, { end: exact, strict, sensitive }), regexp = _compilePath.regexp, keys = _compilePath.keys; var match = regexp.exec(pathname); if (!match) return null; var _match = _toArray(match), url = _match[0], values = _match.slice(1); var isExact = pathname === url; if (exact && !isExact) return null; return { path: p, // the p used to match url: p === '/' && url === '' ? '/' : url, // the matched portion of the URL isExact, // whether or not we matched exactly params: keys.reduce((memo, key, index) => { memo[key.name] = values[index]; return memo; }, {}) }; }, null); } export default matchPath; //# sourceMappingURL=match-path.js.map