Skip to content

Commit 2de79ec

Browse files
committed
wip: work on removing mergeDeep dep
1 parent 27b6d45 commit 2de79ec

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/mount.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ export function mount(
175175
}
176176

177177
if (global?.components) {
178-
for (const key of Object.keys(global.components))
178+
for (const key of Object.keys(global.components)) {
179179
app.component(key, global.components[key])
180+
}
180181
}
181182

182183
if (global?.directives) {

src/utils.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,63 @@
1-
import isString from 'lodash/isString'
21
import mergeWith from 'lodash/mergeWith'
32

43
import { GlobalMountOptions } from './types'
54

5+
const isString = (val: unknown): val is string => typeof val === 'string'
6+
7+
function deepMerge(...objects: object[]) {
8+
const isObject = (obj: any) => obj && typeof obj === 'object'
9+
10+
function deepMergeInner(target: object, source: object) {
11+
Object.keys(source).forEach((key: string) => {
12+
const targetValue = target[key]
13+
const sourceValue = source[key]
14+
15+
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
16+
target[key] = targetValue.concat(sourceValue)
17+
} else if (isObject(targetValue) && isObject(sourceValue)) {
18+
target[key] = deepMergeInner(
19+
Object.assign({}, targetValue),
20+
sourceValue
21+
)
22+
} else {
23+
target[key] = sourceValue
24+
}
25+
})
26+
27+
return target
28+
}
29+
30+
if (objects.length < 2) {
31+
throw new Error(
32+
'deepMerge: this function expects at least 2 objects to be provided'
33+
)
34+
}
35+
36+
if (objects.some((object) => !isObject(object))) {
37+
throw new Error('deepMerge: all values should be of type "object"')
38+
}
39+
40+
const target = objects.shift()
41+
let source: object
42+
43+
while ((source = objects.shift())) {
44+
deepMergeInner(target, source)
45+
}
46+
47+
return target
48+
}
49+
650
function mergeGlobalProperties(
751
configGlobal: GlobalMountOptions = {},
852
mountGlobal: GlobalMountOptions = {}
953
): GlobalMountOptions {
54+
// const merged: GlobalMountOptions = deepMerge(configGlobal, mountGlobal)
55+
// merged.components = {
56+
// ...mountGlobal.components,
57+
// ...configGlobal.components
58+
// }
59+
// console.log(merged)
60+
// return merged
1061
return mergeWith(
1162
{},
1263
configGlobal,
@@ -24,6 +75,7 @@ function mergeGlobalProperties(
2475
}
2576
}
2677
)
78+
// return mountGlobal
2779
}
2880

2981
export { isString, mergeGlobalProperties }

tests/config.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { h } from 'vue'
2+
13
import { config, mount } from '../src'
24
import Hello from './components/Hello.vue'
35

@@ -15,16 +17,24 @@ describe('config', () => {
1517

1618
describe('components', () => {
1719
const Component = {
18-
template: '<div>{{ msg }} <hello/></div>',
20+
components: { Hello },
21+
template: '<div>{{ msg }} <Hello /></div>',
1922
props: ['msg']
2023
}
2124

2225
it('allows setting components globally', () => {
23-
config.global.components = { Hello }
26+
const HelloOverride = {
27+
name: 'HelloOverride',
28+
props: ['msg'],
29+
render() {
30+
return () => h('div', `${this.msg} Hello world override`)
31+
}
32+
}
33+
config.global.components = { Hello: HelloOverride }
2434
const wrapper1 = mount(Component, { props: { msg: 'Wrapper1' } })
2535
const wrapper2 = mount(Component, { props: { msg: 'Wrapper2' } })
26-
expect(wrapper1.text()).toEqual('Wrapper1 Hello world')
27-
expect(wrapper2.text()).toEqual('Wrapper2 Hello world')
36+
expect(wrapper1.text()).toEqual('Wrapper1 Hello world override')
37+
expect(wrapper2.text()).toEqual('Wrapper2 Hello world override')
2838
})
2939

3040
it('allows overwriting globally set component config on a per mount instance', () => {

0 commit comments

Comments
 (0)