-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
53 lines (37 loc) · 1.03 KB
/
index.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
'use strict'
const map = require('lodash/map')
const isArray = require('lodash/isArray')
const cloneDeep = require('lodash/cloneDeep')
const isEmpty = require('lodash/isEmpty')
const difference = require('lodash/difference')
function diff (defaults, source) {
const result = cloneDeep(source)
map(result, function (value, key) {
if (typeof (value) === 'object') {
if (isArray(value)) {
const tmp = difference(value, defaults[ key ])
if (!tmp.length) {
delete result[ key ]
}
} else {
if ((typeof defaults[ key ] === 'undefined') ||
(value === null && defaults[ key ] !== null)) {
result[ key ] = value
} else {
const tmp = diff(defaults[ key ], value)
if (isEmpty(tmp)) {
delete result[ key ]
} else {
result[ key ] = tmp
}
}
}
} else {
if (defaults[ key ] === value) {
delete result[ key ]
}
}
})
return result
}
module.exports = diff