-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathobject-mapper.js
More file actions
126 lines (106 loc) · 2.85 KB
/
object-mapper.js
File metadata and controls
126 lines (106 loc) · 2.85 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
123
124
125
126
'use strict';
var utils = require('./utils')
, getKeyValue = require('./get-key-value')
, setKeyValue = require('./set-key-value')
, _undefined
;
/**
* Map a object to another using the passed map
* @param fromObject
* @param toObject
* @param propertyMap
* @returns {*}
* @constructor
*/
function ObjectMapper(fromObject, toObject, propertyMap) {
var propertyKeys;
if (typeof propertyMap === 'undefined') {
propertyMap = toObject;
toObject = _undefined;
}
if (typeof toObject === 'undefined') {
toObject = {};
}
propertyKeys = Object.keys(propertyMap);
return _map(fromObject, toObject, propertyMap, propertyKeys);
}
module.exports = ObjectMapper;
module.exports.merge = ObjectMapper;
module.exports.getKeyValue = getKeyValue;
module.exports.setKeyValue = setKeyValue;
/**
* Function that handle each key from map
* @param fromObject
* @param toObject
* @param propertyMap
* @param propertyKeys
* @returns {*}
* @private
* @recursive
*/
function _map(fromObject, toObject, propertyMap, propertyKeys) {
var fromKey
, toKey
;
if (propertyKeys.length) {
fromKey = propertyKeys.splice(0, 1)[0];
if (propertyMap.hasOwnProperty(fromKey)) {
toKey = propertyMap[fromKey];
toObject = _mapKey(fromObject, fromKey, toObject, toKey);
} else {
toObject = null;
}
return _map(fromObject, toObject, propertyMap, propertyKeys);
} else {
return toObject;
}
}
/**
* Function that calls get and set key values
* @param fromObject
* @param fromKey
* @param toObject
* @param toKey
* @private
* @recursive
*/
function _mapKey(fromObject, fromKey, toObject, toKey) {
var fromValue
, restToKeys
, _default // = null
, transform
;
if (Array.isArray(toKey) && toKey.length) {
toKey = toKey.slice();
restToKeys = toKey.splice(1);
toKey = toKey[0];
}
if (toKey instanceof Object && Object.getPrototypeOf(toKey) === Object.prototype) {
_default = toKey.default; // || null;
transform = toKey.transform;
toKey = toKey.key;
}
if (Array.isArray(toKey)) {
transform = toKey[1];
_default = toKey[2]; // || null;
toKey = toKey[0];
}
if (typeof _default === 'function') {
_default = _default(fromObject, fromKey, toObject, toKey);
}
fromValue = getKeyValue(fromObject, fromKey);
if (typeof fromValue === 'undefined') { // || fromValue === null) {
fromValue = _default;
}
if (typeof fromValue !== 'undefined' && typeof transform === 'function') {
fromValue = transform(fromValue, fromObject, toObject, fromKey, toKey);
}
if (typeof fromValue === 'undefined' || typeof toKey === 'undefined') {
return toObject;
}
toObject = setKeyValue(toObject, toKey, fromValue);
if (Array.isArray(restToKeys) && restToKeys.length) {
toObject = _mapKey(fromObject, fromKey, toObject, restToKeys);
}
return toObject;
}