-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.js
37 lines (33 loc) · 887 Bytes
/
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
/**
* Combine from left to right, 2 or more objects into a new single one.
* Properties will be shallow copied. Those with the same name will be
* overwriten by right most object.
*
* @tag Object
* @signature ( ...source: Object[] ): Object
*
* @param {Object[]} source Array of objects
*
* @return {Object}
*
* @example
* merge({a: "lorem"}, {b: "ipsum", c: 41}, {c: 42, b: undefined})
* // => { a: "lorem", b: "ipsum", c: 42 }
*/
module.exports = (...sources) => {
const result = {}
for (let i = 0, length = sources.length; i < length; i++) {
const sourceEntries = Object.entries(sources[i])
for (
let j = 0, sourceEntriesLength = sourceEntries.length;
j < sourceEntriesLength;
j++
) {
const [key, value] = sourceEntries[j]
if (value !== undefined) {
result[key] = value
}
}
}
return result
}