1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.PathPattern = exports.trimParam = void 0;
|
25 | const path_1 = require("./path");
|
26 |
|
27 |
|
28 | const WILDCARD_CAPTURE_REGEX = new RegExp("{[^/{}]+}", "g");
|
29 |
|
30 | function trimParam(param) {
|
31 | const paramNoBraces = param.slice(1, -1);
|
32 | if (paramNoBraces.includes("=")) {
|
33 | return paramNoBraces.slice(0, paramNoBraces.indexOf("="));
|
34 | }
|
35 | return paramNoBraces;
|
36 | }
|
37 | exports.trimParam = trimParam;
|
38 |
|
39 | class Segment {
|
40 | constructor(value) {
|
41 | this.value = value;
|
42 | this.name = "segment";
|
43 | this.trimmed = value;
|
44 | }
|
45 | isSingleSegmentWildcard() {
|
46 | return this.value.includes("*") && !this.isMultiSegmentWildcard();
|
47 | }
|
48 | isMultiSegmentWildcard() {
|
49 | return this.value.includes("**");
|
50 | }
|
51 | }
|
52 |
|
53 | class SingleCaptureSegment {
|
54 | constructor(value) {
|
55 | this.value = value;
|
56 | this.name = "single-capture";
|
57 | this.trimmed = trimParam(value);
|
58 | }
|
59 | isSingleSegmentWildcard() {
|
60 | return true;
|
61 | }
|
62 | isMultiSegmentWildcard() {
|
63 | return false;
|
64 | }
|
65 | }
|
66 |
|
67 | class MultiCaptureSegment {
|
68 | constructor(value) {
|
69 | this.value = value;
|
70 | this.name = "multi-capture";
|
71 | this.trimmed = trimParam(value);
|
72 | }
|
73 | isSingleSegmentWildcard() {
|
74 | return false;
|
75 | }
|
76 | isMultiSegmentWildcard() {
|
77 | return true;
|
78 | }
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | class PathPattern {
|
85 |
|
86 |
|
87 | static compile(rawPath) {
|
88 | return undefined;
|
89 | }
|
90 | constructor(raw) {
|
91 | this.raw = raw;
|
92 | this.segments = [];
|
93 | this.initPathSegments(raw);
|
94 | }
|
95 | getValue() {
|
96 | return this.raw;
|
97 | }
|
98 |
|
99 | hasWildcards() {
|
100 | return this.segments.some((segment) => segment.isSingleSegmentWildcard() || segment.isMultiSegmentWildcard());
|
101 | }
|
102 | hasCaptures() {
|
103 | return this.segments.some((segment) => segment.name === "single-capture" || segment.name === "multi-capture");
|
104 | }
|
105 | extractMatches(path) {
|
106 | const matches = {};
|
107 | if (!this.hasCaptures()) {
|
108 | return matches;
|
109 | }
|
110 | const pathSegments = (0, path_1.pathParts)(path);
|
111 | let pathNdx = 0;
|
112 | for (let segmentNdx = 0; segmentNdx < this.segments.length && pathNdx < pathSegments.length; segmentNdx++) {
|
113 | const segment = this.segments[segmentNdx];
|
114 | const remainingSegments = this.segments.length - 1 - segmentNdx;
|
115 | const nextPathNdx = pathSegments.length - remainingSegments;
|
116 | if (segment.name === "single-capture") {
|
117 | matches[segment.trimmed] = pathSegments[pathNdx];
|
118 | }
|
119 | else if (segment.name === "multi-capture") {
|
120 | matches[segment.trimmed] = pathSegments.slice(pathNdx, nextPathNdx).join("/");
|
121 | }
|
122 | pathNdx = segment.isMultiSegmentWildcard() ? nextPathNdx : pathNdx + 1;
|
123 | }
|
124 | return matches;
|
125 | }
|
126 | initPathSegments(raw) {
|
127 | const parts = (0, path_1.pathParts)(raw);
|
128 | for (const part of parts) {
|
129 | let segment;
|
130 | const capture = part.match(WILDCARD_CAPTURE_REGEX);
|
131 | if (capture && capture.length === 1) {
|
132 | segment = part.includes("**")
|
133 | ? new MultiCaptureSegment(part)
|
134 | : new SingleCaptureSegment(part);
|
135 | }
|
136 | else {
|
137 | segment = new Segment(part);
|
138 | }
|
139 | this.segments.push(segment);
|
140 | }
|
141 | }
|
142 | }
|
143 | exports.PathPattern = PathPattern;
|