@kapeta/local-cluster-service 0.5.10 → 0.5.11
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/utils/pathTemplateParser.js +42 -23
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## [0.5.11](https://github.com/kapetacom/local-cluster-service/compare/v0.5.10...v0.5.11) (2023-06-20)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* Improve path template parser. ([#36](https://github.com/kapetacom/local-cluster-service/issues/36)) ([b655da1](https://github.com/kapetacom/local-cluster-service/commit/b655da1e97740ff2b4fe0f2bcd7f75b04003753e))
|
7
|
+
|
1
8
|
## [0.5.10](https://github.com/kapetacom/local-cluster-service/compare/v0.5.9...v0.5.10) (2023-06-19)
|
2
9
|
|
3
10
|
|
package/package.json
CHANGED
@@ -2,6 +2,17 @@
|
|
2
2
|
const TYPE_VARIABLE = 'variable';
|
3
3
|
const TYPE_PATH = 'path';
|
4
4
|
|
5
|
+
/**
|
6
|
+
* A path template is a string that can be used to match a path and extract variables from it.
|
7
|
+
*
|
8
|
+
* E.g. /foo/{bar}/baz
|
9
|
+
*
|
10
|
+
* Would match /foo/123/baz and extract bar=123
|
11
|
+
*
|
12
|
+
* You can also specify a regex for the variable:
|
13
|
+
* /foo/{bar:[0-9]+}/baz
|
14
|
+
*
|
15
|
+
*/
|
5
16
|
class PathTemplate {
|
6
17
|
constructor(pathTemplate) {
|
7
18
|
if (!pathTemplate.startsWith('/')) {
|
@@ -9,33 +20,41 @@ class PathTemplate {
|
|
9
20
|
}
|
10
21
|
this._path = pathTemplate;
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
23
|
+
const variableRegex = /{([^}]+)}/g;
|
24
|
+
let match, offset = 0;
|
25
|
+
this._parts = [];
|
26
|
+
while((match = variableRegex.exec(pathTemplate)) !== null) {
|
27
|
+
if (match.index > offset) {
|
28
|
+
this._parts.push({
|
29
|
+
type: TYPE_PATH,
|
30
|
+
value: pathTemplate.substring(offset, match.index)
|
31
|
+
});
|
32
|
+
}
|
18
33
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
regex = /^[^\/]+/
|
23
|
-
}
|
34
|
+
let regex;
|
35
|
+
let value = match[1];
|
36
|
+
[value, regex] = value.split(/:/, 2);
|
24
37
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
};
|
38
|
+
if (regex) {
|
39
|
+
regex = new RegExp('^' + regex);
|
40
|
+
} else {
|
41
|
+
regex = /^[^\/]+/
|
30
42
|
}
|
31
43
|
|
32
|
-
|
33
|
-
type:
|
34
|
-
value
|
35
|
-
|
36
|
-
|
37
|
-
|
44
|
+
this._parts.push({
|
45
|
+
type: TYPE_VARIABLE,
|
46
|
+
value,
|
47
|
+
regex
|
48
|
+
});
|
49
|
+
offset = match.index + match[0].length;
|
50
|
+
}
|
38
51
|
|
52
|
+
if (offset < pathTemplate.length) {
|
53
|
+
this._parts.push({
|
54
|
+
type: TYPE_PATH,
|
55
|
+
value: pathTemplate.substring(offset)
|
56
|
+
});
|
57
|
+
}
|
39
58
|
}
|
40
59
|
|
41
60
|
get path() {
|
@@ -61,7 +80,7 @@ class PathTemplate {
|
|
61
80
|
return null;
|
62
81
|
}
|
63
82
|
|
64
|
-
path = path.
|
83
|
+
path = path.substring(part.value.length);
|
65
84
|
break;
|
66
85
|
case TYPE_VARIABLE:
|
67
86
|
if (!part.regex.test(path)) {
|