-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathuri-templates.js
417 lines (405 loc) · 13 KB
/
uri-templates.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.UriTemplate = factory());
})(this, (function () { 'use strict';
var uriTemplateGlobalModifiers = {
"+": true,
"#": true,
".": true,
"/": true,
";": true,
"?": true,
"&": true
};
var uriTemplateSuffices = {
"*": true
};
function notReallyPercentEncode(string) {
return encodeURI(string).replace(/%25[0-9][0-9]/g, function (doubleEncoded) {
return "%" + doubleEncoded.substring(3);
});
}
function uriTemplateSubstitution(spec) {
var modifier = "";
if (uriTemplateGlobalModifiers[spec.charAt(0)]) {
modifier = spec.charAt(0);
spec = spec.substring(1);
}
var separator = "";
var prefix = "";
var shouldEscape = true;
var showVariables = false;
var trimEmptyString = false;
if (modifier == '+') {
shouldEscape = false;
} else if (modifier == ".") {
prefix = ".";
separator = ".";
} else if (modifier == "/") {
prefix = "/";
separator = "/";
} else if (modifier == '#') {
prefix = "#";
shouldEscape = false;
} else if (modifier == ';') {
prefix = ";";
separator = ";",
showVariables = true;
trimEmptyString = true;
} else if (modifier == '?') {
prefix = "?";
separator = "&",
showVariables = true;
} else if (modifier == '&') {
prefix = "&";
separator = "&",
showVariables = true;
}
var varNames = [];
var varList = spec.split(",");
var varSpecs = [];
var varSpecMap = {};
for (var i = 0; i < varList.length; i++) {
var varName = varList[i];
var truncate = null;
if (varName.indexOf(":") != -1) {
var parts = varName.split(":");
varName = parts[0];
truncate = parseInt(parts[1]);
}
var suffices = {};
while (uriTemplateSuffices[varName.charAt(varName.length - 1)]) {
suffices[varName.charAt(varName.length - 1)] = true;
varName = varName.substring(0, varName.length - 1);
}
var varSpec = {
truncate: truncate,
name: varName,
suffices: suffices
};
varSpecs.push(varSpec);
varSpecMap[varName] = varSpec;
varNames.push(varName);
}
var subFunction = function (valueFunction) {
var result = "";
var startIndex = 0;
for (var i = 0; i < varSpecs.length; i++) {
var varSpec = varSpecs[i];
var value = valueFunction(varSpec.name);
if (value == null || (Array.isArray(value) && value.length == 0) || (typeof value == 'object' && Object.keys(value).length == 0)) {
startIndex++;
continue;
}
if (i == startIndex) {
result += prefix;
} else {
result += (separator || ",");
}
if (Array.isArray(value)) {
if (showVariables) {
result += varSpec.name + "=";
}
for (var j = 0; j < value.length; j++) {
if (j > 0) {
result += varSpec.suffices['*'] ? (separator || ",") : ",";
if (varSpec.suffices['*'] && showVariables) {
result += varSpec.name + "=";
}
}
result += shouldEscape ? encodeURIComponent(value[j]).replace(/!/g, "%21") : notReallyPercentEncode(value[j]);
}
} else if (typeof value == "object") {
if (showVariables && !varSpec.suffices['*']) {
result += varSpec.name + "=";
}
var first = true;
for (var key in value) {
if (!first) {
result += varSpec.suffices['*'] ? (separator || ",") : ",";
}
first = false;
result += shouldEscape ? encodeURIComponent(key).replace(/!/g, "%21") : notReallyPercentEncode(key);
result += varSpec.suffices['*'] ? '=' : ",";
result += shouldEscape ? encodeURIComponent(value[key]).replace(/!/g, "%21") : notReallyPercentEncode(value[key]);
}
} else {
if (showVariables) {
result += varSpec.name;
if (!trimEmptyString || value != "") {
result += "=";
}
}
if (varSpec.truncate != null) {
value = value.substring(0, varSpec.truncate);
}
result += shouldEscape ? encodeURIComponent(value).replace(/!/g, "%21"): notReallyPercentEncode(value);
}
}
return result;
};
var guessFunction = function (stringValue, resultObj) {
if (prefix) {
if (stringValue.substring(0, prefix.length) == prefix) {
stringValue = stringValue.substring(prefix.length);
} else {
return null;
}
}
if (varSpecs.length == 1 && varSpecs[0].suffices['*']) {
var varSpec = varSpecs[0];
var varName = varSpec.name;
var arrayValue = varSpec.suffices['*'] ? stringValue.split(separator || ",") : [stringValue];
var hasEquals = (shouldEscape && stringValue.indexOf('=') != -1); // There's otherwise no way to distinguish between "{value*}" for arrays and objects
for (var i = 1; i < arrayValue.length; i++) {
var stringValue = arrayValue[i];
if (hasEquals && stringValue.indexOf('=') == -1) {
// Bit of a hack - if we're expecting "=" for key/value pairs, and values can't contain "=", then assume a value has been accidentally split
arrayValue[i - 1] += (separator || ",") + stringValue;
arrayValue.splice(i, 1);
i--;
}
}
for (var i = 0; i < arrayValue.length; i++) {
var stringValue = arrayValue[i];
if (shouldEscape && stringValue.indexOf('=') != -1) {
hasEquals = true;
}
var innerArrayValue = stringValue.split(",");
for (var j = 0; j < innerArrayValue.length; j++) {
if (shouldEscape) {
innerArrayValue[j] = decodeURIComponent(innerArrayValue[j]);
}
}
if (innerArrayValue.length == 1) {
arrayValue[i] = innerArrayValue[0];
} else {
arrayValue[i] = innerArrayValue;
}
}
if (showVariables || hasEquals) {
var objectValue = resultObj[varName] || {};
for (var j = 0; j < arrayValue.length; j++) {
var innerValue = stringValue;
if (showVariables && !innerValue) {
// The empty string isn't a valid variable, so if our value is zero-length we have nothing
continue;
}
if (typeof arrayValue[j] == "string") {
var stringValue = arrayValue[j];
var innerVarName = stringValue.split("=", 1)[0];
var stringValue = stringValue.substring(innerVarName.length + 1);
innerValue = stringValue;
} else {
var stringValue = arrayValue[j][0];
var innerVarName = stringValue.split("=", 1)[0];
var stringValue = stringValue.substring(innerVarName.length + 1);
arrayValue[j][0] = stringValue;
innerValue = arrayValue[j];
}
if (objectValue[innerVarName] !== undefined) {
if (Array.isArray(objectValue[innerVarName])) {
objectValue[innerVarName].push(innerValue);
} else {
objectValue[innerVarName] = [objectValue[innerVarName], innerValue];
}
} else {
objectValue[innerVarName] = innerValue;
}
}
if (Object.keys(objectValue).length == 1 && objectValue[varName] !== undefined) {
resultObj[varName] = objectValue[varName];
} else {
resultObj[varName] = objectValue;
}
} else {
if (resultObj[varName] !== undefined) {
if (Array.isArray(resultObj[varName])) {
resultObj[varName] = resultObj[varName].concat(arrayValue);
} else {
resultObj[varName] = [resultObj[varName]].concat(arrayValue);
}
} else {
if (arrayValue.length == 1 && !varSpec.suffices['*']) {
resultObj[varName] = arrayValue[0];
} else {
resultObj[varName] = arrayValue;
}
}
}
} else {
var arrayValue = (varSpecs.length == 1) ? [stringValue] : stringValue.split(separator || ",");
var specIndexMap = {};
for (var i = 0; i < arrayValue.length; i++) {
// Try from beginning
var firstStarred = 0;
for (; firstStarred < varSpecs.length - 1 && firstStarred < i; firstStarred++) {
if (varSpecs[firstStarred].suffices['*']) {
break;
}
}
if (firstStarred == i) {
// The first [i] of them have no "*" suffix
specIndexMap[i] = i;
continue;
} else {
// Try from the end
for (var lastStarred = varSpecs.length - 1; lastStarred > 0 && (varSpecs.length - lastStarred) < (arrayValue.length - i); lastStarred--) {
if (varSpecs[lastStarred].suffices['*']) {
break;
}
}
if ((varSpecs.length - lastStarred) == (arrayValue.length - i)) {
// The last [length - i] of them have no "*" suffix
specIndexMap[i] = lastStarred;
continue;
}
}
// Just give up and use the first one
specIndexMap[i] = firstStarred;
}
for (var i = 0; i < arrayValue.length; i++) {
var stringValue = arrayValue[i];
if (!stringValue && showVariables) {
// The empty string isn't a valid variable, so if our value is zero-length we have nothing
continue;
}
var innerArrayValue = stringValue.split(",");
var hasEquals = false;
if (showVariables) {
var stringValue = innerArrayValue[0]; // using innerArrayValue
var varName = stringValue.split("=", 1)[0];
var stringValue = stringValue.substring(varName.length + 1);
innerArrayValue[0] = stringValue;
var varSpec = varSpecMap[varName] || varSpecs[0];
} else {
var varSpec = varSpecs[specIndexMap[i]];
var varName = varSpec.name;
}
for (var j = 0; j < innerArrayValue.length; j++) {
if (shouldEscape) {
innerArrayValue[j] = decodeURIComponent(innerArrayValue[j]);
}
}
if ((showVariables || varSpec.suffices['*'])&& resultObj[varName] !== undefined) {
if (Array.isArray(resultObj[varName])) {
resultObj[varName] = resultObj[varName].concat(innerArrayValue);
} else {
resultObj[varName] = [resultObj[varName]].concat(innerArrayValue);
}
} else {
if (innerArrayValue.length == 1 && !varSpec.suffices['*']) {
resultObj[varName] = innerArrayValue[0];
} else {
resultObj[varName] = innerArrayValue;
}
}
}
}
};
subFunction.varNames = varNames;
return {
prefix: prefix,
substitution: subFunction,
unSubstitution: guessFunction
};
}
function UriTemplate(template) {
if (!(this instanceof UriTemplate)) {
return new UriTemplate(template);
}
var parts = template.split("{");
var textParts = [parts.shift()];
var prefixes = [];
var substitutions = [];
var unSubstitutions = [];
var varNames = [];
while (parts.length > 0) {
var part = parts.shift();
var spec = part.split("}")[0];
var remainder = part.substring(spec.length + 1);
var funcs = uriTemplateSubstitution(spec);
substitutions.push(funcs.substitution);
unSubstitutions.push(funcs.unSubstitution);
prefixes.push(funcs.prefix);
textParts.push(remainder);
varNames = varNames.concat(funcs.substitution.varNames);
}
this.fill = function (valueFunction) {
if (valueFunction && typeof valueFunction !== 'function') {
var value = valueFunction;
valueFunction = function (varName) {
return value[varName];
};
}
var result = textParts[0];
for (var i = 0; i < substitutions.length; i++) {
var substitution = substitutions[i];
result += substitution(valueFunction);
result += textParts[i + 1];
}
return result;
};
this.fromUri = function (substituted) {
var result = {};
for (var i = 0; i < textParts.length; i++) {
var part = textParts[i];
if (substituted.substring(0, part.length) !== part) {
return undefined;
}
substituted = substituted.substring(part.length);
if (i >= textParts.length - 1) {
if (substituted == "") {
break;
} else {
return undefined;
}
}
var nextPart = textParts[i + 1];
var offset = i;
while (true) {
if (offset == textParts.length - 2) {
var endPart = substituted.substring(substituted.length - nextPart.length);
if (endPart !== nextPart) {
return undefined;
}
var stringValue = substituted.substring(0, substituted.length - nextPart.length);
substituted = endPart;
} else if (nextPart) {
var nextPartPos = substituted.indexOf(nextPart);
var stringValue = substituted.substring(0, nextPartPos);
substituted = substituted.substring(nextPartPos);
} else if (prefixes[offset + 1]) {
var nextPartPos = substituted.indexOf(prefixes[offset + 1]);
if (nextPartPos === -1) nextPartPos = substituted.length;
var stringValue = substituted.substring(0, nextPartPos);
substituted = substituted.substring(nextPartPos);
} else if (textParts.length > offset + 2) {
// If the separator between this variable and the next is blank (with no prefix), continue onwards
offset++;
nextPart = textParts[offset + 1];
continue;
} else {
var stringValue = substituted;
substituted = "";
}
break;
}
unSubstitutions[i](stringValue, result);
}
return result;
};
this.varNames = varNames;
this.template = template;
}
UriTemplate.prototype = {
toString: function () {
return this.template;
},
fillFromObject: function (obj) {
return this.fill(obj);
}
};
return UriTemplate;
}));