metadata_overrider.js
3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { stringify } from './facade/lang';
var _nextReferenceId = 0;
export var MetadataOverrider = (function () {
function MetadataOverrider() {
this._references = new Map();
}
/**
* Creates a new instance for the given metadata class
* based on an old instance and overrides.
*/
MetadataOverrider.prototype.overrideMetadata = function (metadataClass, oldMetadata, override) {
var props = {};
if (oldMetadata) {
_valueProps(oldMetadata).forEach(function (prop) { return props[prop] = oldMetadata[prop]; });
}
if (override.set) {
if (override.remove || override.add) {
throw new Error("Cannot set and add/remove " + stringify(metadataClass) + " at the same time!");
}
setMetadata(props, override.set);
}
if (override.remove) {
removeMetadata(props, override.remove, this._references);
}
if (override.add) {
addMetadata(props, override.add);
}
return new metadataClass(props);
};
return MetadataOverrider;
}());
function removeMetadata(metadata, remove, references) {
var removeObjects = new Set();
var _loop_1 = function(prop) {
var removeValue = remove[prop];
if (removeValue instanceof Array) {
removeValue.forEach(function (value) { removeObjects.add(_propHashKey(prop, value, references)); });
}
else {
removeObjects.add(_propHashKey(prop, removeValue, references));
}
};
for (var prop in remove) {
_loop_1(prop);
}
var _loop_2 = function(prop) {
var propValue = metadata[prop];
if (propValue instanceof Array) {
metadata[prop] = propValue.filter(function (value) { return !removeObjects.has(_propHashKey(prop, value, references)); });
}
else {
if (removeObjects.has(_propHashKey(prop, propValue, references))) {
metadata[prop] = undefined;
}
}
};
for (var prop in metadata) {
_loop_2(prop);
}
}
function addMetadata(metadata, add) {
for (var prop in add) {
var addValue = add[prop];
var propValue = metadata[prop];
if (propValue != null && propValue instanceof Array) {
metadata[prop] = propValue.concat(addValue);
}
else {
metadata[prop] = addValue;
}
}
}
function setMetadata(metadata, set) {
for (var prop in set) {
metadata[prop] = set[prop];
}
}
function _propHashKey(propName, propValue, references) {
var replacer = function (key, value) {
if (typeof value === 'function') {
value = _serializeReference(value, references);
}
return value;
};
return propName + ":" + JSON.stringify(propValue, replacer);
}
function _serializeReference(ref, references) {
var id = references.get(ref);
if (!id) {
id = "" + stringify(ref) + _nextReferenceId++;
references.set(ref, id);
}
return id;
}
function _valueProps(obj) {
var props = [];
// regular public props
Object.keys(obj).forEach(function (prop) {
if (!prop.startsWith('_')) {
props.push(prop);
}
});
// getters
var proto = obj;
while (proto = Object.getPrototypeOf(proto)) {
Object.keys(proto).forEach(function (protoProp) {
var desc = Object.getOwnPropertyDescriptor(proto, protoProp);
if (!protoProp.startsWith('_') && desc && 'get' in desc) {
props.push(protoProp);
}
});
}
return props;
}
//# sourceMappingURL=metadata_overrider.js.map