Skip to content

Commit 70eaaf2

Browse files
committed
Remove normalizePathname from library
1 parent fe645f9 commit 70eaaf2

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

Readme.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,35 @@ match("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
162162
match("/invalid"); //=> false
163163
```
164164

165-
### Normalize Pathname
165+
#### Normalize Pathname
166166

167-
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`:
167+
You should make sure variations of the same path to match your input `path`. Here's one possible solution:
168168

169169
```js
170+
/**
171+
* Normalize a pathname for matching, replaces multiple slashes with a single
172+
* slash and normalizes unicode characters to "NFC". When using this method,
173+
* `decode` should be an identity function so you don't decode strings twice.
174+
*/
175+
function normalizePathname(pathname: string) {
176+
return (
177+
decodeURI(pathname)
178+
// Replaces repeated slashes in the URL.
179+
.replace(/\/+/g, "/")
180+
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
181+
// Note: Missing native IE support, may want to skip this step.
182+
.normalize()
183+
);
184+
}
185+
170186
const re = pathToRegexp("/caf\u00E9");
171-
const input = encodeURI("/caf\u00E9");
187+
const input = encodeURI("/cafe\u0301");
172188

173189
re.test(input); //=> false
174190
re.test(normalizePathname(input)); //=> true
175191
```
176192

177-
**Note:** It may be preferable to implement something in your own library that normalizes the pathname for matching. E.g. [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically URI encodes paths for you, which would result in a consistent match.
178-
179-
**Tip:** Consider using [`String.prototype.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) to resolve unicode variants of the same string.
193+
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you, which would result in a consistent match if you use `encodeURI` in `pathToRegexp` options.
180194

181195
### Parse
182196

src/index.spec.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,30 +2842,6 @@ describe("path-to-regexp", function() {
28422842
);
28432843
});
28442844
});
2845-
2846-
describe("normalize pathname", function() {
2847-
it("should match normalized pathnames", function() {
2848-
const re = pathToRegexp.pathToRegexp("/caf\u00E9");
2849-
const input = encodeURI("/cafe\u0301");
2850-
2851-
expect(exec(re, input)).toEqual(null);
2852-
expect(
2853-
exec(re, pathToRegexp.normalizePathname(input).normalize())
2854-
).toEqual(["/caf\u00E9"]);
2855-
});
2856-
2857-
it("should not normalize encoded slash", function() {
2858-
const input = "/test/route%2F";
2859-
2860-
expect(pathToRegexp.normalizePathname(input)).toEqual("/test/route%2F");
2861-
});
2862-
2863-
it("should fix repeated slashes", function() {
2864-
const input = encodeURI("/test///route");
2865-
2866-
expect(pathToRegexp.normalizePathname(input)).toEqual("/test/route");
2867-
});
2868-
});
28692845
});
28702846

28712847
/**

src/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ export interface ParseOptions {
1414
whitelist?: string | string[];
1515
}
1616

17-
/**
18-
* Normalize a pathname for matching, replaces multiple slashes with a single
19-
* slash and normalizes unicode characters to "NFC". When using this method,
20-
* `decode` should be an identity function so you don't decode strings twice.
21-
*/
22-
export function normalizePathname(pathname: string) {
23-
return decodeURI(pathname).replace(/\/+/g, "/");
24-
}
25-
2617
/**
2718
* Balanced bracket helper function.
2819
*/

0 commit comments

Comments
 (0)