-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostabee-mapper-mixin.html
63 lines (60 loc) · 2 KB
/
hostabee-mapper-mixin.html
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
<!--
@license
Copyright (c) 2019 Hostabee SAS.
This program is available under Apache License Version 2.0.
-->
<script>
/**
* @namespace Hostabee
*/
window.Hostabee = window.Hostabee || {};
/**
* @mixinFunction
* @polymer
* @memberof Hostabee
*/
Hostabee.MapperMixin = (superClass) => class MapperMixin extends superClass {
/**
* Transforms the given entity using the provided mapping. Returns a copy
* of the comment with properties listed in the mapping values renamed
* like the keys.
*
* For example, if the mapping is `{prop: 'myProp'}`, the property
* "myProp" of the given comment will be renamed "prop".
*
* @param {Object} entity The entity to be transformed/converted.
* @param {Object} mapping The mapping to use for the transformation/conversion.
* @return a copy of the entity with all properties present in the mapper
* values renamed like the keys.
*/
applyMapping(entity, mapping = {}) {
var obj = Object.assign({}, entity);
Object.keys(mapping).forEach(key => {
obj[key] = obj[mapping[key]];
delete obj[mapping[key]];
});
return obj;
}
/**
* Transforms the given entity using the provided mapping. Returns a copy
* of the comment with properties listed in the mapping values renamed
* like the keys.
*
* For example, if the mapping is `{prop: 'myProp'}`, the property
* "prop" of the given comment will be renamed "myProp".
*
* @param {Object} entity The entity to be transformed/converted.
* @param {Object} mapping The mapping to use for the transformation/conversion.
* @return a copy of the entity with all properties present in the
* mapper keys renamed like the values.
*/
reverseApplyMapping(entity, mapping = {}) {
var obj = Object.assign({}, entity);
Object.keys(mapping).forEach(key => {
obj[mapping[key]] = obj[key];
delete obj[key];
});
return obj;
}
};
</script>