From a9eaf1c7d2597bb67dba458dfb6f60bb8fa2d975 Mon Sep 17 00:00:00 2001 From: NikolaBorislavovHristov Date: Tue, 27 Aug 2019 12:58:53 +0300 Subject: [PATCH] use match instead of exec in Router --- src/router/Router/Router.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/router/Router/Router.js b/src/router/Router/Router.js index c12fd80b9..5110902b3 100644 --- a/src/router/Router/Router.js +++ b/src/router/Router/Router.js @@ -13,11 +13,13 @@ const Router = ({ homePath, viewsConfig, onPathNotMatch }) => { }); }); const routeConfigForPath = React.useCallback((path) => { - for (const viewConfig of viewsConfig) { - for (const routeConfig of viewConfig) { - const match = routeConfig.regexp.exec(path); - if (match) { - return routeConfig; + if (typeof path === 'string') { + for (const viewConfig of viewsConfig) { + for (const routeConfig of viewConfig) { + const match = path.match(routeConfig.regexp); + if (match) { + return routeConfig; + } } } } @@ -36,7 +38,7 @@ const Router = ({ homePath, viewsConfig, onPathNotMatch }) => { } const routeViewIndex = viewsConfig.findIndex((v) => v.includes(routeConfig)); - const match = routeConfig.regexp.exec(pathname); + const match = pathname.match(routeConfig.regexp); const queryParams = Array.from(new URLSearchParams(query !== null ? query : '').entries()) .reduce((result, [key, value]) => { result[key] = value; @@ -108,9 +110,7 @@ Router.propTypes = { homePath: PropTypes.string, onPathNotMatch: PropTypes.func, viewsConfig: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.exact({ - regexp: PropTypes.shape({ - exec: PropTypes.func.isRequired - }).isRequired, + regexp: PropTypes.instanceOf(RegExp).isRequired, keys: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string.isRequired })).isRequired,