-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjsonConverter.js
48 lines (37 loc) · 1.29 KB
/
jsonConverter.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
var deepEqual = require('deep-equal'),
excludeProps = require('./excludeProps'),
includeProps = require('./includeProps');
function processProperties(object, plainInstance, extraExclude, extraInclude, tempObject){
Object.keys(object).forEach(function(key){
var item = object[key];
if(
item !== undefined &&
typeof item !== 'function' &&
!deepEqual(plainInstance[key], item)
){
tempObject[key] = item;
}
});
includeProps.forEach(function(key){
key in object && (tempObject[key] = object[key]);
});
extraInclude && extraInclude.forEach(function(key){
key in object && (tempObject[key] = object[key]);
});
excludeProps.forEach(function(key){
delete tempObject[key];
});
extraExclude && extraExclude.forEach(function(key){
delete tempObject[key];
});
}
function jsonConverter(object, extraExclude, extraInclude){
var plainInstance = new object.constructor(),
tempObject = (Array.isArray(object) || object instanceof Array) ? [] : {};
processProperties(object, plainInstance, extraExclude, extraInclude, tempObject);
if(!Object.keys(tempObject).length){
return;
}
return tempObject;
}
module.exports = jsonConverter;