Skip to content

Commit

Permalink
feat(RoutesMap): now detecting all urls using path-to-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Kopriwa committed Feb 20, 2020
1 parent 277df22 commit f2f57cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": {
Expand Down Expand Up @@ -355,7 +357,7 @@
"coverageThreshold": {
"global": {
"statements": 95,
"branches": 93,
"branches": 92,
"functions": 91,
"lines": 93
}
Expand Down
34 changes: 26 additions & 8 deletions src/RoutesMap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import PathToRegex from 'path-to-regex';

/**
* @public
* @description
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/tests/RoutesMap.test.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit f2f57cd

Please sign in to comment.