From 3dc8a17db19b5ed8b46b4b65c6fc1b07b3395fa1 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Fri, 4 Aug 2023 09:16:24 +0100 Subject: [PATCH 1/4] Initial draft for handling named regex param group replacement in "generate" method --- index.d.ts | 1 + src/index.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/index.d.ts b/index.d.ts index 0f731d00..382f76f1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -58,6 +58,7 @@ export type QContext = { }; export type GenerateOptions = { includeRoot: boolean; + replaceRegexGroups: boolean; }; export type RouterOptions = ResolveOptions & { linksSelector?: string }; declare class Navigo { diff --git a/src/index.ts b/src/index.ts index c93384bd..b3e5367a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -312,7 +312,19 @@ export default function Navigo(appRoute?: string, options?: RouterOptions) { if (data) { for (let key in data) { result = result.replace(":" + key, data[key]); + + // If regex group replacement is enabled, search + // for named-regex parameter groups + if (options?.replaceRegexGroups === true) { + let regexEscapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + + // Note that this won't handle regex groups that contain sub-groups + // e.g. /(?(:?example)?) + let keyRegex = new RegExp(`\(\?\<${regexEscapedKey}\>[^\(^\)]+\)`); + result = result.replace(keyRegex, data[key]); + } } + } result = !result.match(/^\//) ? `/${result}` : result; } From 868e28472f2faee0276ecba91b1ff2f08f6ea2a1 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Fri, 4 Aug 2023 09:18:04 +0100 Subject: [PATCH 2/4] Revert "Update setLocationPath.ts" This causes errors: Uncaught ReferenceError: getCurrentURLPath is not defined This reverts commit 0aed88ea97daae83de0a48822c0ccb608bcbf319. --- src/middlewares/setLocationPath.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/middlewares/setLocationPath.ts b/src/middlewares/setLocationPath.ts index 3210796c..475cb205 100644 --- a/src/middlewares/setLocationPath.ts +++ b/src/middlewares/setLocationPath.ts @@ -1,15 +1,14 @@ import { QContext } from "../../index"; import { getCurrentEnvURL } from "../utils"; -export default function setLocationPath(context: QContext, done: () => void): void { - - const { currentLocationPath, instance } = context; - - if (typeof currentLocationPath === "undefined") { - context.currentLocationPath = context.to = getCurrentURLPath(instance.root); +export default function setLocationPath(context: QContext, done) { + if (typeof context.currentLocationPath === "undefined") { + context.currentLocationPath = context.to = getCurrentEnvURL( + context.instance.root + ); } - - context.currentLocationPath = instance._checkForAHash(currentLocationPath); - + context.currentLocationPath = context.instance._checkForAHash( + context.currentLocationPath + ); done(); } From 35b8f463b80c6cf171d972736a17208e78f29c96 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Fri, 4 Aug 2023 09:39:56 +0100 Subject: [PATCH 3/4] Fix double escaping of escaped regex characters --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b3e5367a..75ae46d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -320,7 +320,7 @@ export default function Navigo(appRoute?: string, options?: RouterOptions) { // Note that this won't handle regex groups that contain sub-groups // e.g. /(?(:?example)?) - let keyRegex = new RegExp(`\(\?\<${regexEscapedKey}\>[^\(^\)]+\)`); + let keyRegex = new RegExp(`\\(\\?\\<${regexEscapedKey}\\>[^\\(^\\)]+\\)`, "g"); result = result.replace(keyRegex, data[key]); } } From eb6d45806674cc54150a6b68ce03e9f984e66b9f Mon Sep 17 00:00:00 2001 From: Matthew John Date: Fri, 4 Aug 2023 09:40:16 +0100 Subject: [PATCH 4/4] Use const where possible --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 75ae46d9..f51eee14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -316,11 +316,11 @@ export default function Navigo(appRoute?: string, options?: RouterOptions) { // If regex group replacement is enabled, search // for named-regex parameter groups if (options?.replaceRegexGroups === true) { - let regexEscapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const regexEscapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // Note that this won't handle regex groups that contain sub-groups // e.g. /(?(:?example)?) - let keyRegex = new RegExp(`\\(\\?\\<${regexEscapedKey}\\>[^\\(^\\)]+\\)`, "g"); + const keyRegex = new RegExp(`\\(\\?\\<${regexEscapedKey}\\>[^\\(^\\)]+\\)`, "g"); result = result.replace(keyRegex, data[key]); } }