forked from Netflix/falcor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanData.js
114 lines (102 loc) · 3.29 KB
/
cleanData.js
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
var util = require('util');
var internalKeyMap = require('./../lib/internal');
var internalKeys = Object.keys(internalKeyMap);
var $modelCreated = require('./../lib/internal/model-created.js');
module.exports = {
clean: clean,
strip: strip,
internalKeys: internalKeys,
convertKey: convert,
convertModelCreatedAtoms: convertModelCreatedAtoms,
convertNodes: function convertNodesHeader(obj, transform) {
return convertNodes(null, null, obj, transform);
},
stripDerefAndVersionKeys: function(item) {
strip.apply(null, [item, '$size'].concat(internalKeys));
return item;
},
traverseAndConvert: traverseAndConvert
};
function convertModelCreatedAtoms(cache) {
convertNodes(null, null, cache, function transform(sentinel) {
if (sentinel.$type === 'atom' && sentinel[$modelCreated] &&
typeof sentinel.value !== 'object') {
return sentinel.value;
}
return sentinel;
});
};
function clean(item, options) {
options = options || {
strip: ['$size'].concat(internalKeys)
};
strip.apply(null, [item].concat(options.strip));
traverseAndConvert(item);
return item;
}
function convertNodes(parent, fromKey, obj, transform) {
if (obj != null && typeof obj === "object") {
if (obj.$type) {
parent[fromKey] = transform(obj);
}
Object.keys(obj).forEach(function(k) {
if (typeof obj[k] === "object" && !Array.isArray(obj[k])) {
convertNodes(obj, k, obj[k], transform);
}
});
}
return obj;
}
function convert(obj, config) {
if (obj != null && typeof obj === "object") {
Object.keys(config).forEach(function(key) {
// Converts the object.
if (obj[key]) {
obj[key] = config[key](obj[key]);
}
});
Object.keys(obj).forEach(function(k) {
if (typeof obj[k] === "object" && !Array.isArray(obj[k])) {
convert(obj[k], config);
}
});
}
return obj;
}
function traverseAndConvert(obj) {
if (Array.isArray(obj)) {
for (var i = 0; i < obj.length; i++) {
if (typeof obj[i] === "object") {
traverseAndConvert(obj[i]);
} else if (typeof obj[i] === "number") {
obj[i] = obj[i] + "";
} else if(typeof obj[i] === "undefined") {
obj[i] = null;
}
}
} else if (obj != null && typeof obj === "object") {
Object.keys(obj).forEach(function(k) {
if (typeof obj[k] === "object") {
traverseAndConvert(obj[k]);
} else if (typeof obj[k] === "number") {
obj[k] = obj[k] + "";
} else if(typeof obj[k] === "undefined") {
obj[k] = null;
}
});
}
return obj;
}
function strip(obj, key) {
var keys = Array.prototype.slice.call(arguments, 1);
var args = [0].concat(keys);
if (obj != null && typeof obj === "object") {
Object.keys(obj).forEach(function(k) {
if (~keys.indexOf(k)) {
delete obj[k];
} else if ((args[0] = obj[k]) != null && typeof obj[k] === "object") {
strip.apply(null, args);
}
});
}
}