@eleven-am/pondsocket 0.1.62 → 0.1.64
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/matcher/matcher.js +54 -73
- package/package.json +1 -1
package/matcher/matcher.js
CHANGED
|
@@ -1,91 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseAddress = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @example
|
|
10
|
-
* /api/:id should match /api/123 and return { id: 123 }
|
|
11
|
-
* /api/:id/:name should match /api/123/abc and return { id: 123, name: abc }
|
|
12
|
-
* hello:id should match hello:123 and return { id: 123 }
|
|
13
|
-
* @private
|
|
14
|
-
*/
|
|
15
|
-
function matchPath(path, address) {
|
|
16
|
-
const pathParts = path.split('/');
|
|
17
|
-
const addressParts = address.split('/');
|
|
18
|
-
const params = {};
|
|
19
|
-
const length = Math.max(pathParts.length, addressParts.length);
|
|
20
|
-
for (let i = 0; i < length; i++) {
|
|
21
|
-
const pathPart = pathParts[i];
|
|
22
|
-
const addressPart = addressParts[i];
|
|
23
|
-
if (pathPart === undefined || addressPart === undefined) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
else if (pathPart === '*') {
|
|
27
|
-
return params;
|
|
4
|
+
function pathToRegexp(path) {
|
|
5
|
+
const parts = path.split('/');
|
|
6
|
+
const regexpParts = parts.map((part) => {
|
|
7
|
+
if (part.startsWith(':')) {
|
|
8
|
+
return '([^/]+)';
|
|
28
9
|
}
|
|
29
|
-
else if (
|
|
30
|
-
|
|
10
|
+
else if (part === '*') {
|
|
11
|
+
return '(.*)';
|
|
31
12
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return params;
|
|
13
|
+
return part;
|
|
14
|
+
});
|
|
15
|
+
return new RegExp(`^${regexpParts.join('/')}$`);
|
|
37
16
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
17
|
+
function getParams(path, route) {
|
|
18
|
+
const regexp = pathToRegexp(route);
|
|
19
|
+
const match = path.match(regexp);
|
|
20
|
+
if (!match) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const cleanRoute = route.replace(/:/g, '');
|
|
24
|
+
const cleanMatch = cleanRoute.match(regexp);
|
|
25
|
+
if (!cleanMatch) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const keys = cleanMatch.slice(1).filter((s) => s !== '');
|
|
29
|
+
const values = match.slice(1).filter((s) => s !== '');
|
|
30
|
+
if (keys.length !== values.length) {
|
|
45
31
|
return {};
|
|
46
32
|
}
|
|
47
|
-
return
|
|
33
|
+
return keys.reduce((params, key, index) => {
|
|
34
|
+
if (key === '*') {
|
|
35
|
+
params[key] = `/${values.slice(index).join('/')}`.replace(/\/+/g, '/');
|
|
36
|
+
return params;
|
|
37
|
+
}
|
|
38
|
+
params[key] = values[index];
|
|
39
|
+
return params;
|
|
40
|
+
}, {});
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* /api/id?name=abc should return { name: 'abc' }
|
|
55
|
-
* /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
|
|
56
|
-
*/
|
|
57
|
-
function getQuery(address) {
|
|
58
|
-
const obj = {};
|
|
59
|
-
const params = address.split('?')[1];
|
|
60
|
-
if (params) {
|
|
61
|
-
params.split('&').forEach((param) => {
|
|
62
|
-
const [key, value] = param.split('=');
|
|
63
|
-
obj[key] = value;
|
|
64
|
-
});
|
|
42
|
+
function getQuery(query) {
|
|
43
|
+
if (!query) {
|
|
44
|
+
return {};
|
|
65
45
|
}
|
|
66
|
-
|
|
46
|
+
const parts = query.split('&');
|
|
47
|
+
return parts.reduce((params, part) => {
|
|
48
|
+
const [key, value] = part.split('=');
|
|
49
|
+
params[key] = value;
|
|
50
|
+
return params;
|
|
51
|
+
}, {});
|
|
67
52
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*/
|
|
73
|
-
function parseAddress(path, address) {
|
|
74
|
-
let params;
|
|
75
|
-
const [paramsPath] = address.split('?');
|
|
76
|
-
if (typeof path === 'string') {
|
|
77
|
-
params = matchPath(path, paramsPath);
|
|
78
|
-
if (params === null) {
|
|
53
|
+
function parseAddress(route, address) {
|
|
54
|
+
if (route instanceof RegExp) {
|
|
55
|
+
const match = address.match(route);
|
|
56
|
+
if (!match) {
|
|
79
57
|
return null;
|
|
80
58
|
}
|
|
59
|
+
return {
|
|
60
|
+
params: {},
|
|
61
|
+
query: {},
|
|
62
|
+
};
|
|
81
63
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
64
|
+
const [path, queryParams] = address.split('?');
|
|
65
|
+
const params = getParams(path, route);
|
|
66
|
+
if (!params) {
|
|
67
|
+
return null;
|
|
87
68
|
}
|
|
88
|
-
const query = getQuery(
|
|
69
|
+
const query = getQuery(queryParams);
|
|
89
70
|
return {
|
|
90
71
|
params,
|
|
91
72
|
query,
|