Skip to content

Commit 0ba5819

Browse files
committed
feat: support multiple sources
1 parent bf1392b commit 0ba5819

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

src/index.js

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,62 @@ const assign = function (target, source, handler) {
1515
return target
1616
}
1717

18-
export default function mergeData(target, source) {
19-
// Shallow copy target
20-
target = assign({}, target)
18+
export default function mergeData() {
19+
const args = [].slice.call(arguments)
20+
const target = assign({}, args[0])
21+
const sources = args.slice(1)
2122

22-
// Merge
23-
assign(target, source, propName => {
24-
const targetValue = target[propName]
25-
const sourceValue = source[propName]
26-
if (targetValue) {
27-
switch (propName) {
28-
// append
29-
case 'staticClass':
30-
target[propName] = (targetValue + ' ' + sourceValue).trim()
31-
break
32-
// override
33-
case 'attrs':
34-
case 'domProps':
35-
case 'scopedSlots':
36-
case 'staticStyle':
37-
case 'props':
38-
case 'hook':
39-
case 'transition':
40-
assign(targetValue, sourceValue)
41-
break
42-
// expand
43-
case 'class':
44-
case 'style':
45-
case 'directives':
46-
target[propName] = [].concat(sourceValue, targetValue)
47-
break
48-
// expand
49-
case 'on':
50-
case 'nativeOn':
51-
assign(targetValue, sourceValue, listenerName => {
52-
if (targetValue[listenerName]) {
53-
targetValue[listenerName] = [].concat(
54-
sourceValue[listenerName],
55-
targetValue[listenerName]
56-
)
57-
} else {
58-
targetValue[listenerName] = sourceValue[listenerName]
59-
}
60-
})
61-
break
62-
// override
63-
default:
64-
target[propName] = source[propName]
65-
break
23+
for (const i in sources) {
24+
const source = sources[i]
25+
assign(target, source, propName => {
26+
const targetValue = target[propName]
27+
const sourceValue = source[propName]
28+
if (targetValue) {
29+
switch (propName) {
30+
// append
31+
case 'staticClass':
32+
target[propName] = (targetValue + ' ' + sourceValue).trim()
33+
break
34+
// override
35+
case 'attrs':
36+
case 'domProps':
37+
case 'scopedSlots':
38+
case 'staticStyle':
39+
case 'props':
40+
case 'hook':
41+
case 'transition':
42+
assign(targetValue, sourceValue)
43+
break
44+
// expand
45+
case 'class':
46+
case 'style':
47+
case 'directives':
48+
target[propName] = [].concat(sourceValue, targetValue)
49+
break
50+
// expand
51+
case 'on':
52+
case 'nativeOn':
53+
assign(targetValue, sourceValue, listenerName => {
54+
if (targetValue[listenerName]) {
55+
targetValue[listenerName] = [].concat(
56+
sourceValue[listenerName],
57+
targetValue[listenerName]
58+
)
59+
} else {
60+
targetValue[listenerName] = sourceValue[listenerName]
61+
}
62+
})
63+
break
64+
// override
65+
default:
66+
target[propName] = source[propName]
67+
break
68+
}
69+
} else {
70+
target[propName] = sourceValue
6671
}
67-
} else {
68-
target[propName] = sourceValue
69-
}
70-
})
72+
})
73+
}
7174

7275
return target
7376
}

test/index.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,18 @@ const source = {
6060
}
6161
}
6262

63+
const source2 = {
64+
attrs: {
65+
new: true
66+
}
67+
}
68+
6369
const result = {
6470
attrs: {
6571
disabled: true,
6672
id: 'btn',
67-
type: 'button'
73+
type: 'button',
74+
new: true
6875
},
6976
class: [
7077
'btn3',
@@ -115,5 +122,5 @@ const result = {
115122
}
116123

117124
test('The merged data is correct.', () => {
118-
expect(mergeData(target, source)).toEqual(result)
125+
expect(mergeData(target, source, source2)).toEqual(result)
119126
})

0 commit comments

Comments
 (0)