UNPKG

5.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParamType = void 0;
4var common_1 = require("../common/common");
5var predicates_1 = require("../common/predicates");
6/**
7 * An internal class which implements [[ParamTypeDefinition]].
8 *
9 * A [[ParamTypeDefinition]] is a plain javascript object used to register custom parameter types.
10 * When a param type definition is registered, an instance of this class is created internally.
11 *
12 * This class has naive implementations for all the [[ParamTypeDefinition]] methods.
13 *
14 * Used by [[UrlMatcher]] when matching or formatting URLs, or comparing and validating parameter values.
15 *
16 * #### Example:
17 * ```js
18 * var paramTypeDef = {
19 * decode: function(val) { return parseInt(val, 10); },
20 * encode: function(val) { return val && val.toString(); },
21 * equals: function(a, b) { return this.is(a) && a === b; },
22 * is: function(val) { return angular.isNumber(val) && isFinite(val) && val % 1 === 0; },
23 * pattern: /\d+/
24 * }
25 *
26 * var paramType = new ParamType(paramTypeDef);
27 * ```
28 */
29var ParamType = /** @class */ (function () {
30 /**
31 * @param def A configuration object which contains the custom type definition. The object's
32 * properties will override the default methods and/or pattern in `ParamType`'s public interface.
33 * @returns a new ParamType object
34 */
35 function ParamType(def) {
36 /** @inheritdoc */
37 this.pattern = /.*/;
38 /** @inheritdoc */
39 this.inherit = true;
40 common_1.extend(this, def);
41 }
42 // consider these four methods to be "abstract methods" that should be overridden
43 /** @inheritdoc */
44 ParamType.prototype.is = function (val, key) {
45 return true;
46 };
47 /** @inheritdoc */
48 ParamType.prototype.encode = function (val, key) {
49 return val;
50 };
51 /** @inheritdoc */
52 ParamType.prototype.decode = function (val, key) {
53 return val;
54 };
55 /** @inheritdoc */
56 ParamType.prototype.equals = function (a, b) {
57 // tslint:disable-next-line:triple-equals
58 return a == b;
59 };
60 ParamType.prototype.$subPattern = function () {
61 var sub = this.pattern.toString();
62 return sub.substr(1, sub.length - 2);
63 };
64 ParamType.prototype.toString = function () {
65 return "{ParamType:" + this.name + "}";
66 };
67 /** Given an encoded string, or a decoded object, returns a decoded object */
68 ParamType.prototype.$normalize = function (val) {
69 return this.is(val) ? val : this.decode(val);
70 };
71 /**
72 * Wraps an existing custom ParamType as an array of ParamType, depending on 'mode'.
73 * e.g.:
74 * - urlmatcher pattern "/path?{queryParam[]:int}"
75 * - url: "/path?queryParam=1&queryParam=2
76 * - $stateParams.queryParam will be [1, 2]
77 * if `mode` is "auto", then
78 * - url: "/path?queryParam=1 will create $stateParams.queryParam: 1
79 * - url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2]
80 */
81 ParamType.prototype.$asArray = function (mode, isSearch) {
82 if (!mode)
83 return this;
84 if (mode === 'auto' && !isSearch)
85 throw new Error("'auto' array mode is for query parameters only");
86 return new ArrayType(this, mode);
87 };
88 return ParamType;
89}());
90exports.ParamType = ParamType;
91/** Wraps up a `ParamType` object to handle array values. */
92function ArrayType(type, mode) {
93 var _this = this;
94 // Wrap non-array value as array
95 function arrayWrap(val) {
96 return predicates_1.isArray(val) ? val : predicates_1.isDefined(val) ? [val] : [];
97 }
98 // Unwrap array value for "auto" mode. Return undefined for empty array.
99 function arrayUnwrap(val) {
100 switch (val.length) {
101 case 0:
102 return undefined;
103 case 1:
104 return mode === 'auto' ? val[0] : val;
105 default:
106 return val;
107 }
108 }
109 // Wraps type (.is/.encode/.decode) functions to operate on each value of an array
110 function arrayHandler(callback, allTruthyMode) {
111 return function handleArray(val) {
112 if (predicates_1.isArray(val) && val.length === 0)
113 return val;
114 var arr = arrayWrap(val);
115 var result = common_1.map(arr, callback);
116 return allTruthyMode === true ? common_1.filter(result, function (x) { return !x; }).length === 0 : arrayUnwrap(result);
117 };
118 }
119 // Wraps type (.equals) functions to operate on each value of an array
120 function arrayEqualsHandler(callback) {
121 return function handleArray(val1, val2) {
122 var left = arrayWrap(val1), right = arrayWrap(val2);
123 if (left.length !== right.length)
124 return false;
125 for (var i = 0; i < left.length; i++) {
126 if (!callback(left[i], right[i]))
127 return false;
128 }
129 return true;
130 };
131 }
132 ['encode', 'decode', 'equals', '$normalize'].forEach(function (name) {
133 var paramTypeFn = type[name].bind(type);
134 var wrapperFn = name === 'equals' ? arrayEqualsHandler : arrayHandler;
135 _this[name] = wrapperFn(paramTypeFn);
136 });
137 common_1.extend(this, {
138 dynamic: type.dynamic,
139 name: type.name,
140 pattern: type.pattern,
141 inherit: type.inherit,
142 raw: type.raw,
143 is: arrayHandler(type.is.bind(type), true),
144 $arrayMode: mode,
145 });
146}
147//# sourceMappingURL=paramType.js.map
\No newline at end of file