Skip to content

Commit

Permalink
Merge pull request #57 from vordgi/get-56
Browse files Browse the repository at this point in the history
get-56 improve groups parsing in get-params
  • Loading branch information
vordgi authored May 1, 2024
2 parents 0f4d5e6 + 3a76304 commit 1cbcc58
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
9 changes: 2 additions & 7 deletions package/src/get-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ export const getParams = () => {

const cleanUrlPathname = normalizePathname(urlPathname);
const cleanPagePath = normalizePagePath(pagePath);
const pagePathParts = cleanPagePath
.split("/")
.slice(1)
.filter((part) => !part.match(/^(\([^)]+\)|\@.+)$/));
const pagePathParts = cleanPagePath.split("/");
const pagePathInterceptedParts = normalizeInterceptingRoutes(pagePathParts);
const pathnameParts = cleanUrlPathname.split("/").slice(1);
const pathnameParts = cleanUrlPathname.split("/");

const isRootPage = cleanUrlPathname === "" && cleanPagePath === "";
const isNotFoundPage = pagePath.match(/\/_not-found\/?$/);
const isValidCatchALl =
cleanPagePath.match(/\/\[\.\.\.[^\]]+\]/) && pathnameParts.length >= pagePathInterceptedParts.length;
const isValidOptionalCatchALl =
cleanPagePath.match(/\/\[\[\.\.\.[^\]]+\]\]/) && pathnameParts.length >= pagePathInterceptedParts.length - 1;
const isCorrectMatched =
isRootPage ||
isNotFoundPage ||
pagePathInterceptedParts.length === pathnameParts.length ||
isValidCatchALl ||
Expand Down
3 changes: 2 additions & 1 deletion package/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const normalizePathname = (pathname: string) => {
export const normalizePagePath = (pagePath: string) => {
const cleanPagePath = pagePath && new URL(pagePath, "http://n").pathname;
const pagePathWithoutFileType = cleanPagePath?.replace(/(\/page|\/_not-found)$/, "");
return pagePathWithoutFileType || "/";
const pagePathWithoutGroups = pagePathWithoutFileType.replace(/\/(\([^)]+\)|\@.+)/g, "");
return pagePathWithoutGroups || "/";
};

export const parseSegments = (pagePathParts: string[], pathnameParts: string[]) => {
Expand Down
14 changes: 14 additions & 0 deletions tests/nextjs-app/app/(frontend)/(corporate)/groups/groups.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "@playwright/test";

test("should return correct page params and pathname", async ({ page }) => {
await page.goto("/groups");
page.waitForSelector("#groups-page");

const pathname = await page.$("#get-pathname");
const pathnameRow = await pathname?.textContent();
expect(pathnameRow).toEqual("/groups");

const params = await page.$("#get-params");
const paramsRow = await params?.textContent();
expect(paramsRow && JSON.parse(paramsRow)).toEqual({});
});
23 changes: 23 additions & 0 deletions tests/nextjs-app/app/(frontend)/(corporate)/groups/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";
import { NamespaceServerContext } from "../../../../components/NamespaceServerContext";
import BlockWithContext from "../../../../components/BlockWithContext";
import Nav from "../../../../components/Nav";

export default function Page() {
const pathname = getPathname();
const params = getParams();

return (
<div id="groups-page">
<Nav />
<div>
<p id="get-pathname">{pathname}</p>
<p id="get-params">{JSON.stringify(params)}</p>
<NamespaceServerContext.Provider value={{ namespace: "dynamic-auto" }}>
<BlockWithContext />
</NamespaceServerContext.Provider>
</div>
</div>
);
}

0 comments on commit 1cbcc58

Please sign in to comment.