all files / model/ Property.js

56.52% Statements 13/23
18.18% Branches 2/11
91.67% Functions 11/12
56.52% Lines 13/23
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                  35×       19389×       3330× 13×   3317×         3045×       13×       27598×       172×       20217×       504×                                                   18389×       24921×      
import { isArray, last } from '../util'
import Coordinate from './Coordinate'
 
/*
  Internal helper class used by model/data/Node.
*/
export default class Property {
 
  constructor(definition) {
    this.definition = definition
  }
 
  isArray() {
    return isArray(this.definition.type)
  }
 
  isReference() {
    if (this.isArray()) {
      return last(this.definition.type) === 'id'
    } else {
      return this.definition.type === 'id'
    }
  }
 
  isText() {
    return Boolean(this.definition._isText)
  }
 
  isOwned() {
    return Boolean(this.definition.owned)
  }
 
  isOptional() {
    return Boolean(this.definition.optional)
  }
 
  isNotNull() {
    return Boolean(this.definition.notNull)
  }
 
  hasDefault() {
    return this.definition.hasOwnProperty('default')
  }
 
  getDefault() {
    return this.definition.default
  }
 
  createDefaultValue() {
    if (isArray(this.definition.type)) {
      return []
    }
    switch(this.definition.type) {
      case 'object':
        return {}
      case 'number':
        return -1
      case 'coordinate':
        return new Coordinate([], 0)
      case 'boolean':
        return false
      case 'id':
        return null
      case 'string':
        return ''
      default:
        return null
    }
  }
 
  get type() {
    return this.definition.type
  }
 
  get name() {
    return this.definition.name
  }
}