All files index.js

100% Statements 38/38
100% Branches 22/22
100% Functions 11/11
100% Lines 38/38
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 751x 1x     29x       24x       15x 15x 15x       27x       26x 26x 26x 29x 29x   29x 10x 4x     10x 10x       19x 19x 20x       26x 16x   26x       15x 15x 15x 15x 15x 15x 15x 15x 15x 15x     15x             1x 1x 1x    
var _id = 0
var sheet = document.head.appendChild(document.createElement("style")).sheet
 
function hyphenate(str) {
  return str.replace(/[A-Z]/g, "-$&").toLowerCase()
}
 
function insert(rule) {
  sheet.insertRule(rule, sheet.cssRules.length)
}
 
function createStyle(obj) {
  var id = "p" + _id++
  parse(obj, "." + id).forEach(insert)
  return id
}
 
function wrap(stringToWrap, wrapper) {
  return wrapper + "{" + stringToWrap + "}"
}
 
function parse(obj, classname, isInsideObj) {
  var arr = [""]
  isInsideObj = isInsideObj || 0
  for (var prop in obj) {
    var value = obj[prop]
    prop = hyphenate(prop)
    // Same as typeof value === 'object', but smaller
    if (!value.sub && !Array.isArray(value)) {
      if (/^(:|>|\.|\*)/.test(prop)) {
        prop = classname + prop
      }
      // replace & in "&:hover", "p>&"
      prop = prop.replace(/&/g, classname)
      arr.push(
        wrap(parse(value, classname, 1 && !/^@/.test(prop)).join(""), prop)
      )
    } else {
      value = Array.isArray(value) ? value : [value]
      value.forEach(function(value) {
        return (arr[0] += prop + ":" + value + ";")
      })
    }
  }
  if (!isInsideObj) {
    arr[0] = wrap(arr[0], classname)
  }
  return arr
}
 
export default function(h) {
  return function(nodeName) {
    var cache = {}
    return function(decls) {
      return function(attributes, children) {
        attributes = attributes || {}
        children = attributes.children || children
        var nodeDecls = typeof decls == "function" ? decls(attributes) : decls
        var key = JSON.stringify(nodeDecls)
        cache[key] || (cache[key] = createStyle(nodeDecls))
        attributes.class = [attributes.class, cache[key]]
          .filter(Boolean)
          .join(" ")
        return h(nodeName, attributes, children)
      }
    }
  }
}
 
export function keyframes(obj) {
  var id = "p" + _id++
  insert(wrap(parse(obj, id, 1).join(""), "@keyframes " + id))
  return id
}