UNPKG

1.03 kBJavaScriptView Raw
1/* @flow */
2
3import { normalizeChildren } from 'core/vdom/helpers/normalize-children'
4
5export function normalizeScopedSlots (
6 slots: { [key: string]: Function } | void,
7 normalSlots: { [key: string]: Array<VNode> }
8): any {
9 let res
10 if (!slots) {
11 res = {}
12 } else if (slots._normalized) {
13 return slots
14 } else {
15 res = {}
16 for (const key in slots) {
17 if (slots[key] && key[0] !== '$') {
18 res[key] = normalizeScopedSlot(slots[key])
19 }
20 }
21 }
22 // expose normal slots on scopedSlots
23 for (const key in normalSlots) {
24 if (!(key in res)) {
25 res[key] = proxyNormalSlot(normalSlots, key)
26 }
27 }
28 res._normalized = true
29 res.$stable = slots ? slots.$stable : true
30 return res
31}
32
33function normalizeScopedSlot(fn: Function): Function {
34 return scope => {
35 const res = fn(scope)
36 return res && typeof res === 'object' && !Array.isArray(res)
37 ? [res] // single vnode
38 : normalizeChildren(res)
39 }
40}
41
42function proxyNormalSlot(slots, key) {
43 return () => slots[key]
44}