UNPKG

9.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Param = exports.DefType = void 0;
4var common_1 = require("../common/common");
5var hof_1 = require("../common/hof");
6var predicates_1 = require("../common/predicates");
7var coreservices_1 = require("../common/coreservices");
8var paramType_1 = require("./paramType");
9var hasOwn = Object.prototype.hasOwnProperty;
10var isShorthand = function (cfg) {
11 return ['value', 'type', 'squash', 'array', 'dynamic'].filter(hasOwn.bind(cfg || {})).length === 0;
12};
13var DefType;
14(function (DefType) {
15 DefType[DefType["PATH"] = 0] = "PATH";
16 DefType[DefType["SEARCH"] = 1] = "SEARCH";
17 DefType[DefType["CONFIG"] = 2] = "CONFIG";
18})(DefType || (DefType = {}));
19exports.DefType = DefType;
20function getParamDeclaration(paramName, location, state) {
21 var noReloadOnSearch = (state.reloadOnSearch === false && location === DefType.SEARCH) || undefined;
22 var dynamic = common_1.find([state.dynamic, noReloadOnSearch], predicates_1.isDefined);
23 var defaultConfig = predicates_1.isDefined(dynamic) ? { dynamic: dynamic } : {};
24 var paramConfig = unwrapShorthand(state && state.params && state.params[paramName]);
25 return common_1.extend(defaultConfig, paramConfig);
26}
27function unwrapShorthand(cfg) {
28 cfg = isShorthand(cfg) ? { value: cfg } : cfg;
29 getStaticDefaultValue['__cacheable'] = true;
30 function getStaticDefaultValue() {
31 return cfg.value;
32 }
33 var $$fn = predicates_1.isInjectable(cfg.value) ? cfg.value : getStaticDefaultValue;
34 return common_1.extend(cfg, { $$fn: $$fn });
35}
36function getType(cfg, urlType, location, id, paramTypes) {
37 if (cfg.type && urlType && urlType.name !== 'string')
38 throw new Error("Param '" + id + "' has two type configurations.");
39 if (cfg.type && urlType && urlType.name === 'string' && paramTypes.type(cfg.type))
40 return paramTypes.type(cfg.type);
41 if (urlType)
42 return urlType;
43 if (!cfg.type) {
44 var type = location === DefType.CONFIG
45 ? 'any'
46 : location === DefType.PATH
47 ? 'path'
48 : location === DefType.SEARCH
49 ? 'query'
50 : 'string';
51 return paramTypes.type(type);
52 }
53 return cfg.type instanceof paramType_1.ParamType ? cfg.type : paramTypes.type(cfg.type);
54}
55/** returns false, true, or the squash value to indicate the "default parameter url squash policy". */
56function getSquashPolicy(config, isOptional, defaultPolicy) {
57 var squash = config.squash;
58 if (!isOptional || squash === false)
59 return false;
60 if (!predicates_1.isDefined(squash) || squash == null)
61 return defaultPolicy;
62 if (squash === true || predicates_1.isString(squash))
63 return squash;
64 throw new Error("Invalid squash policy: '" + squash + "'. Valid policies: false, true, or arbitrary string");
65}
66function getReplace(config, arrayMode, isOptional, squash) {
67 var defaultPolicy = [
68 { from: '', to: isOptional || arrayMode ? undefined : '' },
69 { from: null, to: isOptional || arrayMode ? undefined : '' },
70 ];
71 var replace = predicates_1.isArray(config.replace) ? config.replace : [];
72 if (predicates_1.isString(squash))
73 replace.push({ from: squash, to: undefined });
74 var configuredKeys = common_1.map(replace, hof_1.prop('from'));
75 return common_1.filter(defaultPolicy, function (item) { return configuredKeys.indexOf(item.from) === -1; }).concat(replace);
76}
77var Param = /** @class */ (function () {
78 function Param(id, type, location, urlConfig, state) {
79 var config = getParamDeclaration(id, location, state);
80 type = getType(config, type, location, id, urlConfig.paramTypes);
81 var arrayMode = getArrayMode();
82 type = arrayMode ? type.$asArray(arrayMode, location === DefType.SEARCH) : type;
83 var isOptional = config.value !== undefined || location === DefType.SEARCH;
84 var dynamic = predicates_1.isDefined(config.dynamic) ? !!config.dynamic : !!type.dynamic;
85 var raw = predicates_1.isDefined(config.raw) ? !!config.raw : !!type.raw;
86 var squash = getSquashPolicy(config, isOptional, urlConfig.defaultSquashPolicy());
87 var replace = getReplace(config, arrayMode, isOptional, squash);
88 var inherit = predicates_1.isDefined(config.inherit) ? !!config.inherit : !!type.inherit;
89 // array config: param name (param[]) overrides default settings. explicit config overrides param name.
90 function getArrayMode() {
91 var arrayDefaults = { array: location === DefType.SEARCH ? 'auto' : false };
92 var arrayParamNomenclature = id.match(/\[\]$/) ? { array: true } : {};
93 return common_1.extend(arrayDefaults, arrayParamNomenclature, config).array;
94 }
95 common_1.extend(this, { id: id, type: type, location: location, isOptional: isOptional, dynamic: dynamic, raw: raw, squash: squash, replace: replace, inherit: inherit, array: arrayMode, config: config });
96 }
97 Param.values = function (params, values) {
98 if (values === void 0) { values = {}; }
99 var paramValues = {};
100 for (var _i = 0, params_1 = params; _i < params_1.length; _i++) {
101 var param = params_1[_i];
102 paramValues[param.id] = param.value(values[param.id]);
103 }
104 return paramValues;
105 };
106 /**
107 * Finds [[Param]] objects which have different param values
108 *
109 * Filters a list of [[Param]] objects to only those whose parameter values differ in two param value objects
110 *
111 * @param params: The list of Param objects to filter
112 * @param values1: The first set of parameter values
113 * @param values2: the second set of parameter values
114 *
115 * @returns any Param objects whose values were different between values1 and values2
116 */
117 Param.changed = function (params, values1, values2) {
118 if (values1 === void 0) { values1 = {}; }
119 if (values2 === void 0) { values2 = {}; }
120 return params.filter(function (param) { return !param.type.equals(values1[param.id], values2[param.id]); });
121 };
122 /**
123 * Checks if two param value objects are equal (for a set of [[Param]] objects)
124 *
125 * @param params The list of [[Param]] objects to check
126 * @param values1 The first set of param values
127 * @param values2 The second set of param values
128 *
129 * @returns true if the param values in values1 and values2 are equal
130 */
131 Param.equals = function (params, values1, values2) {
132 if (values1 === void 0) { values1 = {}; }
133 if (values2 === void 0) { values2 = {}; }
134 return Param.changed(params, values1, values2).length === 0;
135 };
136 /** Returns true if a the parameter values are valid, according to the Param definitions */
137 Param.validates = function (params, values) {
138 if (values === void 0) { values = {}; }
139 return params.map(function (param) { return param.validates(values[param.id]); }).reduce(common_1.allTrueR, true);
140 };
141 Param.prototype.isDefaultValue = function (value) {
142 return this.isOptional && this.type.equals(this.value(), value);
143 };
144 /**
145 * [Internal] Gets the decoded representation of a value if the value is defined, otherwise, returns the
146 * default value, which may be the result of an injectable function.
147 */
148 Param.prototype.value = function (value) {
149 var _this = this;
150 /**
151 * [Internal] Get the default value of a parameter, which may be an injectable function.
152 */
153 var getDefaultValue = function () {
154 if (_this._defaultValueCache)
155 return _this._defaultValueCache.defaultValue;
156 if (!coreservices_1.services.$injector)
157 throw new Error('Injectable functions cannot be called at configuration time');
158 var defaultValue = coreservices_1.services.$injector.invoke(_this.config.$$fn);
159 if (defaultValue !== null && defaultValue !== undefined && !_this.type.is(defaultValue))
160 throw new Error("Default value (" + defaultValue + ") for parameter '" + _this.id + "' is not an instance of ParamType (" + _this.type.name + ")");
161 if (_this.config.$$fn['__cacheable']) {
162 _this._defaultValueCache = { defaultValue: defaultValue };
163 }
164 return defaultValue;
165 };
166 var replaceSpecialValues = function (val) {
167 for (var _i = 0, _a = _this.replace; _i < _a.length; _i++) {
168 var tuple = _a[_i];
169 if (tuple.from === val)
170 return tuple.to;
171 }
172 return val;
173 };
174 value = replaceSpecialValues(value);
175 return predicates_1.isUndefined(value) ? getDefaultValue() : this.type.$normalize(value);
176 };
177 Param.prototype.isSearch = function () {
178 return this.location === DefType.SEARCH;
179 };
180 Param.prototype.validates = function (value) {
181 // There was no parameter value, but the param is optional
182 if ((predicates_1.isUndefined(value) || value === null) && this.isOptional)
183 return true;
184 // The value was not of the correct ParamType, and could not be decoded to the correct ParamType
185 var normalized = this.type.$normalize(value);
186 if (!this.type.is(normalized))
187 return false;
188 // The value was of the correct type, but when encoded, did not match the ParamType's regexp
189 var encoded = this.type.encode(normalized);
190 return !(predicates_1.isString(encoded) && !this.type.pattern.exec(encoded));
191 };
192 Param.prototype.toString = function () {
193 return "{Param:" + this.id + " " + this.type + " squash: '" + this.squash + "' optional: " + this.isOptional + "}";
194 };
195 return Param;
196}());
197exports.Param = Param;
198//# sourceMappingURL=param.js.map
\No newline at end of file