forked from ngrx/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch-pattern.ts
233 lines (195 loc) · 6.03 KB
/
match-pattern.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* This is a straight up copy of react-router's PatternUtils. It may be worth
* investigating if the react-router team is open to splitting this out
* into a separate package
*/
export interface Params {
[param: string]: string | string[];
}
function escapeRegExp(input: string) {
return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function escapeSource(input: string) {
return escapeRegExp(input).replace(/\/+/g, '/+');
}
export interface CompiledPattern {
pattern: string;
regexpSource: string;
paramNames: string[];
tokens: string[];
}
function _compilePattern(pattern: string): CompiledPattern {
let regexpSource = '';
const paramNames: string[] = [];
const tokens: string[] = [];
let match: RegExpExecArray;
let lastIndex = 0;
const matcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|\*\*|\*|\(|\)/g;
while ((match = matcher.exec(pattern))) {
if (match.index !== lastIndex) {
tokens.push(pattern.slice(lastIndex, match.index));
regexpSource += escapeSource(pattern.slice(lastIndex, match.index));
}
if ( match[1] ) {
regexpSource += '([^/]+)';
paramNames.push(match[1]);
}
else if ( match[0] === '**' ) {
regexpSource += '(.*)';
paramNames.push('splat');
}
else if ( match[0] === '*' ) {
regexpSource += '(.*?)';
paramNames.push('splat');
}
else if ( match[0] === '(' ) {
regexpSource += '(?:';
}
else if ( match[0] === ')' ) {
regexpSource += ')?';
}
tokens.push(match[0]);
lastIndex = matcher.lastIndex;
}
if ( lastIndex !== pattern.length ) {
tokens.push(pattern.slice(lastIndex, pattern.length));
regexpSource += escapeSource(pattern.slice(lastIndex, pattern.length));
}
return {
pattern,
regexpSource,
paramNames,
tokens
};
}
const CompiledPatternsCache: { [pattern: string]: CompiledPattern } = {};
export function compilePattern(pattern) {
if ( !(pattern in CompiledPatternsCache) ) {
CompiledPatternsCache[pattern] = _compilePattern(pattern);
}
return CompiledPatternsCache[pattern];
}
/**
* Attempts to match a pattern on the given pathname. Patterns may use
* the following special characters:
*
* - :paramName Matches a URL segment up to the next /, ?, or #. The
* captured string is considered a "param"
* - () Wraps a segment of the URL that is optional
* - * Consumes (non-greedy) all characters up to the next
* character in the pattern, or to the end of the URL if
* there is none
* - ** Consumes (greedy) all characters up to the next character
* in the pattern, or to the end of the URL if there is none
*
* The return value is an object with the following properties:
*
* - remainingPathname
* - paramNames
* - paramValues
*/
export function matchPattern(pattern: string, pathname: string) {
// Make leading slashes consistent between pattern and pathname.
if ( pattern.charAt(0) !== '/' ) {
pattern = `/${pattern}`;
}
if ( pathname.charAt(0) !== '/' ) {
pathname = `/${pathname}`;
}
let { regexpSource, paramNames, tokens } = compilePattern(pattern);
regexpSource += '/*'; // Capture path separators
// Special-case patterns like '*' for catch-all routes.
if (tokens[tokens.length - 1] === '*') {
regexpSource += '$';
}
const match = pathname.match(new RegExp(`^${regexpSource}`, 'i'));
let remainingPathname: string;
let paramValues: string[];
if ( match != null ) {
const matchedPath = match[0];
remainingPathname = pathname.substr(matchedPath.length);
// If we didn't match the entire pathname, then make sure that the match we
// did get ends at a path separator (potentially the one we added above at
// the beginning of the path, if the actual match was empty).
if (
remainingPathname &&
matchedPath.charAt(matchedPath.length - 1) !== '/'
) {
return {
remainingPathname: null,
paramNames,
paramValues: null
};
}
paramValues = match.slice(1).map(v => v && decodeURIComponent(v));
}
else {
remainingPathname = paramValues = null;
}
return {
remainingPathname,
paramNames,
paramValues
};
}
export function getParamNames(pattern: string) {
return compilePattern(pattern).paramNames;
}
export function getParams(pattern: string, pathname: string) {
const { paramNames, paramValues } = matchPattern(pattern, pathname);
if (paramValues != null) {
return paramNames.reduce(function (memo, paramName, index) {
memo[paramName] = paramValues[index];
return memo;
}, {});
}
return null;
}
/**
* Returns a version of the given pattern with params interpolated. Throws
* if there is a dynamic segment of the pattern for which there is no param.
*/
export function formatPattern(pattern: string, params: Params = {}) {
const { tokens } = compilePattern(pattern);
let parenCount = 0;
let pathname = '';
let splatIndex = 0;
let token: string;
let paramName: string;
let paramValue;
for (let i = 0, len = tokens.length; i < len; ++i) {
token = tokens[i];
if (token === '*' || token === '**') {
paramValue = Array.isArray(params['splat']) ?
params['splat'][splatIndex++] :
params['splat'];
if ( paramValue != null || parenCount > 0 ) {
console.error('Missing splat #%s for path "%s"', splatIndex, pattern);
}
if ( paramValue != null ) {
pathname += encodeURI(paramValue);
}
}
else if ( token === '(' ) {
parenCount += 1;
}
else if ( token === ')' ) {
parenCount -= 1;
}
else if ( token.charAt(0) === ':' ) {
paramName = token.substring(1);
paramValue = params[paramName];
if ( !(paramValue != null || parenCount > 0) ) {
console.error('Missing "%s" parameter for path "%s"',
paramName, pattern);
}
if ( paramValue != null ) {
pathname += encodeURIComponent(paramValue);
}
}
else {
pathname += token;
}
}
return pathname.replace(/\/+/g, '/');
}