-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (37 loc) · 1.24 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
/**
* @module multi-merge
* @author Emre BALCI <[email protected]> (github.com/eballci)
* @license MIT
*/
const forEach = Array.prototype.forEach
const defineProperty = Object.defineProperty
const hasOwnProperty = Object.prototype.hasOwnProperty
const getOwnPropertyNames = Object.getOwnPropertyNames
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
function merge() {
let overwrite = true
const destination = arguments[0]
if (typeof destination !== 'object') {
throw new TypeError(
'First argument, which is destination, have to be passed as object.'
)
}
forEach.call(arguments, function forEachSources(source, index) {
if (typeof source === 'boolean') {
overwrite = source
return
}
if (index === 0 || typeof source !== 'object') {
return
}
forEach.call(getOwnPropertyNames(source), function forEachKeys(key) {
if (!overwrite && hasOwnProperty.call(destination, key)) {
return
}
const descriptor = getOwnPropertyDescriptor(source, key)
defineProperty(destination, key, descriptor)
})
})
return destination
}
module.exports = merge