@marianmeres/simple-router 1.9.1 → 2.0.0
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/dist/index.cjs +31 -12
- package/dist/index.js +31 -12
- package/dist/route.d.ts +1 -0
- package/package.json +1 -2
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
class SimpleRoute {
|
|
4
4
|
route;
|
|
5
5
|
static SPLITTER = '/';
|
|
6
|
+
static WILDCARD = '*';
|
|
6
7
|
_parsed;
|
|
7
8
|
constructor(route) {
|
|
8
9
|
this.route = route;
|
|
@@ -25,7 +26,8 @@ class SimpleRoute {
|
|
|
25
26
|
}
|
|
26
27
|
static _parse(route) {
|
|
27
28
|
let wasSpread = false;
|
|
28
|
-
|
|
29
|
+
let wasWildcard = false;
|
|
30
|
+
return SimpleRoute._sanitizeAndSplit(route).reduce((memo, segment, idx, all) => {
|
|
29
31
|
let name = null;
|
|
30
32
|
// if optional, remove trailing '?' marker
|
|
31
33
|
let isOptional = segment.endsWith('?');
|
|
@@ -40,20 +42,33 @@ class SimpleRoute {
|
|
|
40
42
|
throw new Error('Multiple spread segments are invalid');
|
|
41
43
|
}
|
|
42
44
|
wasSpread = true;
|
|
43
|
-
segment = '[' + segment.
|
|
45
|
+
segment = '[' + segment.slice(4);
|
|
44
46
|
}
|
|
45
47
|
let test = new RegExp('^' + SimpleRoute._escapeRegExp(segment) + '$');
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
test = /.+/;
|
|
51
|
-
// id([0-9]+)
|
|
52
|
-
let m2 = m[1].match(/^(\w.*)\((.+)\)$/);
|
|
53
|
-
if (m2) {
|
|
54
|
-
name = m2[1];
|
|
55
|
-
test = new RegExp('^' + m2[2] + '$');
|
|
48
|
+
// catch all wildcard
|
|
49
|
+
if (segment === SimpleRoute.WILDCARD) {
|
|
50
|
+
if (idx < all.length - 1) {
|
|
51
|
+
throw new Error(`Wildcard '${SimpleRoute.WILDCARD}' can be used only as a last segment`);
|
|
56
52
|
}
|
|
53
|
+
wasWildcard = true;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// starting with at least one word char within brackets...
|
|
57
|
+
let m = segment.match(/^\[(\w.*)]$/);
|
|
58
|
+
if (m) {
|
|
59
|
+
name = m[1];
|
|
60
|
+
test = /.+/;
|
|
61
|
+
// id([0-9]+)
|
|
62
|
+
let m2 = m[1].match(/^(\w.*)\((.+)\)$/);
|
|
63
|
+
if (m2) {
|
|
64
|
+
name = m2[1];
|
|
65
|
+
test = new RegExp('^' + m2[2] + '$');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (wasWildcard) {
|
|
70
|
+
isOptional = true;
|
|
71
|
+
test = /.*/;
|
|
57
72
|
}
|
|
58
73
|
memo.push({ segment, name, test, isOptional, isSpread });
|
|
59
74
|
return memo;
|
|
@@ -105,6 +120,10 @@ class SimpleRoute {
|
|
|
105
120
|
});
|
|
106
121
|
segments = newSegments;
|
|
107
122
|
}
|
|
123
|
+
// return early special case: first segment is a wildcard
|
|
124
|
+
if (this._parsed?.[0]?.segment === SimpleRoute.WILDCARD) {
|
|
125
|
+
return matched;
|
|
126
|
+
}
|
|
108
127
|
// minimum required (not optional) segments length
|
|
109
128
|
const reqLen = this._parsed.reduce((memo, p, idx, arr) => {
|
|
110
129
|
const next = arr[idx + 1];
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
class SimpleRoute {
|
|
2
2
|
route;
|
|
3
3
|
static SPLITTER = '/';
|
|
4
|
+
static WILDCARD = '*';
|
|
4
5
|
_parsed;
|
|
5
6
|
constructor(route) {
|
|
6
7
|
this.route = route;
|
|
@@ -23,7 +24,8 @@ class SimpleRoute {
|
|
|
23
24
|
}
|
|
24
25
|
static _parse(route) {
|
|
25
26
|
let wasSpread = false;
|
|
26
|
-
|
|
27
|
+
let wasWildcard = false;
|
|
28
|
+
return SimpleRoute._sanitizeAndSplit(route).reduce((memo, segment, idx, all) => {
|
|
27
29
|
let name = null;
|
|
28
30
|
// if optional, remove trailing '?' marker
|
|
29
31
|
let isOptional = segment.endsWith('?');
|
|
@@ -38,20 +40,33 @@ class SimpleRoute {
|
|
|
38
40
|
throw new Error('Multiple spread segments are invalid');
|
|
39
41
|
}
|
|
40
42
|
wasSpread = true;
|
|
41
|
-
segment = '[' + segment.
|
|
43
|
+
segment = '[' + segment.slice(4);
|
|
42
44
|
}
|
|
43
45
|
let test = new RegExp('^' + SimpleRoute._escapeRegExp(segment) + '$');
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
test = /.+/;
|
|
49
|
-
// id([0-9]+)
|
|
50
|
-
let m2 = m[1].match(/^(\w.*)\((.+)\)$/);
|
|
51
|
-
if (m2) {
|
|
52
|
-
name = m2[1];
|
|
53
|
-
test = new RegExp('^' + m2[2] + '$');
|
|
46
|
+
// catch all wildcard
|
|
47
|
+
if (segment === SimpleRoute.WILDCARD) {
|
|
48
|
+
if (idx < all.length - 1) {
|
|
49
|
+
throw new Error(`Wildcard '${SimpleRoute.WILDCARD}' can be used only as a last segment`);
|
|
54
50
|
}
|
|
51
|
+
wasWildcard = true;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// starting with at least one word char within brackets...
|
|
55
|
+
let m = segment.match(/^\[(\w.*)]$/);
|
|
56
|
+
if (m) {
|
|
57
|
+
name = m[1];
|
|
58
|
+
test = /.+/;
|
|
59
|
+
// id([0-9]+)
|
|
60
|
+
let m2 = m[1].match(/^(\w.*)\((.+)\)$/);
|
|
61
|
+
if (m2) {
|
|
62
|
+
name = m2[1];
|
|
63
|
+
test = new RegExp('^' + m2[2] + '$');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (wasWildcard) {
|
|
68
|
+
isOptional = true;
|
|
69
|
+
test = /.*/;
|
|
55
70
|
}
|
|
56
71
|
memo.push({ segment, name, test, isOptional, isSpread });
|
|
57
72
|
return memo;
|
|
@@ -103,6 +118,10 @@ class SimpleRoute {
|
|
|
103
118
|
});
|
|
104
119
|
segments = newSegments;
|
|
105
120
|
}
|
|
121
|
+
// return early special case: first segment is a wildcard
|
|
122
|
+
if (this._parsed?.[0]?.segment === SimpleRoute.WILDCARD) {
|
|
123
|
+
return matched;
|
|
124
|
+
}
|
|
106
125
|
// minimum required (not optional) segments length
|
|
107
126
|
const reqLen = this._parsed.reduce((memo, p, idx, arr) => {
|
|
108
127
|
const next = arr[idx + 1];
|
package/dist/route.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface RouteConfig {
|
|
|
8
8
|
export declare class SimpleRoute {
|
|
9
9
|
readonly route: any;
|
|
10
10
|
static SPLITTER: string;
|
|
11
|
+
static WILDCARD: string;
|
|
11
12
|
protected _parsed: RouteConfig[];
|
|
12
13
|
constructor(route: any);
|
|
13
14
|
protected static _escapeRegExp(str: string): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marianmeres/simple-router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Minimalistic route parser for sapper-like regex routes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -41,7 +41,6 @@
|
|
|
41
41
|
"@rollup/plugin-typescript": "^9.0.2",
|
|
42
42
|
"@types/node": "^14.18.33",
|
|
43
43
|
"lodash": "^4.17.21",
|
|
44
|
-
"nodemon": "^2.0.20",
|
|
45
44
|
"prettier": "^2.2.1",
|
|
46
45
|
"rimraf": "^3.0.2",
|
|
47
46
|
"rollup": "^3.3.0",
|