Skip to content

Commit

Permalink
Merge pull request #8 from marvinhagemeister/next2
Browse files Browse the repository at this point in the history
3.0.0 Milestone
  • Loading branch information
marvinhagemeister authored May 21, 2019
2 parents dfac00e + 2ddc522 commit 90cfef3
Show file tree
Hide file tree
Showing 5 changed files with 534 additions and 470 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.0.0

- **breaking:** URLs are now case-sensitive to be complient with the RFC3986
- Add support for custom variables in query parameters (`/foo?bar=:id`)
- Switch back from `mjs` to `js` for better ecosystem compatibility

## 2.0.0

- **breaking:** Refactored exact match handling.
Expand Down
32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@marvinh/path-to-regexp",
"amdName": "pathToRegExp",
"version": "2.0.0",
"version": "3.0.0",
"description": "Ultra small and fast path to regex generation",
"main": "dist/index.js",
"umd:main": "dist/index.umd.js",
"module": "dist/index.mjs",
"module": "dist/index.module.js",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"author": "Marvin Hagemeister <marvin@marvinhagemeister.de>",
Expand All @@ -18,26 +18,25 @@
"test-watch": "npm t -- --watch",
"coverage": "npm t -- --coverage",
"prepublishOnly": "npm run lint && npm t && npm run build",
"bench": "ts-node -P benchmark/tsconfig.bench.json benchmark/index.ts",
"precommit": "lint-staged"
"bench": "ts-node -P benchmark/tsconfig.bench.json benchmark/index.ts"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"benchmarkjs-pretty": "^2.0.0",
"husky": "^1.3.1",
"jest": "^24.7.1",
"lint-staged": "^8.1.5",
"husky": "^2.3.0",
"jest": "^24.8.0",
"lint-staged": "^8.1.7",
"microbundle": "^0.11.0",
"path-to-regexp": "^3.0.0",
"prettier": "^1.16.4",
"prettier": "^1.17.1",
"rimraf": "^2.6.3",
"ts-jest": "^24.0.1",
"ts-node": "^8.0.3",
"tslint": "^5.15.0",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"tslint-config-sevenval": "^2.0.0",
"typescript": "^3.4.1",
"typescript": "^3.4.5",
"url-pattern": "^1.0.3"
},
"lint-staged": {
Expand All @@ -46,5 +45,10 @@
"npm run lint",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
34 changes: 31 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ describe("pathToRegex", () => {
expect(new PathRegExp("foo/").match("foo2/bar")).toEqual(null);
});

it("should convert to lowercase", () => {
expect(new PathRegExp("fOo/*").match("FOO/asd")).toEqual({
it("should be case sensitive", () => {
expect(new PathRegExp("fOo/*").match("FOO/asd")).toEqual(null);
expect(new PathRegExp("fOo/*").match("fOo/asd")).toEqual({
absolute: false,
path: "fOo/*",
matched: "foo/asd",
matched: "fOo/asd",
params: {
"*": "asd",
},
Expand Down Expand Up @@ -174,6 +175,33 @@ describe("pathToRegex", () => {
path: "/:foo/bar",
});
});

it("should match route with query parameters", () => {
expect(new PathRegExp("/foo?bar=:id", true).match("/foo?bar=123")).toEqual({
absolute: true,
matched: "/foo?bar=123",
params: {
id: "123",
},
path: "/foo?bar=:id",
});
});

it("should match route with query parameters #2", () => {
expect(
new PathRegExp("/foo?bar=:id&bob=:id2", true).match(
"/foo?bar=123&bob=abc",
),
).toEqual({
absolute: true,
matched: "/foo?bar=123&bob=abc",
params: {
id: "123",
id2: "abc",
},
path: "/foo?bar=:id&bob=:id2",
});
});
});

describe("createUrl", () => {
Expand Down
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ const enum CHARS {
COLON = 58,
DOT = 46,
SLASH = 47,
QUESTION_MARK = 63,
AMPERSAND = 38,
}

const escape: Record<any, string> = {
[CHARS.ASTERIKS]: "(.*)",
[CHARS.DOT]: ".",
[CHARS.SLASH]: "\\/",
[CHARS.QUESTION_MARK]: "\\?",
[CHARS.AMPERSAND]: "\\&",
};

export function parse(input: string, exact = false) {
input = normalize(input);
const str = normalize(input);
let reg = "";
let absolute = false;
if (input[0] === "/") {
if (str[0] === "/") {
reg += "^";
absolute = true;
}

const str = input.toLowerCase();

const params: string[] = [];
let param = -1;
const len = str.length;
Expand All @@ -37,11 +39,11 @@ export function parse(input: string, exact = false) {
if (char === CHARS.COLON) {
param = i + 1;
} else if (param !== -1) {
const isSlash = char === CHARS.SLASH;
const isDelimiter = char === CHARS.SLASH || char === CHARS.AMPERSAND;
const isEnd = i === len - 1;
if (isSlash || isEnd) {
params.push(input.slice(param, i));
reg += "([\\w-_.]+)" + (isSlash ? escape[char] : "");
if (isDelimiter || isEnd) {
params.push(input.slice(param, isEnd && !isDelimiter ? len : i));
reg += "([\\w-_.]+)" + (isDelimiter ? escape[char] : "");
param = -1;
}
} else {
Expand All @@ -63,7 +65,8 @@ export function parse(input: string, exact = false) {

function normalize(url: string) {
// Normalize url
return url.length > 1 && url.charCodeAt(url.length - 1) === 47
return url.length > 1 &&
(url.charCodeAt(url.length - 1) === CHARS.SLASH || url.indexOf("=") > -1)
? url
: url + "/";
}
Expand All @@ -85,12 +88,16 @@ export class PathRegExp {
url = normalize(url);

regex.lastIndex = 0;
const res = regex.exec(url.toLowerCase());
const res = regex.exec(url);
// No match
if (res === null) return null;

const match = res[0];
const out: MatchResult = {
matched: res[0].slice(0, -1),
matched:
match.charCodeAt(match.length - 1) === CHARS.SLASH
? match.slice(0, -1)
: match,
params: {},
path: this.path,
absolute: this.absolute,
Expand Down
Loading

0 comments on commit 90cfef3

Please sign in to comment.