Commit 6e1d3d0b by Wee

fix(test): use original version test of rr4

parent a614346b
......@@ -41,17 +41,17 @@ class Switch extends React.Component {
render() {
const { route } = this.context.router;
const { children } = this.props;
const location = this.props.location || route.location; // 可以手动指定 location
const location = this.props.location || route.location;
let match, child;
React.Children.forEach(children, element => {
if (match == null && React.isValidElement(element)) {
const {
path: pathProp, // path
exact, // 路径完全匹配
strict, // 是否结尾匹配带/的路径
sensitive, // 大小写敏感
from // path 的备胎
path: pathProp,
exact,
strict,
sensitive,
from
} = element.props;
const path = pathProp || from;
......@@ -65,7 +65,7 @@ class Switch extends React.Component {
});
return match
? React.cloneElement(child, { location, computedMatch: match }) // 重新计算两个参数
? React.cloneElement(child, { location, computedMatch: match })
: null;
}
}
......
......@@ -5,6 +5,7 @@ import PropTypes from "prop-types";
import StaticRouter from "../StaticRouter";
import Redirect from "../Redirect";
import Route from "../../LiveRoute";
// import Route from "../Route";
import Prompt from "../Prompt";
describe("A <StaticRouter>", () => {
......
......@@ -28,7 +28,7 @@ const generatePath = (pattern = "/", params = {}) => {
return pattern;
}
const generator = compileGenerator(pattern);
return generator(params);
return generator(params, { pretty: true });
};
export default generatePath;
import pathToRegexp from "path-to-regexp";
// ES6 的 import 会共享,所以可以理解为一个全局缓存
// 缓存的结构是 option 对象下的 pattern
const patternCache = {};
const cacheLimit = 10000;
let cacheCount = 0;
......@@ -16,12 +14,6 @@ const compilePath = (pattern, options) => {
const re = pathToRegexp(pattern, keys, options);
const compiledPattern = { re, keys };
// 这是 path-to-regex 的返回值
// var keys = []
// var re = pathToRegexp('/foo/:bar', keys)
// re = /^\/foo\/([^\/]+?)\/?$/i
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
if (cacheCount < cacheLimit) {
cache[pattern] = compiledPattern;
cacheCount++;
......@@ -40,18 +32,14 @@ const matchPath = (pathname, options = {}, parent) => {
if (path == null) return parent;
// path-to-regex 的 end 相当于就是 router 的 exact,相当于是否使用全局匹配 /g
const { re, keys } = compilePath(path, { end: exact, strict, sensitive });
// 使用生成的正则表达式去匹配
const match = re.exec(pathname);
// 不匹配直接返回
if (!match) return null;
const [url, ...values] = match;
const isExact = pathname === url;
// 如果在要求 exact 匹配时为非 exact 的匹配,则直接返回
if (exact && !isExact) return null;
return {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment