piral-core 1.6.2-beta.7367 → 1.6.2-beta.7393
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/lib/utils/routes.js +100 -2
- package/lib/utils/routes.js.map +1 -1
- package/package.json +4 -5
- package/src/utils/routes.ts +132 -2
package/lib/utils/routes.js
CHANGED
|
@@ -1,5 +1,103 @@
|
|
|
1
|
-
|
|
1
|
+
const defaultDelimiter = escapeString('/');
|
|
2
|
+
const pathExpr = new RegExp([
|
|
3
|
+
'(\\\\.)',
|
|
4
|
+
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))',
|
|
5
|
+
].join('|'), 'g');
|
|
6
|
+
function escapeString(str) {
|
|
7
|
+
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1');
|
|
8
|
+
}
|
|
9
|
+
function escapeGroup(group) {
|
|
10
|
+
return group.replace(/([=!:$\/()])/g, '\\$1');
|
|
11
|
+
}
|
|
12
|
+
function parse(str) {
|
|
13
|
+
const tokens = [];
|
|
14
|
+
let key = 0;
|
|
15
|
+
let index = 0;
|
|
16
|
+
let path = '';
|
|
17
|
+
let res;
|
|
18
|
+
while ((res = pathExpr.exec(str)) !== null) {
|
|
19
|
+
const m = res[0];
|
|
20
|
+
const escaped = res[1];
|
|
21
|
+
const offset = res.index;
|
|
22
|
+
path += str.slice(index, offset);
|
|
23
|
+
index = offset + m.length;
|
|
24
|
+
// Ignore already escaped sequences.
|
|
25
|
+
if (escaped) {
|
|
26
|
+
path += escaped[1];
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const next = str[index];
|
|
30
|
+
const prefix = res[2];
|
|
31
|
+
const name = res[3];
|
|
32
|
+
const capture = res[4];
|
|
33
|
+
const group = res[5];
|
|
34
|
+
const modifier = res[6];
|
|
35
|
+
const asterisk = res[7];
|
|
36
|
+
// Push the current path onto the tokens.
|
|
37
|
+
if (path) {
|
|
38
|
+
tokens.push(path);
|
|
39
|
+
path = '';
|
|
40
|
+
}
|
|
41
|
+
const partial = prefix != null && next != null && next !== prefix;
|
|
42
|
+
const repeat = modifier === '+' || modifier === '*';
|
|
43
|
+
const optional = modifier === '?' || modifier === '*';
|
|
44
|
+
const delimiter = res[2] || '/';
|
|
45
|
+
const pattern = capture || group;
|
|
46
|
+
tokens.push({
|
|
47
|
+
name: name || `${key++}`,
|
|
48
|
+
prefix: prefix || '',
|
|
49
|
+
delimiter,
|
|
50
|
+
optional,
|
|
51
|
+
repeat,
|
|
52
|
+
partial,
|
|
53
|
+
asterisk: !!asterisk,
|
|
54
|
+
pattern: pattern ? escapeGroup(pattern) : asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?',
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Match any characters still remaining.
|
|
58
|
+
if (index < str.length) {
|
|
59
|
+
path += str.substring(index);
|
|
60
|
+
}
|
|
61
|
+
// If the path exists, push it onto the end.
|
|
62
|
+
if (path) {
|
|
63
|
+
tokens.push(path);
|
|
64
|
+
}
|
|
65
|
+
return tokens;
|
|
66
|
+
}
|
|
67
|
+
function tokensToRegExp(tokens) {
|
|
68
|
+
let route = '';
|
|
69
|
+
for (const token of tokens) {
|
|
70
|
+
if (typeof token === 'string') {
|
|
71
|
+
route += escapeString(token);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const prefix = escapeString(token.prefix);
|
|
75
|
+
let capture = '(?:' + token.pattern + ')';
|
|
76
|
+
if (token.repeat) {
|
|
77
|
+
capture += '(?:' + prefix + capture + ')*';
|
|
78
|
+
}
|
|
79
|
+
if (token.optional) {
|
|
80
|
+
if (!token.partial) {
|
|
81
|
+
capture = '(?:' + prefix + '(' + capture + '))?';
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
capture = prefix + '(' + capture + ')?';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
capture = prefix + '(' + capture + ')';
|
|
89
|
+
}
|
|
90
|
+
route += capture;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const endsWithDelimiter = route.slice(-defaultDelimiter.length) === defaultDelimiter;
|
|
94
|
+
const path = endsWithDelimiter ? route.slice(0, -defaultDelimiter.length) : route;
|
|
95
|
+
return new RegExp(`^${path}(?:${defaultDelimiter}(?=$))?$`, 'i');
|
|
96
|
+
}
|
|
97
|
+
function stringToRegexp(path) {
|
|
98
|
+
return tokensToRegExp(parse(path));
|
|
99
|
+
}
|
|
2
100
|
export function createRouteMatcher(path) {
|
|
3
|
-
return
|
|
101
|
+
return stringToRegexp(path);
|
|
4
102
|
}
|
|
5
103
|
//# sourceMappingURL=routes.js.map
|
package/lib/utils/routes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/utils/routes.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/utils/routes.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;AAC3C,MAAM,QAAQ,GAAG,IAAI,MAAM,CACzB;IACE,SAAS;IACT,wGAAwG;CACzG,CAAC,IAAI,CAAC,GAAG,CAAC,EACX,GAAG,CACJ,CAAC;AAEF,SAAS,YAAY,CAAC,GAAW;IAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,4BAA4B,EAAE,MAAM,CAAC,CAAC;AAC3D,CAAC;AAED,SAAS,WAAW,CAAC,KAAa;IAChC,OAAO,KAAK,CAAC,OAAO,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;AAChD,CAAC;AAeD,SAAS,KAAK,CAAC,GAAW;IACxB,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,GAAG,GAAG,C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;YAE1C,IAAI,KAAK,CAAC,MAAM,EAAE;gBAChB,OAAO,IAAI,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;aAC5C;YAED,IAAI,KAAK,CAAC,QAAQ,EAAE;gBAClB,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE;oBAClB,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,GAAG,GAAG,OAAO,GAAG,KAAK,CAAC;iBAClD;qBAAM;oBACL,OAAO,GAAG,MAAM,GAAG,GAAG,GAAG,OAAO,GAAG,IAAI,CAAC;iBACzC;aACF;iBAAM;gBACL,OAAO,GAAG,MAAM,GAAG,GAAG,GAAG,OAAO,GAAG,GAAG,CAAC;aACxC;YAED,KAAK,IAAI,OAAO,CAAC;SAClB;KACF;IAED,MAAM,iBAAiB,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,gBAAgB,CAAC,MAAM,CAAC,KAAK,gBAAgB,CAAC;IACrF,MAAM,IAAI,GAAG,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAClF,OAAO,IAAI,MAAM,CAAC,IAAI,IAAI,MAAM,gBAAgB,UAAU,EAAE,GAAG,CAAC,CAAC;AACnE,CAAC;AAED,SAAS,cAAc,CAAC,IAAY;IAClC,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-core",
|
|
3
|
-
"version": "1.6.2-beta.
|
|
3
|
+
"version": "1.6.2-beta.7393",
|
|
4
4
|
"description": "The core library for creating a Piral instance.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portal",
|
|
@@ -61,9 +61,8 @@
|
|
|
61
61
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"
|
|
65
|
-
"piral-
|
|
66
|
-
"piral-debug-utils": "1.6.2-beta.7367",
|
|
64
|
+
"piral-base": "1.6.2-beta.7393",
|
|
65
|
+
"piral-debug-utils": "1.6.2-beta.7393",
|
|
67
66
|
"zustand": "^3.0.0"
|
|
68
67
|
},
|
|
69
68
|
"devDependencies": {
|
|
@@ -83,5 +82,5 @@
|
|
|
83
82
|
"react-router-dom",
|
|
84
83
|
"tslib"
|
|
85
84
|
],
|
|
86
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "75d596d714a582f54071e1799e0ce183dbacbb09"
|
|
87
86
|
}
|
package/src/utils/routes.ts
CHANGED
|
@@ -1,5 +1,135 @@
|
|
|
1
|
-
|
|
1
|
+
const defaultDelimiter = escapeString('/');
|
|
2
|
+
const pathExpr = new RegExp(
|
|
3
|
+
[
|
|
4
|
+
'(\\\\.)',
|
|
5
|
+
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))',
|
|
6
|
+
].join('|'),
|
|
7
|
+
'g',
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
function escapeString(str: string) {
|
|
11
|
+
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function escapeGroup(group: string) {
|
|
15
|
+
return group.replace(/([=!:$\/()])/g, '\\$1');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Token =
|
|
19
|
+
| string
|
|
20
|
+
| {
|
|
21
|
+
name: string;
|
|
22
|
+
prefix: string;
|
|
23
|
+
delimiter: string;
|
|
24
|
+
optional: boolean;
|
|
25
|
+
repeat: boolean;
|
|
26
|
+
partial: boolean;
|
|
27
|
+
asterisk: boolean;
|
|
28
|
+
pattern: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function parse(str: string) {
|
|
32
|
+
const tokens: Array<Token> = [];
|
|
33
|
+
let key = 0;
|
|
34
|
+
let index = 0;
|
|
35
|
+
let path = '';
|
|
36
|
+
let res: RegExpExecArray;
|
|
37
|
+
|
|
38
|
+
while ((res = pathExpr.exec(str)) !== null) {
|
|
39
|
+
const m = res[0];
|
|
40
|
+
const escaped = res[1];
|
|
41
|
+
const offset = res.index;
|
|
42
|
+
path += str.slice(index, offset);
|
|
43
|
+
index = offset + m.length;
|
|
44
|
+
|
|
45
|
+
// Ignore already escaped sequences.
|
|
46
|
+
if (escaped) {
|
|
47
|
+
path += escaped[1];
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const next = str[index];
|
|
52
|
+
const prefix = res[2];
|
|
53
|
+
const name = res[3];
|
|
54
|
+
const capture = res[4];
|
|
55
|
+
const group = res[5];
|
|
56
|
+
const modifier = res[6];
|
|
57
|
+
const asterisk = res[7];
|
|
58
|
+
|
|
59
|
+
// Push the current path onto the tokens.
|
|
60
|
+
if (path) {
|
|
61
|
+
tokens.push(path);
|
|
62
|
+
path = '';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const partial = prefix != null && next != null && next !== prefix;
|
|
66
|
+
const repeat = modifier === '+' || modifier === '*';
|
|
67
|
+
const optional = modifier === '?' || modifier === '*';
|
|
68
|
+
const delimiter = res[2] || '/';
|
|
69
|
+
const pattern = capture || group;
|
|
70
|
+
|
|
71
|
+
tokens.push({
|
|
72
|
+
name: name || `${key++}`,
|
|
73
|
+
prefix: prefix || '',
|
|
74
|
+
delimiter,
|
|
75
|
+
optional,
|
|
76
|
+
repeat,
|
|
77
|
+
partial,
|
|
78
|
+
asterisk: !!asterisk,
|
|
79
|
+
pattern: pattern ? escapeGroup(pattern) : asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Match any characters still remaining.
|
|
84
|
+
if (index < str.length) {
|
|
85
|
+
path += str.substring(index);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// If the path exists, push it onto the end.
|
|
89
|
+
if (path) {
|
|
90
|
+
tokens.push(path);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return tokens;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function tokensToRegExp(tokens: Array<Token>) {
|
|
97
|
+
let route = '';
|
|
98
|
+
|
|
99
|
+
for (const token of tokens) {
|
|
100
|
+
if (typeof token === 'string') {
|
|
101
|
+
route += escapeString(token);
|
|
102
|
+
} else {
|
|
103
|
+
const prefix = escapeString(token.prefix);
|
|
104
|
+
let capture = '(?:' + token.pattern + ')';
|
|
105
|
+
|
|
106
|
+
if (token.repeat) {
|
|
107
|
+
capture += '(?:' + prefix + capture + ')*';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (token.optional) {
|
|
111
|
+
if (!token.partial) {
|
|
112
|
+
capture = '(?:' + prefix + '(' + capture + '))?';
|
|
113
|
+
} else {
|
|
114
|
+
capture = prefix + '(' + capture + ')?';
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
capture = prefix + '(' + capture + ')';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
route += capture;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const endsWithDelimiter = route.slice(-defaultDelimiter.length) === defaultDelimiter;
|
|
125
|
+
const path = endsWithDelimiter ? route.slice(0, -defaultDelimiter.length) : route;
|
|
126
|
+
return new RegExp(`^${path}(?:${defaultDelimiter}(?=$))?$`, 'i');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function stringToRegexp(path: string) {
|
|
130
|
+
return tokensToRegExp(parse(path));
|
|
131
|
+
}
|
|
2
132
|
|
|
3
133
|
export function createRouteMatcher(path: string): RegExp {
|
|
4
|
-
return
|
|
134
|
+
return stringToRegexp(path);
|
|
5
135
|
}
|