@b9g/match-pattern 0.1.3 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/package.json +10 -17
- package/src/match-pattern.d.ts +2 -2
- package/src/match-pattern.js +9 -40
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -6
package/README.md
CHANGED
|
@@ -93,20 +93,20 @@ new MatchPattern({
|
|
|
93
93
|
})
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
## Trailing Slash
|
|
96
|
+
## Trailing Slash Handling
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
**Issue:** [kenchris/urlpattern-polyfill#131](https://github.com/kenchris/urlpattern-polyfill/issues/131)
|
|
101
|
-
|
|
102
|
-
**Solution:** ✅ Automatic trailing slash normalization implemented
|
|
98
|
+
MatchPattern does not automatically normalize trailing slashes. Use explicit patterns:
|
|
103
99
|
|
|
104
100
|
```javascript
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
101
|
+
// Exact matching
|
|
102
|
+
const exactPattern = new MatchPattern('/api/posts/:id');
|
|
103
|
+
exactPattern.test('/api/posts/123'); // ✅ true
|
|
104
|
+
exactPattern.test('/api/posts/123/'); // ❌ false
|
|
105
|
+
|
|
106
|
+
// Optional trailing slash
|
|
107
|
+
const flexiblePattern = new MatchPattern('/api/posts/:id{/}?');
|
|
108
|
+
flexiblePattern.test('/api/posts/123'); // ✅ true
|
|
109
|
+
flexiblePattern.test('/api/posts/123/'); // ✅ true
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
## Known Limitations
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b9g/match-pattern",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"devDependencies": {
|
|
5
|
-
"@b9g/libuild": "^0.1.
|
|
5
|
+
"@b9g/libuild": "^0.1.11",
|
|
6
|
+
"bun-types": "latest"
|
|
6
7
|
},
|
|
7
8
|
"peerDependencies": {
|
|
8
9
|
"urlpattern-polyfill": "^10.0.0"
|
|
@@ -13,21 +14,9 @@
|
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
16
|
"type": "module",
|
|
16
|
-
"types": "src/
|
|
17
|
-
"module": "src/
|
|
17
|
+
"types": "src/match-pattern.d.ts",
|
|
18
|
+
"module": "src/match-pattern.js",
|
|
18
19
|
"exports": {
|
|
19
|
-
".": {
|
|
20
|
-
"types": "./src/index.d.ts",
|
|
21
|
-
"import": "./src/index.js"
|
|
22
|
-
},
|
|
23
|
-
"./index": {
|
|
24
|
-
"types": "./src/index.d.ts",
|
|
25
|
-
"import": "./src/index.js"
|
|
26
|
-
},
|
|
27
|
-
"./index.js": {
|
|
28
|
-
"types": "./src/index.d.ts",
|
|
29
|
-
"import": "./src/index.js"
|
|
30
|
-
},
|
|
31
20
|
"./match-pattern": {
|
|
32
21
|
"types": "./src/match-pattern.d.ts",
|
|
33
22
|
"import": "./src/match-pattern.js"
|
|
@@ -36,6 +25,10 @@
|
|
|
36
25
|
"types": "./src/match-pattern.d.ts",
|
|
37
26
|
"import": "./src/match-pattern.js"
|
|
38
27
|
},
|
|
39
|
-
"./package.json": "./package.json"
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./src/match-pattern.d.ts",
|
|
31
|
+
"import": "./src/match-pattern.js"
|
|
32
|
+
}
|
|
40
33
|
}
|
|
41
34
|
}
|
package/src/match-pattern.d.ts
CHANGED
|
@@ -32,11 +32,11 @@ export declare class MatchPattern extends URLPattern {
|
|
|
32
32
|
private _originalInput;
|
|
33
33
|
constructor(input: string | URLPatternInit, baseURL?: string);
|
|
34
34
|
/**
|
|
35
|
-
* Enhanced exec that returns unified params object
|
|
35
|
+
* Enhanced exec that returns unified params object
|
|
36
36
|
*/
|
|
37
37
|
exec(input: string | URL): MatchPatternResult | null;
|
|
38
38
|
/**
|
|
39
|
-
* Enhanced test with order-independent search parameter matching
|
|
39
|
+
* Enhanced test with order-independent search parameter matching
|
|
40
40
|
*/
|
|
41
41
|
test(input: string | URL): boolean;
|
|
42
42
|
}
|
package/src/match-pattern.js
CHANGED
|
@@ -12,47 +12,41 @@ var MatchPattern = class extends URLPattern {
|
|
|
12
12
|
if (typeof input === "string") {
|
|
13
13
|
processedInput = parseStringPattern(input);
|
|
14
14
|
}
|
|
15
|
-
const normalizedInput = normalizePatternTrailingSlash(processedInput);
|
|
16
15
|
if (baseURL !== void 0) {
|
|
17
|
-
super(
|
|
16
|
+
super(processedInput, baseURL);
|
|
18
17
|
} else {
|
|
19
|
-
super(
|
|
18
|
+
super(processedInput);
|
|
20
19
|
}
|
|
21
|
-
this._originalInput =
|
|
20
|
+
this._originalInput = processedInput;
|
|
22
21
|
}
|
|
23
22
|
/**
|
|
24
|
-
* Enhanced exec that returns unified params object
|
|
23
|
+
* Enhanced exec that returns unified params object
|
|
25
24
|
*/
|
|
26
25
|
exec(input) {
|
|
27
26
|
if (!this.test(input)) {
|
|
28
27
|
return null;
|
|
29
28
|
}
|
|
30
|
-
const
|
|
31
|
-
const normalizedUrl = normalizeTrailingSlash(url);
|
|
32
|
-
const result = super.exec(normalizedUrl);
|
|
29
|
+
const result = super.exec(input);
|
|
33
30
|
if (result) {
|
|
34
31
|
const enhancedResult = {
|
|
35
32
|
...result,
|
|
36
33
|
params: extractUnifiedParams(result, input)
|
|
37
|
-
// Use original input for search params
|
|
38
34
|
};
|
|
39
35
|
return enhancedResult;
|
|
40
36
|
}
|
|
41
37
|
return buildCustomResult(this, input);
|
|
42
38
|
}
|
|
43
39
|
/**
|
|
44
|
-
* Enhanced test with order-independent search parameter matching
|
|
40
|
+
* Enhanced test with order-independent search parameter matching
|
|
45
41
|
*/
|
|
46
42
|
test(input) {
|
|
47
43
|
const url = typeof input === "string" ? new URL(input) : input;
|
|
48
|
-
const normalizedUrl = normalizeTrailingSlash(url);
|
|
49
44
|
if (!this.search || this.search === "*") {
|
|
50
|
-
return super.test(
|
|
45
|
+
return super.test(input);
|
|
51
46
|
}
|
|
52
47
|
const pathPatternInit = typeof this._originalInput === "string" ? { pathname: this._originalInput } : { ...this._originalInput, search: void 0 };
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
if (!pathPattern.test(normalizedUrl)) {
|
|
48
|
+
const pathPattern = new URLPattern(pathPatternInit);
|
|
49
|
+
if (!pathPattern.test(input)) {
|
|
56
50
|
return false;
|
|
57
51
|
}
|
|
58
52
|
return testSearchParameters(this.search, url.searchParams);
|
|
@@ -113,31 +107,6 @@ function extractUnifiedParams(result, url) {
|
|
|
113
107
|
}
|
|
114
108
|
return params;
|
|
115
109
|
}
|
|
116
|
-
function normalizeTrailingSlash(url) {
|
|
117
|
-
const normalized = new URL(url.href);
|
|
118
|
-
if (normalized.pathname === "/") {
|
|
119
|
-
return normalized;
|
|
120
|
-
}
|
|
121
|
-
if (normalized.pathname.endsWith("/")) {
|
|
122
|
-
normalized.pathname = normalized.pathname.slice(0, -1);
|
|
123
|
-
}
|
|
124
|
-
return normalized;
|
|
125
|
-
}
|
|
126
|
-
function normalizePatternTrailingSlash(patternInit) {
|
|
127
|
-
if (typeof patternInit === "string") {
|
|
128
|
-
if (patternInit === "/" || patternInit === "") {
|
|
129
|
-
return patternInit;
|
|
130
|
-
}
|
|
131
|
-
return patternInit.endsWith("/") ? patternInit.slice(0, -1) : patternInit;
|
|
132
|
-
}
|
|
133
|
-
const normalized = { ...patternInit };
|
|
134
|
-
if (normalized.pathname && normalized.pathname !== "/") {
|
|
135
|
-
if (normalized.pathname.endsWith("/")) {
|
|
136
|
-
normalized.pathname = normalized.pathname.slice(0, -1);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return normalized;
|
|
140
|
-
}
|
|
141
110
|
function buildCustomResult(pattern, input) {
|
|
142
111
|
const url = typeof input === "string" ? new URL(input) : input;
|
|
143
112
|
const result = {
|
package/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { MatchPattern, type MatchPatternResult } from "./match-pattern.ts";
|