All files index.js

100% Statements 33/33
100% Branches 25/25
100% Functions 9/9
100% Lines 33/33
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 661x 1x 1x     13x       19x       19x 19x 21x     19x 19x       26x       19x 19x   19x 21x 21x 8x 8x 8x       19x 19x       13x 13x 13x 13x   13x 11x 11x 11x 11x     11x 11x     11x          
var _id = 0
var sheet = document.head.appendChild(document.createElement("style")).sheet
var serialize = JSON.stringify.bind(null)
 
function hyphenate(str) {
  return str.replace(/[A-Z]/g, "-$&").toLowerCase()
}
 
function insert(rule) {
  sheet.insertRule(rule, 0)
}
 
function createRule(className, decls, media) {
  var newDecls = []
  for (var property in decls) {
    typeof decls[property] !== "object" &&
      newDecls.push(hyphenate(property) + ":" + decls[property] + ";")
  }
  var rule = "." + className + "{" + newDecls.join("") + "}"
  return media ? media + "{" + rule + "}" : rule
}
 
function concat(str1, str2) {
  return str1 + (/^\w/.test(str2) ? " " : "") + str2
}
 
function parse(decls, child, media, className) {
  child = child || ""
  className = className || "p" + (_id++).toString(36)
 
  for (var property in decls) {
    var value = decls[property]
    if (typeof value === "object") {
      var nextMedia = /^@/.test(property) ? property : null
      var nextChild = nextMedia ? child : concat(child, property)
      parse(value, nextChild, nextMedia, className)
    }
  }
 
  insert(createRule(concat(className, child), decls, media))
  return className
}
 
export default function(h) {
  return function(nodeName) {
    var cache = {}
    return function(decls) {
      var isDeclsFunction = typeof decls === "function"
 
      return function(attributes, children) {
        attributes = attributes || {}
        children = attributes.children || children
        var key = serialize(attributes)
        cache[key] ||
          (cache[key] =
            (isDeclsFunction && parse(decls(attributes))) || parse(decls))
        var node = h(nodeName, attributes, children)
        node.attributes.class = [attributes.class, cache[key]]
          .filter(Boolean)
          .join(" ")
        return node
      }
    }
  }
}