properties-parser.js 800 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
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
import {
  hasOwn
} from 'uni-shared'

function parseProperty (name, property, watch) {
  const type = [property.type]
  if (Array.isArray(property.optionalTypes)) {
    type.push(...property.optionalTypes)
  }
  const prop = Object.create(null)
  prop.type = type
  if (hasOwn(property, 'value')) {
    prop['default'] = prop.value
  }
  if (hasOwn(property, 'observer')) {
    watch[name] = property.observer
  }
  return prop
}

export function parseProperties (properties, vueComponentOptions) {
  if (!properties) {
    return
  }
  const props = Object.create(null)
  const {
    watch
  } = vueComponentOptions
  Object.keys(properties).forEach(name => {
    props[name] = parseProperty(name, properties[name], watch)
  })
  vueComponentOptions.props = props
}