1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParamTypes = void 0;
|
4 | var common_1 = require("../common/common");
|
5 | var predicates_1 = require("../common/predicates");
|
6 | var hof_1 = require("../common/hof");
|
7 | var coreservices_1 = require("../common/coreservices");
|
8 | var paramType_1 = require("./paramType");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var ParamTypes = (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 |
|
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 |
|
56 |
|
57 |
|
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 | }());
|
82 | exports.ParamTypes = ParamTypes;
|
83 | function 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 |
|
92 | equals: function (a, b) { return a == b; },
|
93 | };
|
94 | return common_1.extend({}, defaultTypeBase, def);
|
95 | };
|
96 |
|
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 |
|
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 | }
|
154 | initDefaultTypes();
|
155 |
|
\ | No newline at end of file |