js-routes 1.4.14 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.4.14"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/routes.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ /**
2
+ * File generated by js-routes RubyVariables.GEM_VERSION
3
+ * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
+ */
5
+ declare type RouteParameter = unknown;
6
+ declare type RouteParameters = Record<string, RouteParameter>;
7
+ declare type Serializer = (value: unknown) => string;
8
+ declare type RouteHelper = {
9
+ (...args: RouteParameter[]): string;
10
+ requiredParams(): string[];
11
+ toString(): string;
12
+ };
13
+ declare type RouteHelpers = Record<string, RouteHelper>;
14
+ declare type Configuration = {
15
+ prefix: string;
16
+ default_url_options: RouteParameters;
17
+ special_options_key: string;
18
+ serializer: Serializer;
19
+ };
20
+ declare type Optional<T> = {
21
+ [P in keyof T]?: T[P] | null;
22
+ };
23
+ interface RouterExposedMethods {
24
+ config(): Configuration;
25
+ configure(arg: Partial<Configuration>): Configuration;
26
+ serialize: Serializer;
27
+ }
28
+ declare type KeywordUrlOptions = Optional<{
29
+ host: string;
30
+ protocol: string;
31
+ subdomain: string;
32
+ port: string;
33
+ anchor: string;
34
+ trailing_slash: boolean;
35
+ }>;
36
+ declare type PartsTable = Record<
37
+ string,
38
+ {
39
+ r?: boolean;
40
+ d?: unknown;
41
+ }
42
+ >;
43
+ declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM";
44
+ declare const RubyVariables: {
45
+ PREFIX: string;
46
+ DEPRECATED_GLOBBING_BEHAVIOR: boolean;
47
+ SPECIAL_OPTIONS_KEY: string;
48
+ DEFAULT_URL_OPTIONS: RouteParameters;
49
+ SERIALIZER: Serializer;
50
+ NAMESPACE: string;
51
+ ROUTES_OBJECT: RouteHelpers;
52
+ MODULE_TYPE: ModuleType | null;
53
+ WRAPPER: <T>(callback: T) => T;
54
+ };
55
+ declare const define:
56
+ | undefined
57
+ | (((arg: unknown[], callback: () => unknown) => void) & {
58
+ amd?: unknown;
59
+ });
60
+ declare const module:
61
+ | {
62
+ exports: any;
63
+ }
64
+ | undefined;
data/lib/routes.js CHANGED
@@ -1,528 +1,484 @@
1
- /*
2
- File generated by js-routes GEM_VERSION
3
- Based on Rails RAILS_VERSION routes of APP_CLASS
1
+ /**
2
+ * File generated by js-routes RubyVariables.GEM_VERSION
3
+ * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
-
6
- (function() {
7
- var DeprecatedGlobbingBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, UriEncoderSegmentRegex, Utils, error, result,
8
- hasProp = {}.hasOwnProperty,
9
- slice = [].slice;
10
-
11
- ParameterMissing = function(message, fileName, lineNumber) {
12
- var instance;
13
- instance = new Error(message, fileName, lineNumber);
14
- if (Object.setPrototypeOf) {
15
- Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
16
- } else {
17
- instance.__proto__ = this.__proto__;
18
- }
19
- if (Error.captureStackTrace) {
20
- Error.captureStackTrace(instance, ParameterMissing);
21
- }
22
- return instance;
23
- };
24
-
25
- ParameterMissing.prototype = Object.create(Error.prototype, {
26
- constructor: {
27
- value: Error,
28
- enumerable: false,
29
- writable: true,
30
- configurable: true
5
+ RubyVariables.WRAPPER((that) => {
6
+ const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
7
+ let NodeTypes;
8
+ (function (NodeTypes) {
9
+ NodeTypes[NodeTypes["GROUP"] = 1] = "GROUP";
10
+ NodeTypes[NodeTypes["CAT"] = 2] = "CAT";
11
+ NodeTypes[NodeTypes["SYMBOL"] = 3] = "SYMBOL";
12
+ NodeTypes[NodeTypes["OR"] = 4] = "OR";
13
+ NodeTypes[NodeTypes["STAR"] = 5] = "STAR";
14
+ NodeTypes[NodeTypes["LITERAL"] = 6] = "LITERAL";
15
+ NodeTypes[NodeTypes["SLASH"] = 7] = "SLASH";
16
+ NodeTypes[NodeTypes["DOT"] = 8] = "DOT";
17
+ })(NodeTypes || (NodeTypes = {}));
18
+ const Root = that;
19
+ const ModuleReferences = {
20
+ CJS: {
21
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22
+ define: (routes) => (module.exports = routes),
23
+ support: () => typeof module === "object",
24
+ },
25
+ AMD: {
26
+ define: (routes) =>
27
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
+ define([], function () {
29
+ return routes;
30
+ }),
31
+ support: () => typeof define === "function" && !!define.amd,
32
+ },
33
+ UMD: {
34
+ define: (routes) => {
35
+ if (ModuleReferences.AMD.support()) {
36
+ ModuleReferences.AMD.define(routes);
37
+ }
38
+ else {
39
+ if (ModuleReferences.CJS.support()) {
40
+ try {
41
+ ModuleReferences.CJS.define(routes);
42
+ }
43
+ catch (error) {
44
+ if (error.name !== "TypeError")
45
+ throw error;
46
+ }
47
+ }
48
+ }
49
+ },
50
+ support: () => ModuleReferences.AMD.support() || ModuleReferences.CJS.support(),
51
+ },
52
+ ESM: {
53
+ define: () => null,
54
+ support: () => true,
55
+ },
56
+ };
57
+ class ParametersMissing extends Error {
58
+ constructor(...keys) {
59
+ super(`Route missing required keys: ${keys.join(", ")}`);
60
+ this.keys = keys;
61
+ Object.setPrototypeOf(this, Object.getPrototypeOf(this));
62
+ this.name = ParametersMissing.name;
63
+ }
31
64
  }
32
- });
33
-
34
- if (Object.setPrototypeOf) {
35
- Object.setPrototypeOf(ParameterMissing, Error);
36
- } else {
37
- ParameterMissing.__proto__ = Error;
38
- }
39
-
40
- NodeTypes = NODE_TYPES;
41
-
42
- DeprecatedGlobbingBehavior = DEPRECATED_GLOBBING_BEHAVIOR;
43
-
44
- SpecialOptionsKey = SPECIAL_OPTIONS_KEY;
45
-
46
- UriEncoderSegmentRegex = /[^a-zA-Z0-9\-\._~!\$&'\(\)\*\+,;=:@]/g;
47
-
48
- ReservedOptions = ['anchor', 'trailing_slash', 'subdomain', 'host', 'port', 'protocol'];
49
-
50
- Utils = {
51
- configuration: {
52
- prefix: PREFIX,
53
- default_url_options: DEFAULT_URL_OPTIONS,
54
- special_options_key: SPECIAL_OPTIONS_KEY,
55
- serializer: SERIALIZER
56
- },
57
- default_serializer: function(object, prefix) {
58
- var element, i, j, key, len, prop, s;
59
- if (prefix == null) {
60
- prefix = null;
61
- }
62
- if (object == null) {
63
- return "";
64
- }
65
- if (!prefix && !(this.get_object_type(object) === "object")) {
66
- throw new Error("Url parameters should be a javascript hash");
67
- }
68
- s = [];
69
- switch (this.get_object_type(object)) {
70
- case "array":
71
- for (i = j = 0, len = object.length; j < len; i = ++j) {
72
- element = object[i];
73
- s.push(this.default_serializer(element, prefix + "[]"));
74
- }
75
- break;
76
- case "object":
77
- for (key in object) {
78
- if (!hasProp.call(object, key)) continue;
79
- prop = object[key];
80
- if ((prop == null) && (prefix != null)) {
81
- prop = "";
82
- }
83
- if (prop != null) {
84
- if (prefix != null) {
85
- key = prefix + "[" + key + "]";
86
- }
87
- s.push(this.default_serializer(prop, key));
88
- }
89
- }
90
- break;
91
- default:
92
- if (object != null) {
93
- s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
94
- }
95
- }
96
- if (!s.length) {
97
- return "";
98
- }
99
- return s.join("&");
100
- },
101
- serialize: function(object) {
102
- var custom_serializer;
103
- custom_serializer = this.configuration.serializer;
104
- if ((custom_serializer != null) && this.get_object_type(custom_serializer) === "function") {
105
- return custom_serializer(object);
106
- } else {
107
- return this.default_serializer(object);
108
- }
109
- },
110
- clean_path: function(path) {
111
- var last_index;
112
- path = path.split("://");
113
- last_index = path.length - 1;
114
- path[last_index] = path[last_index].replace(/\/+/g, "/");
115
- return path.join("://");
116
- },
117
- extract_options: function(number_of_params, args) {
118
- var last_el, options;
119
- last_el = args[args.length - 1];
120
- if ((args.length > number_of_params && last_el === void 0) || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.looks_like_serialized_model(last_el))) {
121
- options = args.pop() || {};
122
- delete options[this.configuration.special_options_key];
123
- return options;
124
- } else {
125
- return {};
126
- }
127
- },
128
- looks_like_serialized_model: function(object) {
129
- return !object[this.configuration.special_options_key] && ("id" in object || "to_param" in object);
130
- },
131
- path_identifier: function(object) {
132
- var property;
133
- if (object === 0) {
134
- return "0";
135
- }
136
- if (!object) {
137
- return "";
138
- }
139
- property = object;
140
- if (this.get_object_type(object) === "object") {
141
- if ("to_param" in object) {
142
- if (object.to_param == null) {
143
- throw new ParameterMissing("Route parameter missing: to_param");
144
- }
145
- property = object.to_param;
146
- } else if ("id" in object) {
147
- if (object.id == null) {
148
- throw new ParameterMissing("Route parameter missing: id");
149
- }
150
- property = object.id;
151
- } else {
152
- property = object;
153
- }
154
- if (this.get_object_type(property) === "function") {
155
- property = property.call(object);
156
- }
157
- }
158
- return property.toString();
159
- },
160
- clone: function(obj) {
161
- var attr, copy, key;
162
- if ((obj == null) || "object" !== this.get_object_type(obj)) {
163
- return obj;
164
- }
165
- copy = obj.constructor();
166
- for (key in obj) {
167
- if (!hasProp.call(obj, key)) continue;
168
- attr = obj[key];
169
- copy[key] = attr;
170
- }
171
- return copy;
172
- },
173
- merge: function() {
174
- var tap, xs;
175
- xs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
176
- tap = function(o, fn) {
177
- fn(o);
178
- return o;
179
- };
180
- if ((xs != null ? xs.length : void 0) > 0) {
181
- return tap({}, function(m) {
182
- var j, k, len, results, v, x;
183
- results = [];
184
- for (j = 0, len = xs.length; j < len; j++) {
185
- x = xs[j];
186
- results.push((function() {
187
- var results1;
188
- results1 = [];
189
- for (k in x) {
190
- v = x[k];
191
- results1.push(m[k] = v);
192
- }
193
- return results1;
194
- })());
195
- }
196
- return results;
197
- });
198
- }
199
- },
200
- normalize_options: function(parts, required_parts, default_options, actual_parameters) {
201
- var i, j, key, len, options, part, parts_options, result, route_parts, url_parameters, use_all_parts, value;
202
- options = this.extract_options(parts.length, actual_parameters);
203
- if (actual_parameters.length > parts.length) {
204
- throw new Error("Too many parameters provided for path");
205
- }
206
- use_all_parts = actual_parameters.length > required_parts.length;
207
- parts_options = {};
208
- for (key in options) {
209
- if (!hasProp.call(options, key)) continue;
210
- use_all_parts = true;
211
- if (this.indexOf(parts, key) >= 0) {
212
- parts_options[key] = value;
213
- }
214
- }
215
- options = this.merge(this.configuration.default_url_options, default_options, options);
216
- result = {};
217
- url_parameters = {};
218
- result['url_parameters'] = url_parameters;
219
- for (key in options) {
220
- if (!hasProp.call(options, key)) continue;
221
- value = options[key];
222
- if (this.indexOf(ReservedOptions, key) >= 0) {
223
- result[key] = value;
224
- } else {
225
- url_parameters[key] = value;
226
- }
227
- }
228
- route_parts = use_all_parts ? parts : required_parts;
229
- i = 0;
230
- for (j = 0, len = route_parts.length; j < len; j++) {
231
- part = route_parts[j];
232
- if (i < actual_parameters.length) {
233
- if (!parts_options.hasOwnProperty(part)) {
234
- url_parameters[part] = actual_parameters[i];
235
- ++i;
236
- }
237
- }
238
- }
239
- return result;
240
- },
241
- build_route: function(parts, required_parts, default_options, route, full_url, args) {
242
- var options, parameters, result, url, url_params;
243
- args = Array.prototype.slice.call(args);
244
- options = this.normalize_options(parts, required_parts, default_options, args);
245
- parameters = options['url_parameters'];
246
- result = "" + (this.get_prefix()) + (this.visit(route, parameters));
247
- url = Utils.clean_path(result);
248
- if (options['trailing_slash'] === true) {
249
- url = url.replace(/(.*?)[\/]?$/, "$1/");
250
- }
251
- if ((url_params = this.serialize(parameters)).length) {
252
- url += "?" + url_params;
253
- }
254
- url += options.anchor ? "#" + options.anchor : "";
255
- if (full_url) {
256
- url = this.route_url(options) + url;
257
- }
258
- return url;
259
- },
260
- visit: function(route, parameters, optional) {
261
- var left, left_part, right, right_part, type, value;
262
- if (optional == null) {
263
- optional = false;
264
- }
265
- type = route[0], left = route[1], right = route[2];
266
- switch (type) {
267
- case NodeTypes.GROUP:
268
- return this.visit(left, parameters, true);
269
- case NodeTypes.STAR:
270
- return this.visit_globbing(left, parameters, true);
271
- case NodeTypes.LITERAL:
272
- case NodeTypes.SLASH:
273
- case NodeTypes.DOT:
274
- return left;
275
- case NodeTypes.CAT:
276
- left_part = this.visit(left, parameters, optional);
277
- right_part = this.visit(right, parameters, optional);
278
- if (optional && ((this.is_optional_node(left[0]) && !left_part) || ((this.is_optional_node(right[0])) && !right_part))) {
279
- return "";
280
- }
281
- return "" + left_part + right_part;
282
- case NodeTypes.SYMBOL:
283
- value = parameters[left];
284
- delete parameters[left];
285
- if (value != null) {
286
- return this.encode_segment(this.path_identifier(value));
287
- }
288
- if (optional) {
289
- return "";
290
- } else {
291
- throw new ParameterMissing("Route parameter missing: " + left);
292
- }
293
- break;
294
- default:
295
- throw new Error("Unknown Rails node type");
296
- }
297
- },
298
- encode_segment: function(segment) {
299
- return segment.replace(UriEncoderSegmentRegex, function(str) {
300
- return encodeURIComponent(str);
301
- });
302
- },
303
- is_optional_node: function(node) {
304
- return this.indexOf([NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT], node) >= 0;
305
- },
306
- build_path_spec: function(route, wildcard) {
307
- var left, right, type;
308
- if (wildcard == null) {
309
- wildcard = false;
310
- }
311
- type = route[0], left = route[1], right = route[2];
312
- switch (type) {
313
- case NodeTypes.GROUP:
314
- return "(" + (this.build_path_spec(left)) + ")";
315
- case NodeTypes.CAT:
316
- return "" + (this.build_path_spec(left)) + (this.build_path_spec(right));
317
- case NodeTypes.STAR:
318
- return this.build_path_spec(left, true);
319
- case NodeTypes.SYMBOL:
320
- if (wildcard === true) {
321
- return "" + (left[0] === '*' ? '' : '*') + left;
322
- } else {
323
- return ":" + left;
324
- }
325
- break;
326
- case NodeTypes.SLASH:
327
- case NodeTypes.DOT:
328
- case NodeTypes.LITERAL:
329
- return left;
330
- default:
331
- throw new Error("Unknown Rails node type");
332
- }
333
- },
334
- visit_globbing: function(route, parameters, optional) {
335
- var left, right, type, value;
336
- type = route[0], left = route[1], right = route[2];
337
- value = parameters[left];
338
- delete parameters[left];
339
- if (value == null) {
340
- return this.visit(route, parameters, optional);
341
- }
342
- value = (function() {
343
- switch (this.get_object_type(value)) {
344
- case "array":
345
- return value.join("/");
346
- default:
347
- return value;
348
- }
349
- }).call(this);
350
- if (DeprecatedGlobbingBehavior) {
351
- return this.path_identifier(value);
352
- } else {
353
- return encodeURI(this.path_identifier(value));
354
- }
355
- },
356
- get_prefix: function() {
357
- var prefix;
358
- prefix = this.configuration.prefix;
359
- if (prefix !== "") {
360
- prefix = (prefix.match("/$") ? prefix : prefix + "/");
361
- }
362
- return prefix;
363
- },
364
- route: function(parts_table, default_options, route_spec, full_url) {
365
- var j, len, part, parts, path_fn, ref, required, required_parts;
366
- required_parts = [];
367
- parts = [];
368
- for (j = 0, len = parts_table.length; j < len; j++) {
369
- ref = parts_table[j], part = ref[0], required = ref[1];
370
- parts.push(part);
371
- if (required) {
372
- required_parts.push(part);
373
- }
374
- }
375
- path_fn = function() {
376
- return Utils.build_route(parts, required_parts, default_options, route_spec, full_url, arguments);
377
- };
378
- path_fn.required_params = required_parts;
379
- path_fn.toString = function() {
380
- return Utils.build_path_spec(route_spec);
381
- };
382
- return path_fn;
383
- },
384
- route_url: function(route_defaults) {
385
- var hostname, port, protocol, subdomain;
386
- if (typeof route_defaults === 'string') {
387
- return route_defaults;
388
- }
389
- hostname = route_defaults.host || Utils.current_host();
390
- if (!hostname) {
391
- return '';
392
- }
393
- subdomain = route_defaults.subdomain ? route_defaults.subdomain + '.' : '';
394
- protocol = route_defaults.protocol || Utils.current_protocol();
395
- port = route_defaults.port || (!route_defaults.host ? Utils.current_port() : void 0);
396
- port = port ? ":" + port : '';
397
- return protocol + "://" + subdomain + hostname + port;
398
- },
399
- has_location: function() {
400
- return (typeof window !== "undefined" && window !== null ? window.location : void 0) != null;
401
- },
402
- current_host: function() {
403
- if (this.has_location()) {
404
- return window.location.hostname;
405
- } else {
406
- return null;
407
- }
408
- },
409
- current_protocol: function() {
410
- if (this.has_location() && window.location.protocol !== '') {
411
- return window.location.protocol.replace(/:$/, '');
412
- } else {
413
- return 'http';
414
- }
415
- },
416
- current_port: function() {
417
- if (this.has_location() && window.location.port !== '') {
418
- return window.location.port;
419
- } else {
420
- return '';
421
- }
422
- },
423
- _classToTypeCache: null,
424
- _classToType: function() {
425
- var j, len, name, ref;
426
- if (this._classToTypeCache != null) {
427
- return this._classToTypeCache;
428
- }
429
- this._classToTypeCache = {};
430
- ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
431
- for (j = 0, len = ref.length; j < len; j++) {
432
- name = ref[j];
433
- this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
434
- }
435
- return this._classToTypeCache;
436
- },
437
- get_object_type: function(obj) {
438
- if (this.jQuery && (this.jQuery.type != null)) {
439
- return this.jQuery.type(obj);
440
- }
441
- if (obj == null) {
442
- return "" + obj;
443
- }
444
- if (typeof obj === "object" || typeof obj === "function") {
445
- return this._classToType()[Object.prototype.toString.call(obj)] || "object";
446
- } else {
447
- return typeof obj;
448
- }
449
- },
450
- indexOf: function(array, element) {
451
- if (Array.prototype.indexOf) {
452
- return array.indexOf(element);
453
- } else {
454
- return this.indexOfImplementation(array, element);
455
- }
456
- },
457
- indexOfImplementation: function(array, element) {
458
- var el, i, j, len, result;
459
- result = -1;
460
- for (i = j = 0, len = array.length; j < len; i = ++j) {
461
- el = array[i];
462
- if (el === element) {
463
- result = i;
464
- }
465
- }
466
- return result;
467
- },
468
- namespace: function(root, namespace, routes) {
469
- var index, j, len, part, parts;
470
- parts = namespace ? namespace.split(".") : [];
471
- if (parts.length === 0) {
472
- return routes;
473
- }
474
- for (index = j = 0, len = parts.length; j < len; index = ++j) {
475
- part = parts[index];
476
- if (index < parts.length - 1) {
477
- root = (root[part] || (root[part] = {}));
478
- } else {
479
- return root[part] = routes;
480
- }
481
- }
482
- },
483
- configure: function(new_config) {
484
- return this.configuration = this.merge(this.configuration, new_config);
485
- },
486
- config: function() {
487
- return this.clone(this.configuration);
488
- },
489
- make: function() {
490
- var routes;
491
- routes = ROUTES;
492
- routes.configure = function(config) {
493
- return Utils.configure(config);
494
- };
495
- routes.config = function() {
496
- return Utils.config();
497
- };
498
- routes.default_serializer = function(object, prefix) {
499
- return Utils.default_serializer(object, prefix);
500
- };
501
- return Object.assign({
502
- "default": routes
503
- }, routes);
65
+ const DeprecatedGlobbingBehavior = RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR;
66
+ const UriEncoderSegmentRegex = /[^a-zA-Z0-9\-._~!$&'()*+,;=:@]/g;
67
+ const ReservedOptions = [
68
+ "anchor",
69
+ "trailing_slash",
70
+ "subdomain",
71
+ "host",
72
+ "port",
73
+ "protocol",
74
+ ];
75
+ class UtilsClass {
76
+ constructor() {
77
+ this.configuration = {
78
+ prefix: RubyVariables.PREFIX,
79
+ default_url_options: RubyVariables.DEFAULT_URL_OPTIONS,
80
+ special_options_key: RubyVariables.SPECIAL_OPTIONS_KEY,
81
+ serializer: RubyVariables.SERIALIZER || this.default_serializer.bind(this),
82
+ };
83
+ }
84
+ default_serializer(value, prefix) {
85
+ if (this.is_nullable(value)) {
86
+ return "";
87
+ }
88
+ if (!prefix && !this.is_object(value)) {
89
+ throw new Error("Url parameters should be a javascript hash");
90
+ }
91
+ prefix = prefix || "";
92
+ const result = [];
93
+ if (this.is_array(value)) {
94
+ for (const element of value) {
95
+ result.push(this.default_serializer(element, prefix + "[]"));
96
+ }
97
+ }
98
+ else if (this.is_object(value)) {
99
+ for (let key in value) {
100
+ if (!hasProp(value, key))
101
+ continue;
102
+ let prop = value[key];
103
+ if (this.is_nullable(prop) && prefix) {
104
+ prop = "";
105
+ }
106
+ if (this.is_not_nullable(prop)) {
107
+ if (prefix) {
108
+ key = prefix + "[" + key + "]";
109
+ }
110
+ result.push(this.default_serializer(prop, key));
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ if (this.is_not_nullable(value)) {
116
+ result.push(encodeURIComponent(prefix) + "=" + encodeURIComponent("" + value));
117
+ }
118
+ }
119
+ return result.join("&");
120
+ }
121
+ serialize(object) {
122
+ return this.configuration.serializer(object);
123
+ }
124
+ extract_options(number_of_params, args) {
125
+ const last_el = args[args.length - 1];
126
+ if ((args.length > number_of_params && last_el === 0) ||
127
+ (this.is_object(last_el) &&
128
+ !this.looks_like_serialized_model(last_el))) {
129
+ if (this.is_object(last_el)) {
130
+ delete last_el[this.configuration.special_options_key];
131
+ }
132
+ return {
133
+ args: args.slice(0, args.length - 1),
134
+ options: last_el,
135
+ };
136
+ }
137
+ else {
138
+ return { args, options: {} };
139
+ }
140
+ }
141
+ looks_like_serialized_model(object) {
142
+ return (this.is_object(object) &&
143
+ !object[this.configuration.special_options_key] &&
144
+ ("id" in object || "to_param" in object || "toParam" in object));
145
+ }
146
+ path_identifier(object) {
147
+ const result = this.unwrap_path_identifier(object);
148
+ return this.is_nullable(result) ? "" : "" + result;
149
+ }
150
+ unwrap_path_identifier(object) {
151
+ let result = object;
152
+ if (!this.is_object(object)) {
153
+ return object;
154
+ }
155
+ if ("to_param" in object) {
156
+ result = object.to_param;
157
+ }
158
+ else if ("toParam" in object) {
159
+ result = object.toParam;
160
+ }
161
+ else if ("id" in object) {
162
+ result = object.id;
163
+ }
164
+ else {
165
+ result = object;
166
+ }
167
+ return this.is_callable(result) ? result.call(object) : result;
168
+ }
169
+ partition_parameters(parts, required_params, default_options, call_arguments) {
170
+ // eslint-disable-next-line prefer-const
171
+ let { args, options } = this.extract_options(parts.length, call_arguments);
172
+ if (args.length > parts.length) {
173
+ throw new Error("Too many parameters provided for path");
174
+ }
175
+ let use_all_parts = args.length > required_params.length;
176
+ const parts_options = {};
177
+ for (const key in options) {
178
+ const value = options[key];
179
+ if (!hasProp(options, key))
180
+ continue;
181
+ use_all_parts = true;
182
+ if (parts.includes(key)) {
183
+ parts_options[key] = value;
184
+ }
185
+ }
186
+ options = {
187
+ ...this.configuration.default_url_options,
188
+ ...default_options,
189
+ ...options,
190
+ };
191
+ const url_parameters = {};
192
+ const query_parameters = {};
193
+ for (const key in options) {
194
+ if (!hasProp(options, key))
195
+ continue;
196
+ const value = options[key];
197
+ if (this.is_reserved_option(key)) {
198
+ url_parameters[key] = value;
199
+ }
200
+ else {
201
+ if (!this.is_nullable(value) &&
202
+ (value !== default_options[key] || required_params.includes(key))) {
203
+ query_parameters[key] = value;
204
+ }
205
+ }
206
+ }
207
+ const route_parts = use_all_parts ? parts : required_params;
208
+ let i = 0;
209
+ for (const part of route_parts) {
210
+ if (i < args.length) {
211
+ if (!hasProp(parts_options, part)) {
212
+ query_parameters[part] = args[i];
213
+ ++i;
214
+ }
215
+ }
216
+ }
217
+ return { url_parameters, query_parameters };
218
+ }
219
+ build_route(parts, required_params, default_options, route, full_url, args) {
220
+ const { url_parameters, query_parameters } = this.partition_parameters(parts, required_params, default_options, args);
221
+ const missing_params = required_params.filter((param) => !hasProp(query_parameters, param) ||
222
+ this.is_nullable(query_parameters[param]));
223
+ if (missing_params.length) {
224
+ throw new ParametersMissing(...missing_params);
225
+ }
226
+ let result = this.get_prefix() + this.visit(route, query_parameters);
227
+ if (url_parameters.trailing_slash) {
228
+ result = result.replace(/(.*?)[/]?$/, "$1/");
229
+ }
230
+ const url_params = this.serialize(query_parameters);
231
+ if (url_params.length) {
232
+ result += "?" + url_params;
233
+ }
234
+ result += url_parameters.anchor ? "#" + url_parameters.anchor : "";
235
+ if (full_url) {
236
+ result = this.route_url(url_parameters) + result;
237
+ }
238
+ return result;
239
+ }
240
+ visit(route, parameters, optional = false) {
241
+ switch (route[0]) {
242
+ case NodeTypes.GROUP:
243
+ return this.visit(route[1], parameters, true);
244
+ case NodeTypes.CAT:
245
+ return this.visit_cat(route, parameters, optional);
246
+ case NodeTypes.SYMBOL:
247
+ return this.visit_symbol(route, parameters, optional);
248
+ case NodeTypes.STAR:
249
+ return this.visit_globbing(route[1], parameters, true);
250
+ case NodeTypes.LITERAL:
251
+ case NodeTypes.SLASH:
252
+ case NodeTypes.DOT:
253
+ return route[1];
254
+ default:
255
+ throw new Error("Unknown Rails node type");
256
+ }
257
+ }
258
+ is_not_nullable(object) {
259
+ return !this.is_nullable(object);
260
+ }
261
+ is_nullable(object) {
262
+ return object === undefined || object === null;
263
+ }
264
+ visit_cat(
265
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
266
+ [_type, left, right], parameters, optional) {
267
+ const left_part = this.visit(left, parameters, optional);
268
+ const right_part = this.visit(right, parameters, optional);
269
+ if (optional &&
270
+ ((this.is_optional_node(left[0]) && !left_part) ||
271
+ (this.is_optional_node(right[0]) && !right_part))) {
272
+ return "";
273
+ }
274
+ return left_part + right_part;
275
+ }
276
+ visit_symbol(
277
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
278
+ [_type, key], parameters, optional) {
279
+ const value = this.path_identifier(parameters[key]);
280
+ delete parameters[key];
281
+ if (value.length) {
282
+ return this.encode_segment(value);
283
+ }
284
+ if (optional) {
285
+ return "";
286
+ }
287
+ else {
288
+ throw new ParametersMissing(key);
289
+ }
290
+ }
291
+ encode_segment(segment) {
292
+ return segment.replace(UriEncoderSegmentRegex, function (str) {
293
+ return encodeURIComponent(str);
294
+ });
295
+ }
296
+ is_optional_node(node) {
297
+ return [NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT].includes(node);
298
+ }
299
+ build_path_spec(route, wildcard = false) {
300
+ let key;
301
+ switch (route[0]) {
302
+ case NodeTypes.GROUP:
303
+ return "(" + this.build_path_spec(route[1]) + ")";
304
+ case NodeTypes.CAT:
305
+ return (this.build_path_spec(route[1]) + this.build_path_spec(route[2]));
306
+ case NodeTypes.STAR:
307
+ return this.build_path_spec(route[1], true);
308
+ case NodeTypes.SYMBOL:
309
+ key = route[1];
310
+ if (wildcard) {
311
+ return (key.startsWith("*") ? "" : "*") + key;
312
+ }
313
+ else {
314
+ return ":" + key;
315
+ }
316
+ break;
317
+ case NodeTypes.SLASH:
318
+ case NodeTypes.DOT:
319
+ case NodeTypes.LITERAL:
320
+ return route[1];
321
+ default:
322
+ throw new Error("Unknown Rails node type");
323
+ }
324
+ }
325
+ visit_globbing(route, parameters, optional) {
326
+ const key = route[1];
327
+ let value = parameters[key];
328
+ delete parameters[key];
329
+ if (this.is_nullable(value)) {
330
+ return this.visit(route, parameters, optional);
331
+ }
332
+ if (this.is_array(value)) {
333
+ value = value.join("/");
334
+ }
335
+ value = this.path_identifier(value);
336
+ return DeprecatedGlobbingBehavior ? "" + value : encodeURI("" + value);
337
+ }
338
+ get_prefix() {
339
+ const prefix = this.configuration.prefix;
340
+ return prefix.match("/$")
341
+ ? prefix.substring(0, prefix.length - 1)
342
+ : prefix;
343
+ }
344
+ route(parts_table, route_spec, full_url = false) {
345
+ const required_params = [];
346
+ const parts = [];
347
+ const default_options = {};
348
+ for (const [part, { r: required, d: value }] of Object.entries(parts_table)) {
349
+ parts.push(part);
350
+ if (required) {
351
+ required_params.push(part);
352
+ }
353
+ if (this.is_not_nullable(value)) {
354
+ default_options[part] = value;
355
+ }
356
+ }
357
+ const result = (...args) => {
358
+ return this.build_route(parts, required_params, default_options, route_spec, full_url, args);
359
+ };
360
+ result.requiredParams = () => required_params;
361
+ result.toString = () => {
362
+ return this.build_path_spec(route_spec);
363
+ };
364
+ return result;
365
+ }
366
+ route_url(route_defaults) {
367
+ const hostname = route_defaults.host || this.current_host();
368
+ if (!hostname) {
369
+ return "";
370
+ }
371
+ const subdomain = route_defaults.subdomain
372
+ ? route_defaults.subdomain + "."
373
+ : "";
374
+ const protocol = route_defaults.protocol || this.current_protocol();
375
+ let port = route_defaults.port ||
376
+ (!route_defaults.host ? this.current_port() : undefined);
377
+ port = port ? ":" + port : "";
378
+ return protocol + "://" + subdomain + hostname + port;
379
+ }
380
+ has_location() {
381
+ return this.is_not_nullable(window) && !!window.location;
382
+ }
383
+ current_host() {
384
+ if (this.has_location()) {
385
+ return window.location.hostname;
386
+ }
387
+ else {
388
+ return null;
389
+ }
390
+ }
391
+ current_protocol() {
392
+ if (this.has_location() && window.location.protocol !== "") {
393
+ return window.location.protocol.replace(/:$/, "");
394
+ }
395
+ else {
396
+ return "http";
397
+ }
398
+ }
399
+ current_port() {
400
+ if (this.has_location() && window.location.port !== "") {
401
+ return window.location.port;
402
+ }
403
+ else {
404
+ return "";
405
+ }
406
+ }
407
+ is_object(value) {
408
+ return typeof value === "object" && "" + value === "[object Object]";
409
+ }
410
+ is_array(object) {
411
+ return object instanceof Array;
412
+ }
413
+ is_callable(object) {
414
+ return typeof object === "function" && !!object.call;
415
+ }
416
+ is_reserved_option(key) {
417
+ return ReservedOptions.includes(key);
418
+ }
419
+ namespace(object, namespace, routes) {
420
+ const parts = (namespace === null || namespace === void 0 ? void 0 : namespace.split(".")) || [];
421
+ if (parts.length === 0) {
422
+ return routes;
423
+ }
424
+ for (let index = 0; index < parts.length; index++) {
425
+ const part = parts[index];
426
+ if (index < parts.length - 1) {
427
+ object = object[part] || (object[part] = {});
428
+ }
429
+ else {
430
+ return (object[part] = routes);
431
+ }
432
+ }
433
+ }
434
+ configure(new_config) {
435
+ this.configuration = { ...this.configuration, ...new_config };
436
+ return this.configuration;
437
+ }
438
+ config() {
439
+ return { ...this.configuration };
440
+ }
441
+ is_module_supported(name) {
442
+ return ModuleReferences[name].support();
443
+ }
444
+ ensure_module_supported(name) {
445
+ if (!this.is_module_supported(name)) {
446
+ throw new Error(`${name} is not supported by runtime`);
447
+ }
448
+ }
449
+ define_module(name, module) {
450
+ if (!name) {
451
+ return;
452
+ }
453
+ this.ensure_module_supported(name);
454
+ ModuleReferences[name].define(module);
455
+ }
504
456
  }
505
- };
506
-
507
- result = Utils.make();
508
-
509
- if (typeof define === "function" && define.amd) {
510
- define([], function() {
511
- return result;
512
- });
513
- } else if (typeof module !== "undefined" && module !== null) {
514
- try {
515
- module.exports = result;
516
- } catch (error1) {
517
- error = error1;
518
- if (error.name !== 'TypeError') {
519
- throw error;
520
- }
457
+ const Utils = new UtilsClass();
458
+ // We want this helper name to be short
459
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
460
+ const __jsr = {
461
+ r(parts_table, route_spec, full_url) {
462
+ return Utils.route(parts_table, route_spec, full_url);
463
+ },
464
+ };
465
+ const result = {
466
+ ...__jsr,
467
+ configure: (config) => {
468
+ return Utils.configure(config);
469
+ },
470
+ config: () => {
471
+ return Utils.config();
472
+ },
473
+ serialize: (object) => {
474
+ return Utils.serialize(object);
475
+ },
476
+ ...RubyVariables.ROUTES_OBJECT,
477
+ };
478
+ Utils.namespace(Root, RubyVariables.NAMESPACE, result);
479
+ if (RubyVariables.MODULE_TYPE) {
480
+ Utils.define_module(RubyVariables.MODULE_TYPE, result);
521
481
  }
522
- } else {
523
- Utils.namespace(this, NAMESPACE, result);
524
- }
525
-
526
- return result;
527
-
528
- }).call(this);
482
+ return result;
483
+ })(this);
484
+ //# sourceMappingURL=routes.js.map