UNPKG

5.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParamTypes = void 0;
4var common_1 = require("../common/common");
5var predicates_1 = require("../common/predicates");
6var hof_1 = require("../common/hof");
7var coreservices_1 = require("../common/coreservices");
8var paramType_1 = require("./paramType");
9/**
10 * A registry for parameter types.
11 *
12 * This registry manages the built-in (and custom) parameter types.
13 *
14 * The built-in parameter types are:
15 *
16 * - [[string]]
17 * - [[path]]
18 * - [[query]]
19 * - [[hash]]
20 * - [[int]]
21 * - [[bool]]
22 * - [[date]]
23 * - [[json]]
24 * - [[any]]
25 *
26 * To register custom parameter types, use [[UrlConfig.type]], i.e.,
27 *
28 * ```js
29 * router.urlService.config.type(customType)
30 * ```
31 */
32var ParamTypes = /** @class */ (function () {
33 function ParamTypes() {
34 this.enqueue = true;
35 this.typeQueue = [];
36 this.defaultTypes = common_1.pick(ParamTypes.prototype, [
37 'hash',
38 'string',
39 'query',
40 'path',
41 'int',
42 'bool',
43 'date',
44 'json',
45 'any',
46 ]);
47 // Register default types. Store them in the prototype of this.types.
48 var makeType = function (definition, name) { return new paramType_1.ParamType(common_1.extend({ name: name }, definition)); };
49 this.types = common_1.inherit(common_1.map(this.defaultTypes, makeType), {});
50 }
51 ParamTypes.prototype.dispose = function () {
52 this.types = {};
53 };
54 /**
55 * Registers a parameter type
56 *
57 * End users should call [[UrlMatcherFactory.type]], which delegates to this method.
58 */
59 ParamTypes.prototype.type = function (name, definition, definitionFn) {
60 if (!predicates_1.isDefined(definition))
61 return this.types[name];
62 if (this.types.hasOwnProperty(name))
63 throw new Error("A type named '" + name + "' has already been defined.");
64 this.types[name] = new paramType_1.ParamType(common_1.extend({ name: name }, definition));
65 if (definitionFn) {
66 this.typeQueue.push({ name: name, def: definitionFn });
67 if (!this.enqueue)
68 this._flushTypeQueue();
69 }
70 return this;
71 };
72 ParamTypes.prototype._flushTypeQueue = function () {
73 while (this.typeQueue.length) {
74 var type = this.typeQueue.shift();
75 if (type.pattern)
76 throw new Error("You cannot override a type's .pattern at runtime.");
77 common_1.extend(this.types[type.name], coreservices_1.services.$injector.invoke(type.def));
78 }
79 };
80 return ParamTypes;
81}());
82exports.ParamTypes = ParamTypes;
83function initDefaultTypes() {
84 var makeDefaultType = function (def) {
85 var valToString = function (val) { return (val != null ? val.toString() : val); };
86 var defaultTypeBase = {
87 encode: valToString,
88 decode: valToString,
89 is: hof_1.is(String),
90 pattern: /.*/,
91 // tslint:disable-next-line:triple-equals
92 equals: function (a, b) { return a == b; },
93 };
94 return common_1.extend({}, defaultTypeBase, def);
95 };
96 // Default Parameter Type Definitions
97 common_1.extend(ParamTypes.prototype, {
98 string: makeDefaultType({}),
99 path: makeDefaultType({
100 pattern: /[^/]*/,
101 }),
102 query: makeDefaultType({}),
103 hash: makeDefaultType({
104 inherit: false,
105 }),
106 int: makeDefaultType({
107 decode: function (val) { return parseInt(val, 10); },
108 is: function (val) {
109 return !predicates_1.isNullOrUndefined(val) && this.decode(val.toString()) === val;
110 },
111 pattern: /-?\d+/,
112 }),
113 bool: makeDefaultType({
114 encode: function (val) { return (val && 1) || 0; },
115 decode: function (val) { return parseInt(val, 10) !== 0; },
116 is: hof_1.is(Boolean),
117 pattern: /0|1/,
118 }),
119 date: makeDefaultType({
120 encode: function (val) {
121 return !this.is(val)
122 ? undefined
123 : [val.getFullYear(), ('0' + (val.getMonth() + 1)).slice(-2), ('0' + val.getDate()).slice(-2)].join('-');
124 },
125 decode: function (val) {
126 if (this.is(val))
127 return val;
128 var match = this.capture.exec(val);
129 return match ? new Date(match[1], match[2] - 1, match[3]) : undefined;
130 },
131 is: function (val) { return val instanceof Date && !isNaN(val.valueOf()); },
132 equals: function (l, r) {
133 return ['getFullYear', 'getMonth', 'getDate'].reduce(function (acc, fn) { return acc && l[fn]() === r[fn](); }, true);
134 },
135 pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
136 capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/,
137 }),
138 json: makeDefaultType({
139 encode: common_1.toJson,
140 decode: common_1.fromJson,
141 is: hof_1.is(Object),
142 equals: common_1.equals,
143 pattern: /[^/]*/,
144 }),
145 // does not encode/decode
146 any: makeDefaultType({
147 encode: common_1.identity,
148 decode: common_1.identity,
149 is: function () { return true; },
150 equals: common_1.equals,
151 }),
152 });
153}
154initDefaultTypes();
155//# sourceMappingURL=paramTypes.js.map
\No newline at end of file