-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (46 loc) · 1.08 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
54
55
const extend = require('extend')
/**
* Extend a `Map` instance with one or more other maps.
* @param {?(Boolean)} [merge = false]
* @param {Map} map
* @param {?(Map)} ...maps
* @return {Map}
*/
function extendMap(merge, map, ...maps) {
if ('boolean' !== typeof merge) {
maps.unshift(map)
map = merge
merge = false
}
const entries = Array.from(map.entries())
for (const m of maps) {
if (!m) { continue }
if (Array.isArray(m)) {
append(m)
} else if ('function' === typeof m.entries) {
append(m.entries())
} else if ('object' === typeof m) {
append(Object.entries(m))
}
}
for (let [ key, value ] of entries) {
const prev = map.get(key)
if (value && 'object' === typeof value && merge) {
value = extend(true, {}, prev, value)
}
map.set(key, value)
}
return map
function append(candidates) {
for (const entry of candidates) {
// istanbul ignore next
if (entry && 2 === entry.length) {
entries.push(entry)
}
}
}
}
/**
* Module exports
*/
module.exports = extendMap