src/vs/base/common/uriTemplate.ts

305 LOC · 295 covered · 10 uncovered · 123 ranges · 181 concepts · 62 introducers · 100 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- uriTemplate.ts ×8
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > export interface IUriTemplateVariable {
7 > readonly explodable: boolean;
8 > readonly name: string;
9 > readonly optional: boolean;
10 > readonly prefixLength?: number;
11 > readonly repeatable: boolean;
12 > }
13 >
14 > interface IUriTemplateComponent {
15 > readonly expression: string;
16 > readonly operator: string;
17 > readonly variables: readonly IUriTemplateVariable[];
18 > }
19 >
20 > /**
21 > * Represents an RFC 6570 URI Template.
22 > */
23 > export class UriTemplate {
24 > /**
25 > * The parsed template components (expressions).
26 > */
27 > public readonly components: ReadonlyArray<IUriTemplateComponent | string>;
28 >
29 > private constructor(
30 > public readonly template: string, uriTemplate.ts ×8
31 > components: ReadonlyArray<IUriTemplateComponent | string>
32 > ) {
33 > this.template = template;
34 > this.components = components;
35 > }
37 > /**
38 > * Parses a URI template string into a UriTemplate instance.
39 > */
40 > public static parse(template: string): UriTemplate {
41 > const components: Array<IUriTemplateComponent | string> = []; uriTemplate.ts ×8
42 > const regex = /\{([^{}]+)\}/g;
43 > let match: RegExpExecArray | null;
44 > let lastPos = 0;
45 > while ((match = regex.exec(template))) {
46 > const [expression, inner] = match;
47 > components.push(template.slice(lastPos, match.index));
48 > lastPos = match.index + expression.length;
49 >
50 > // Handle escaped braces: treat '{{' and '}}' as literals, not expressions
51 > if (template[match.index - 1] === '{' || template[lastPos] === '}') {
52 > components.push(inner); uriTemplate.ts ×1
53 > continue;
54 > }
56 > let operator = '';
57 > let rest = inner;
58 > if (rest.length > 0 && UriTemplate._isOperator(rest[0])) {
59 > operator = rest[0]; uriTemplate.ts ×1
60 > rest = rest.slice(1);
61 > }
62 > const variables = rest.split(',').map((v): IUriTemplateVariable => { uriTemplate.ts ×8
63 > let name = v;
64 > let explodable = false;
65 > let repeatable = false;
66 > let prefixLength: number | undefined = undefined;
67 > let optional = false;
68 > if (name.endsWith('*')) {
69 > explodable = true; uriTemplate.ts ×1
70 > repeatable = true;
71 > name = name.slice(0, -1);
72 > }
73 > const prefixMatch = name.match(/^(.*?):(\d+)$/); uriTemplate.ts ×8
74 > if (prefixMatch) {
75 > name = prefixMatch[1]; uriTemplate.ts ×1
76 > prefixLength = parseInt(prefixMatch[2], 10);
77 > }
78 > if (name.endsWith('?')) { uriTemplate.ts ×8
79 optional = true;
80 name = name.slice(0, -1);
81 }
82 > return { explodable, name, optional, prefixLength, repeatable }; uriTemplate.ts ×8
83 > });
84 > components.push({ expression, operator, variables });
85 > }
86 > components.push(template.slice(lastPos));
87 >
88 > return new UriTemplate(template, components);
89 > }
91 > private static _operators = ['+', '#', '.', '/', ';', '?', '&'] as const;
92 > private static _isOperator(ch: string): boolean {
93 > return (UriTemplate._operators as readonly string[]).includes(ch); uriTemplate.ts ×8
94 > }
96 > /**
97 > * Resolves the template with the given variables.
98 > */
99 > public resolve(variables: Record<string, unknown>): string {
100 > let result = ''; uriTemplate.ts ×2
101 > for (const comp of this.components) {
102 > if (typeof comp === 'string') {
103 > result += comp;
104 > } else {
105 > result += this._expand(comp, variables); uriTemplate.ts ×13
106 > }
108 > return result;
109 > }
111 > private _expand(comp: IUriTemplateComponent, variables: Record<string, unknown>): string {
112 > const op = comp.operator; uriTemplate.ts ×13
113 > const varSpecs = comp.variables;
114 > if (varSpecs.length === 0) {
115 return comp.expression;
116 }
117 > const vals: string[] = []; uriTemplate.ts ×13
118 > const isNamed = op === ';' || op === '?' || op === '&';
119 > const isReserved = op === '+' || op === '#';
120 > const isFragment = op === '#';
121 > const isLabel = op === '.';
122 > const isPath = op === '/';
123 > const isForm = op === '?';
124 > const isFormCont = op === '&';
125 > const isParam = op === ';';
126 >
127 > let prefix = '';
128 > if (op === '+') { prefix = ''; }
129 > else if (op === '#') { prefix = '#'; } uriTemplate.ts ×1
130 > else if (op === '.') { prefix = '.'; } uriTemplate.ts ×4
131 > else if (op === '/') { prefix = ''; } uriTemplate.ts ×1
132 > else if (op === ';') { prefix = ';'; } uriTemplate.ts ×1
133 > else if (op === '?') { prefix = '?'; } uriTemplate.ts ×1
134 > else if (op === '&') { prefix = '&'; } uriTemplate.ts ×1
136 > for (const v of varSpecs) {
137 > const value = variables[v.name];
138 > const defined = Object.prototype.hasOwnProperty.call(variables, v.name);
139 > if (value === undefined || value === null || (Array.isArray(value) && value.length === 0)) {
140 > if (isParam) { uriTemplate.ts ×3
141 if (defined && (value === null || value === undefined)) {
142 vals.push(v.name);
143 }
144 continue;
145 }
146 > if (isForm || isFormCont) { uriTemplate.ts ×3
147 > if (defined) { uriTemplate.ts ×2
148 > vals.push(UriTemplate._formPair(v.name, '', isNamed));
149 > }
150 > continue;
151 > }
152 > continue; uriTemplate.ts ×3
153 > }
154 > if (typeof value === 'object' && !Array.isArray(value)) { uriTemplate.ts ×13
155 > if (v.explodable) { uriTemplate.ts ×6
156 > const pairs: string[] = [];
157 > for (const k in value) {
158 > if (Object.prototype.hasOwnProperty.call(value, k)) {
159 > const thisVal = String((value as Record<string, unknown>)[k]);
160 > if (isParam) {
161 > pairs.push(k + '=' + thisVal); uriTemplate.ts ×4
162 > } else if (isForm || isFormCont) { uriTemplate.ts ×6
163 > pairs.push(k + '=' + thisVal); uriTemplate.ts ×3
164 > } else if (isLabel) { uriTemplate.ts ×1
165 > pairs.push(k + '=' + thisVal); uriTemplate.ts ×5
166 > } else if (isPath) { uriTemplate.ts ×1
167 > pairs.push('/' + k + '=' + UriTemplate._encode(thisVal, isReserved)); uriTemplate.ts ×3
168 > } else { uriTemplate.ts ×2
169 > pairs.push(k + '=' + UriTemplate._encode(thisVal, isReserved)); uriTemplate.ts ×2
170 > }
172 > }
173 > if (isLabel) {
174 > vals.push(pairs.join('.')); uriTemplate.ts ×5
175 > } else if (isPath) { uriTemplate.ts ×6
176 > vals.push(pairs.join('')); uriTemplate.ts ×3
177 > } else if (isParam) { uriTemplate.ts ×1
178 > vals.push(pairs.join(';')); uriTemplate.ts ×4
179 > } else if (isForm || isFormCont) { uriTemplate.ts ×1
180 > vals.push(pairs.join('&')); uriTemplate.ts ×3
181 > } else { uriTemplate.ts ×1
182 > vals.push(pairs.join(',')); uriTemplate.ts ×2
183 > }
184 > } else { uriTemplate.ts ×6
185 > // Not explodable: join as k1,v1,k2,v2,... and assign to variable name uriTemplate.ts ×3
186 > const pairs: string[] = [];
187 > for (const k in value) {
188 > if (Object.prototype.hasOwnProperty.call(value, k)) {
189 > pairs.push(k);
190 > pairs.push(String((value as Record<string, unknown>)[k]));
191 > }
192 > }
193 > // For label, param, form, join as keys=semi,;,dot,.,comma,, (no encoding of , or ;)
194 > const joined = pairs.join(',');
195 > if (isLabel) {
196 > vals.push(joined); uriTemplate.ts ×5
197 > } else if (isParam || isForm || isFormCont) { uriTemplate.ts ×3
198 > vals.push(v.name + '=' + joined); uriTemplate.ts ×1
199 > } else { uriTemplate.ts ×1
200 > vals.push(joined); uriTemplate.ts ×2
201 > }
203 > continue; uriTemplate.ts ×6
204 > }
205 > if (Array.isArray(value)) { uriTemplate.ts ×13
206 > if (v.explodable) { uriTemplate.ts ×5
207 > if (isLabel) {
208 > vals.push(value.join('.')); uriTemplate.ts ×5
209 > } else if (isPath) { uriTemplate.ts ×5
210 > vals.push(value.map(x => '/' + UriTemplate._encode(x, isReserved)).join('')); uriTemplate.ts ×1
211 > } else if (isParam) { uriTemplate.ts ×2
212 > vals.push(value.map(x => v.name + '=' + String(x)).join(';')); uriTemplate.ts ×4
213 > } else if (isForm || isFormCont) { uriTemplate.ts ×1
214 > vals.push(value.map(x => v.name + '=' + String(x)).join('&')); uriTemplate.ts ×1
215 > } else { uriTemplate.ts ×1
216 > vals.push(value.map(x => UriTemplate._encode(x, isReserved)).join(',')); uriTemplate.ts ×1
217 > }
218 > } else { uriTemplate.ts ×5
219 > if (isLabel) {
220 > vals.push(value.join(',')); uriTemplate.ts ×5
221 > } else if (isParam) { uriTemplate.ts ×5
222 > vals.push(v.name + '=' + value.join(',')); uriTemplate.ts ×4
223 > } else if (isForm || isFormCont) { uriTemplate.ts ×2
224 > vals.push(v.name + '=' + value.join(',')); uriTemplate.ts ×3
225 > } else { uriTemplate.ts ×1
226 > vals.push(value.map(x => UriTemplate._encode(x, isReserved)).join(',')); uriTemplate.ts ×1
227 > }
229 > continue;
230 > }
231 > let str = String(value); uriTemplate.ts ×13
232 > if (v.prefixLength !== undefined) {
233 > str = str.substring(0, v.prefixLength); uriTemplate.ts ×1
234 > }
235 > // For simple expansion, encode ! as well (not reserved) uriTemplate.ts ×13
236 > // Only + and # are reserved
237 > const enc = UriTemplate._encode(str, op === '+' || op === '#');
238 > if (isParam) {
239 > vals.push(v.name + '=' + enc); uriTemplate.ts ×2
240 > } else if (isForm || isFormCont) { uriTemplate.ts ×13
241 > vals.push(v.name + '=' + enc); uriTemplate.ts ×1
242 > } else if (isLabel) { uriTemplate.ts ×1
243 > vals.push(enc); uriTemplate.ts ×2
244 > } else if (isPath) { uriTemplate.ts ×1
245 > vals.push('/' + enc); uriTemplate.ts ×1
246 > } else { uriTemplate.ts ×1
247 > vals.push(enc); uriTemplate.ts ×2
248 > }
250 >
251 > let joined = '';
252 > if (isLabel) {
253 > // Remove trailing dot for missing values uriTemplate.ts ×2
254 > const filtered = vals.filter(v => v !== '');
255 > joined = filtered.length ? prefix + filtered.join('.') : '';
256 > } else if (isPath) { uriTemplate.ts ×13
257 > // Remove empty segments for undefined/null uriTemplate.ts ×1
258 > const filtered = vals.filter(v => v !== '');
259 > joined = filtered.length ? filtered.join('') : '';
260 > if (joined && !joined.startsWith('/')) {
261 > joined = '/' + joined; uriTemplate.ts ×3
262 > }
263 > } else if (isParam) { uriTemplate.ts ×1
264 > // For param, if value is empty string, just append ;name uriTemplate.ts ×2
265 > joined = vals.length ? prefix + vals.map(v => v.replace(/=\s*$/, '')).join(';') : '';
266 > } else if (isForm) { uriTemplate.ts ×1
267 > joined = vals.length ? prefix + vals.join('&') : ''; uriTemplate.ts ×1
268 > } else if (isFormCont) { uriTemplate.ts ×1
269 > joined = vals.length ? prefix + vals.join('&') : ''; uriTemplate.ts ×1
270 > } else if (isFragment) { uriTemplate.ts ×1
271 > joined = prefix + vals.join(','); uriTemplate.ts ×1
272 > } else if (isReserved) { uriTemplate.ts ×2
273 > joined = vals.join(','); uriTemplate.ts ×1
274 > } else { uriTemplate.ts ×1
275 > joined = vals.join(','); uriTemplate.ts ×1
276 > }
277 > return joined; uriTemplate.ts ×13
278 > }
280 > private static _encode(str: string, reserved: boolean): string {
281 > return reserved ? encodeURI(str) : pctEncode(str); uriTemplate.ts ×13
282 > }
284 > private static _formPair(k: string, v: unknown, named: boolean): string {
285 > return named ? k + '=' + encodeURIComponent(String(v)) : encodeURIComponent(String(v)); uriTemplate.ts ×2
286 > }
288 >
289 > function pctEncode(str: string): string { uriTemplate.ts ×4
290 > let out = '';
291 > for (let i = 0; i < str.length; i++) {
292 > const chr = str.charCodeAt(i);
293 > if (
294 > // alphanum ranges:
295 > (chr >= 0x30 && chr <= 0x39 || chr >= 0x41 && chr <= 0x5a || chr >= 0x61 && chr <= 0x7a) ||
296 > // unreserved characters: uriTemplate.ts ×1
297 > (chr === 0x2d || chr === 0x2e || chr === 0x5f || chr === 0x7e)
298 > ) { uriTemplate.ts ×4
299 > out += str[i];
300 > } else {
301 > out += '%' + chr.toString(16).toUpperCase().padStart(2, '0'); uriTemplate.ts ×1
302 > }
304 > return out;
305 > }