/
utils.ts
197 lines (172 loc) · 5.63 KB
/
utils.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { GlobalMountOptions, RefSelector, Stub, Stubs } from './types'
import {
Component,
ComponentOptions,
ComponentPublicInstance,
ConcreteComponent,
Directive,
FunctionalComponent
} from 'vue'
import { config } from './config'
function mergeStubs(target: Record<string, any>, source: GlobalMountOptions) {
if (source.stubs) {
if (Array.isArray(source.stubs)) {
source.stubs.forEach((x) => (target[x] = true))
} else {
for (const [k, v] of Object.entries(source.stubs)) {
target[k] = v
}
}
}
}
// perform 1-level-deep-pseudo-clone merge in order to prevent config leaks
// example: vue-router overwrites globalProperties.$router
function mergeAppConfig(
configGlobalConfig: GlobalMountOptions['config'],
mountGlobalConfig: GlobalMountOptions['config']
): Required<GlobalMountOptions>['config'] {
return {
...configGlobalConfig,
...mountGlobalConfig,
globalProperties: {
...configGlobalConfig?.globalProperties,
...mountGlobalConfig?.globalProperties
} as Required<GlobalMountOptions>['config']['globalProperties']
}
}
export function mergeGlobalProperties(
mountGlobal: GlobalMountOptions = {}
): Required<GlobalMountOptions> {
const stubs: Record<string, any> = {}
const configGlobal: GlobalMountOptions = config?.global ?? {}
mergeStubs(stubs, configGlobal)
mergeStubs(stubs, mountGlobal)
const renderStubDefaultSlot =
mountGlobal.renderStubDefaultSlot ??
(configGlobal.renderStubDefaultSlot || config?.renderStubDefaultSlot) ??
false
if (config.renderStubDefaultSlot === true) {
console.warn(
'config.renderStubDefaultSlot is deprecated, use config.global.renderStubDefaultSlot instead'
)
}
return {
mixins: [...(configGlobal.mixins || []), ...(mountGlobal.mixins || [])],
plugins: [...(configGlobal.plugins || []), ...(mountGlobal.plugins || [])],
stubs,
components: { ...configGlobal.components, ...mountGlobal.components },
provide: { ...configGlobal.provide, ...mountGlobal.provide },
mocks: { ...configGlobal.mocks, ...mountGlobal.mocks },
config: mergeAppConfig(configGlobal.config, mountGlobal.config),
directives: { ...configGlobal.directives, ...mountGlobal.directives },
renderStubDefaultSlot
}
}
export const isObject = (obj: unknown): obj is Record<string, any> =>
!!obj && typeof obj === 'object'
// https://stackoverflow.com/a/48218209
export const mergeDeep = (
target: Record<string, unknown>,
source: Record<string, unknown>
) => {
if (!isObject(target) || !isObject(source)) {
return source
}
Object.keys(source).forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = sourceValue
} else if (sourceValue instanceof Date) {
target[key] = sourceValue
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
} else {
target[key] = sourceValue
}
})
return target
}
export function isClassComponent(component: unknown) {
return typeof component === 'function' && '__vccOpts' in component
}
export function isComponent(
component: unknown
): component is ConcreteComponent {
return Boolean(
component &&
(typeof component === 'object' || typeof component === 'function')
)
}
export function isFunctionalComponent(
component: unknown
): component is FunctionalComponent {
return typeof component === 'function' && !isClassComponent(component)
}
export function isObjectComponent(
component: unknown
): component is ComponentOptions {
return Boolean(component && typeof component === 'object')
}
export function textContent(element: Node): string {
// we check if the element is a comment first
// to return an empty string in that case, instead of the comment content
return element.nodeType !== Node.COMMENT_NODE
? element.textContent?.trim() ?? ''
: ''
}
export function hasOwnProperty<O extends {}, P extends PropertyKey>(
obj: O,
prop: P
): obj is O & Record<P, unknown> {
return obj.hasOwnProperty(prop)
}
export function isNotNullOrUndefined<T extends {}>(
obj: T | null | undefined
): obj is T {
return Boolean(obj)
}
export function isRefSelector(
selector: string | RefSelector
): selector is RefSelector {
return typeof selector === 'object' && 'ref' in selector
}
export function convertStubsToRecord(stubs: Stubs) {
if (Array.isArray(stubs)) {
// ['Foo', 'Bar'] => { Foo: true, Bar: true }
return stubs.reduce((acc, current) => {
acc[current] = true
return acc
}, {} as Record<string, Stub>)
}
return stubs
}
const isDirectiveKey = (key: string) => key.match(/^v[A-Z].*/)
export function getComponentsFromStubs(
stubs: Stubs
): Record<string, Component | boolean> {
const normalizedStubs = convertStubsToRecord(stubs)
return Object.fromEntries(
Object.entries(normalizedStubs).filter(([key]) => !isDirectiveKey(key))
) as Record<string, Component | boolean>
}
export function getDirectivesFromStubs(
stubs: Stubs
): Record<string, Directive | true> {
const normalizedStubs = convertStubsToRecord(stubs)
return Object.fromEntries(
Object.entries(normalizedStubs)
.filter(([key, value]) => isDirectiveKey(key) && value !== false)
.map(([key, value]) => [key.substring(1), value])
) as Record<string, Directive>
}
export function hasSetupState(
vm: ComponentPublicInstance
): vm is ComponentPublicInstance & {
$: { setupState: Record<string, unknown> }
} {
return (
vm &&
(vm.$ as unknown as { devtoolsRawSetupState: any }).devtoolsRawSetupState
)
}