-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (44 loc) · 1.36 KB
/
index.js
File metadata and controls
65 lines (44 loc) · 1.36 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
var clone = require('clone');
function transformBoolean (value, options) {
if (options.trues.indexOf(value) > -1) {
return true;
}
if (options.falses.indexOf(value) > -1) {
return false;
}
return value;
}
function transform(object, boolMap, options) {
var booleanPicker = this,
result;
if (object === null || typeof object !=='object') {
return object;
}
if (!options) {
options = {};
}
options.trues = options.trues || [];
options.falses = options.falses || [];
result = clone(object)
if (!options.trues.length && !options.falses.length) {
return result;
}
for(var key in boolMap){
if(!(key in result)){
continue;
}
if(boolMap[key] === true){
result[key] = transformBoolean(result[key], options);
} else if (Array.isArray(boolMap[key])) {
if (Array.isArray(result[key])) {
result[key] = result[key].map(function(item){
return transform(item, boolMap[key][0], options)
});
}
} else if(typeof boolMap[key] === 'object'){
result[key] = transform(result[key], boolMap[key], options);
}
}
return result;
}
module.exports = transform;