Skip to content

Commit

Permalink
Improve regex
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Feb 21, 2023
1 parent edb25b9 commit d5502a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 0 additions & 1 deletion index.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Router = require("./src/router.cjs");
const methods = require("./src/methods.cjs");

module.exports = { Router, ...methods };
4 changes: 2 additions & 2 deletions src/route.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ module.exports = class Route {
return regexp
? new RegExp(`^/?${regexp}/?(?=\/|$)`)
: regexp === ""
? new RegExp("^/?(?=\/|$)")
? new RegExp("^/?(?=/|$)")
: null;
}
return regexp
? new RegExp(`^/?${regexp}/?(?=\/|$)`, "i")
: regexp === ""
? new RegExp("^/?(?=\/|$)", "i")
? new RegExp("^/?(?=/|$)", "i")
: null;
} catch (err) {
throw new TypeError(`Error: ${path} invalid regular expression`);
Expand Down
4 changes: 2 additions & 2 deletions src/route.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ export default class Route {
return regexp
? new RegExp(`^/?${regexp}/?(?=\/|$)`)
: regexp === ""
? new RegExp("^/?(?=\/|$)")
? new RegExp("^/?(?=/|$)")
: null;
}
return regexp
? new RegExp(`^/?${regexp}/?(?=\/|$)`, "i")
: regexp === ""
? new RegExp("^/?(?=\/|$)", "i")
? new RegExp("^/?(?=/|$)", "i")
: null;
} catch (err) {
throw new TypeError(`Error: ${path} invalid regular expression`);
Expand Down
12 changes: 12 additions & 0 deletions tests/routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ describe("Routing test", () => {
.get("{name}/dashboard", function (req, res) {
res.end(req.params.name);
})
.get("/user/{name}/dashboard", function (req, res) {
res.end(req.params.name);
})
.get("/params/{name:([A-Za-z]+)}/{id}", function (req, res) {
res.end(`${req.params.name},${req.params.id}`);
})
Expand Down Expand Up @@ -120,6 +123,15 @@ describe("Routing test", () => {
});
});

test("GET /user/abc/dashboard", async () => {
await request(app.handler())
.get("/user/abc/dashboard")
.expect(200)
.then((res) => {
expect(res.text).toBe("abc");
});
});

test("GET /params/user/1", async () => {
await request(app.handler())
.get("/params/user/1")
Expand Down
12 changes: 12 additions & 0 deletions tests/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe("Routing test", () => {
res.end(req.method.toUpperCase())
).setName("any"),
path("get", "{name}/dashboard", (req, res) => res.end(req.params.name)),
path("get", "/user/{name}/dashboard", (req, res) =>
res.end(req.params.name)
),
path("get", "/params/{name:([A-Za-z]+)}/{id}", (req, res) =>
res.end(`${req.params.name},${req.params.id}`)
).setName("params"),
Expand Down Expand Up @@ -93,6 +96,15 @@ describe("Routing test", () => {
});
});

test("GET /user/abc/dashboard", async () => {
await request(app.handler())
.get("/user/abc/dashboard")
.expect(200)
.then((res) => {
expect(res.text).toBe("abc");
});
});

test("GET /params/user/1", async () => {
await request(app.handler())
.get("/params/user/1")
Expand Down

0 comments on commit d5502a0

Please sign in to comment.