-
Notifications
You must be signed in to change notification settings - Fork 11
/
merge.js
45 lines (36 loc) · 1.02 KB
/
merge.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
var computed = require('./computed')
var resolve = require('./resolve')
var forEach = require('./for-each')
var forEachPair = require('./for-each-pair')
var addLookupMethods = require('./lib/add-lookup-methods')
module.exports = Merge
function Merge (sources) {
var raw = {}
var value = {}
var keys = new Set()
var instance = computed.extended(sources, function update () {
var currentKeys = []
forEach(sources, function (source) {
forEachPair(source, function (key, rawValue) {
currentKeys.push(key)
keys.add(key)
raw[key] = rawValue
value[key] = resolve(rawValue)
})
})
// remove deleted keys
Array.from(keys.values()).filter(function (k) {
return !currentKeys.includes(k)
}).forEach(function (key) {
keys.delete(key)
delete raw[key]
delete value[key]
})
return value
})
var result = function MutantMerge (listener) {
return instance(listener)
}
addLookupMethods(result, raw, instance.checkUpdated)
return result
}