UNPKG

1.18 kBJavaScriptView Raw
1/* @flow */
2
3// helper to process dynamic keys for dynamic arguments in v-bind and v-on.
4// For example, the following template:
5//
6// <div id="app" :[key]="value">
7//
8// compiles to the following:
9//
10// _c('div', { attrs: bindDynamicKeys({ "id": "app" }, [key, value]) })
11
12import { warn } from 'core/util/debug'
13
14export function bindDynamicKeys (baseObj: Object, values: Array<any>): Object {
15 for (let i = 0; i < values.length; i += 2) {
16 const key = values[i]
17 if (typeof key === 'string' && key) {
18 baseObj[values[i]] = values[i + 1]
19 } else if (process.env.NODE_ENV !== 'production' && key !== '' && key !== null) {
20 // null is a special value for explicitly removing a binding
21 warn(
22 `Invalid value for dynamic directive argument (expected string or null): ${key}`,
23 this
24 )
25 }
26 }
27 return baseObj
28}
29
30// helper to dynamically append modifier runtime markers to event names.
31// ensure only append when value is already string, otherwise it will be cast
32// to string and cause the type check to miss.
33export function prependModifier (value: any, symbol: string): any {
34 return typeof value === 'string' ? symbol + value : value
35}