all files / jDollarX/ jDollarX.js

96.83% Statements 61/63
94.44% Branches 34/36
90% Functions 9/10
96.77% Lines 60/62
2 statements, 3 branches Ignored     
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                                 16×     29× 15×       20× 18×   13× 13×                 29× 29× 29× 28× 14×     29× 28×                   28× 28×   28× 27× 27× 27× 16× 13×       11×       28×        
(function (root, factory) {
  /* eslint-disable */
  /* istanbul ignore next */
  Iif (typeof define === 'function' && define.amd) {
    define('J$X', factory)
  } else Eif (typeof module === 'object' && module.exports) {
    module.exports = factory()
  } else {
    root.J$X = factory()
  }
  /* eslint-enable */
})(this, function () {
  var $ = this.$ || function () {
    throw new Error(
      'Cannot find $ in global, run J$X.use( ... ) to assign.'
    )
  }
 
  var KEY_ALIASES = {
    className: 'class',
    htmlFor: 'for'
  }
 
  var ownProp = Object.prototype.hasOwnProperty
  var toString = Object.prototype.toString
  var slice = Array.prototype.slice
 
  function isArray (obj) {
    return toString.call(obj) === '[object Array]'
  }
 
  function ClearObject (obj) {
    for (var key in obj) Eif (ownProp.call(obj, key)) {
      this[key] = obj[key]
    }
  }
  ClearObject.prototype = null
 
  function append ($el, content) {
    if (content == null) {
      return
    } else if (typeof content === 'object') {
      $el.append(content)
    } else {
      var $dummy = $('<div>').text(content.toString())
      $el.append($dummy.html())
    }
  }
 
  function matchIndex (str, regExp, index) {
    var matches = str.match(regExp)
 
    if (matches != null) {
      return matches[index]
    } else {
      return null
    }
  }
 
  var J$X = function (name, props/*, children... */) {
    var children = slice.call(arguments, 2)
    var _props = new ClearObject(props)
    if (children.length > 1) {
      _props.children = children
    } else if (children.length === 1) {
      _props.children = children[0]
    }
 
    if (typeof name === 'string') { // tag name
      return J$X.dom(name, _props)
    } else Eif (typeof name === 'function') { // element constructor
      return name(_props)
    }
 
    throw new Error(
      'Invalid element name, either tag name or element constructor'
    )
  }
 
  J$X.use = function (use$) {
    $ = use$
  }
 
  J$X.dom = function (name, props) {
    var $el = $('<' + name + '>')
    var event
 
    for (var key in props) {
      var value = props[key]
      key = KEY_ALIASES[key] || key
      if (key === 'children') {
        if (!isArray(value)) {
          append($el, value)
        } else {
          for (var i = 0, l = value.length; i < l; i++) {
            append($el, value[i])
          }
        }
      } else if (key === 'style') {
        $el.css(value)
      } else if ((event = matchIndex(key, /^on([A-Z]\w*)$/, 1))) {
        $el.on(event.toLowerCase(), value)
      } else {
        $el.attr(key, value)
      }
    }
    return $el
  }
 
  return J$X
})