-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwomMapper.ts
42 lines (34 loc) · 986 Bytes
/
OwomMapper.ts
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
const DATA_KEY = "DATA";
const INHERITED_KEYS_KEY = "INHERITED_KEYS";
export class OwomMapper<T> {
constructor(data: T, inheritedKeys?: (keyof T)[]) {
this[DATA_KEY] = data;
this[INHERITED_KEYS_KEY] = inheritedKeys;
this.useInheritedKeys();
}
get _() {
return {
useInheritedKeys: this.useInheritedKeys.bind(this), // @NOTE only for testing purposes
removeTemporaryData: this._removeTemporaryProperties.bind(this),
};
}
// @NOTE needs to be called manually in mapper when useDefineForClassFields is true
protected useInheritedKeys() {
this._map();
}
private _removeTemporaryProperties() {
delete this[DATA_KEY];
delete this[INHERITED_KEYS_KEY];
}
private _map() {
if (!this[INHERITED_KEYS_KEY]) {
return;
}
this[INHERITED_KEYS_KEY].map((key: keyof T) => {
const value = this[DATA_KEY][key];
if (typeof value !== "undefined") {
(this as any)[key] = value;
}
});
}
}