Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getCurrentURLPath error and provide ability for named regex group replacement in generate method #333

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type QContext = {
};
export type GenerateOptions = {
includeRoot: boolean;
replaceRegexGroups: boolean;
};
export type RouterOptions = ResolveOptions & { linksSelector?: string };
declare class Navigo {
Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
const regexEscapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

// Note that this won't handle regex groups that contain sub-groups
// e.g. /(?<groupName>(:?example)?)
const keyRegex = new RegExp(`\\(\\?\\<${regexEscapedKey}\\>[^\\(^\\)]+\\)`, "g");
result = result.replace(keyRegex, data[key]);
}
}

}
result = !result.match(/^\//) ? `/${result}` : result;
}
Expand Down
17 changes: 8 additions & 9 deletions src/middlewares/setLocationPath.ts
Original file line number Diff line number Diff line change
@@ -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();
}