diff --git a/package.json b/package.json index 27593f2..7523dc9 100644 --- a/package.json +++ b/package.json @@ -186,13 +186,13 @@ "@babel/plugin-transform-runtime": "^7.8.3", "@babel/preset-env": "^7.8.4", "@babel/preset-react": "^7.8.3", - "@rollup-umd/documentation": "^2.0.13", - "@rollup-umd/rollup": "^1.1.1", + "@rollup-umd/documentation": "^2.1.1", "@rollup-umd/ncu": "^1.0.9", + "@rollup-umd/rollup": "^1.1.1", "@semantic-release/changelog": "^5.0.0", "@semantic-release/git": "^9.0.0", "@semantic-release/github": "^7.0.3", - "@semantic-release/npm": "^7.0.2", + "@semantic-release/npm": "^7.0.3", "@yeutech-lab/documentation": "^2.0.2", "babel-eslint": "^10.0.3", "babel-jest": "^25.1.0", @@ -223,10 +223,11 @@ "react": "^16.8.4", "react-dom": "^16.8.4", "react-router": "^5.1.2", + "react-router-dom": "^5.1.2", "react-test-renderer": "^16.12.0", - "semantic-release": "^17.0.2", + "semantic-release": "^17.0.4", "toctoc": "^0.3.2", - "webpack": "^4.41.5" + "webpack": "^4.41.6" }, "dependencies": { "@babel/runtime": "^7.8.4", @@ -237,6 +238,7 @@ "lodash.get": "^4.4.2", "lodash.omit": "^4.5.0", "lodash.set": "^4.3.2", + "path-to-regex": "^1.3.1", "prop-types": "^15.7.2" }, "peerDependencies": { @@ -355,7 +357,7 @@ "coverageThreshold": { "global": { "statements": 95, - "branches": 93, + "branches": 92, "functions": 91, "lines": 93 } diff --git a/src/RoutesMap.js b/src/RoutesMap.js index aab0970..ac48fa3 100644 --- a/src/RoutesMap.js +++ b/src/RoutesMap.js @@ -1,3 +1,5 @@ +import PathToRegex from 'path-to-regex'; + /** * @public * @description @@ -33,18 +35,34 @@ export default class RoutesMap extends Map { * @description * The get method can get a routes that use params using a path with params set. * @param key - It can get from the usual key or a routes with params such as `/users/1` - * @return {V} - The route configuration object of the route, or undefined if not found + * @return {*} - The route configuration object of the route, or undefined if not found */ get(key) { - let match; - // exact match + const resultList = []; if (this.has(key)) return super.get(key); + for (let path of this.keys()) { // eslint-disable-line no-restricted-syntax, prefer-const + const parser = new PathToRegex(path); + const result = parser.match(key); + if (result) { + resultList.push(path); + } + } - // not exact match, need to apply logic - for (let route of this.keys()) { // eslint-disable-line no-restricted-syntax, prefer-const - const reg = new RegExp(`^${route.replace(/:[-\w.]+/g, '\\w+')}$`); - if (!match && reg.test(key)) match = route; + if (resultList.length === 0) { + return undefined; + } + const match = super.get(resultList[0]); + if (resultList.length > 1) { + console.log(`You have multiple route that match "${key}" in your configuration + Please verify your configuration: + + ${JSON.stringify(resultList, null, 2)} + + We have selected the first match ${JSON.stringify(match, null, 2)} + + You can ask support by opening a new issue: https://github.com/yeutech-lab/react-router-dom-utils/issues/new + `); // eslint-disable-line no-console } - return super.get(match); + return match; } } diff --git a/src/tests/RoutesMap.test.js b/src/tests/RoutesMap.test.js index d598fcf..3ff7417 100644 --- a/src/tests/RoutesMap.test.js +++ b/src/tests/RoutesMap.test.js @@ -1,7 +1,7 @@ import RoutesMap from '../RoutesMap'; describe('RoutesMap', () => { - it('should create a routes map', () => { + it.only('should create a routes map', () => { const routesMap = new RoutesMap([ ['/404.html', { name: 'notFound',