<|start_filename|>dist/docrel.js<|end_filename|>
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.create = create;
exports.createElement = createElement;
exports.setAttributes = setAttributes;
exports.setClassList = setClassList;
exports.setEventListeners = setEventListeners;
exports.appendChildren = appendChildren;
function create() {
  for (var _len = arguments.length, tags = Array(_len), _key = 0; _key < _len; _key++) {
    tags[_key] = arguments[_key];
  }

  return tags.map(function (tag) {
    return function (options, children) {
      return createElement(tag, options, children);
    };
  });
}

function createElement(tag) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var children = arguments[2];

  var result = document.createElement(tag);

  if (options.textContent) result.textContent = options.textContent;
  if (options.innerHTML) result.innerHTML = options.innerHTML;
  if (options.id) result.id = options.id;
  if (options.class) result.className = options.class;

  setAttributes(result, options.attrs);
  setClassList(result, options.classList);
  setEventListeners(result, options.events);

  appendChildren(result, children);

  return result;
}

function setAttributes(el) {
  var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  Object.keys(attrs).forEach(function (attrName) {
    if (attrs[attrName]) {
      el.setAttribute(attrName, attrs[attrName]);
    }
  });
}

function setClassList(el) {
  var classList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

  classList.forEach(function (className) {
    return el.classList.add(className);
  });
}

function setEventListeners(el) {
  var events = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  Object.keys(events).forEach(function (eventName) {
    var eventFn = events[eventName];
    el.addEventListener(eventName, eventFn);
  });
}

function appendChildren(node) {
  var children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

  children.forEach(function (child) {
    return node.appendChild(child);
  });
}

var a = exports.a = create("a")[0];
var article = exports.article = create("article")[0];
var aside = exports.aside = create("aside")[0];
var b = exports.b = create("b")[0];
var br = exports.br = create("br")[0];
var button = exports.button = create("button")[0];
var canvas = exports.canvas = create("canvas")[0];
var div = exports.div = create("div")[0];
var fieldset = exports.fieldset = create("fieldset")[0];
var footer = exports.footer = create("footer")[0];
var form = exports.form = create("form")[0];
var h1 = exports.h1 = create("h1")[0];
var h2 = exports.h2 = create("h2")[0];
var h3 = exports.h3 = create("h3")[0];
var h4 = exports.h4 = create("h4")[0];
var h5 = exports.h5 = create("h5")[0];
var h6 = exports.h6 = create("h6")[0];
var header = exports.header = create("header")[0];
var hr = exports.hr = create("hr")[0];
var i = exports.i = create("i")[0];
var iframe = exports.iframe = create("iframe")[0];
var img = exports.img = create("img")[0];
var input = exports.input = create("input")[0];
var label = exports.label = create("label")[0];
var li = exports.li = create("li")[0];
var link = exports.link = create("link")[0];
var main = exports.main = create("main")[0];
var meta = exports.meta = create("meta")[0];
var nav = exports.nav = create("nav")[0];
var ol = exports.ol = create("ol")[0];
var option = exports.option = create("option")[0];
var p = exports.p = create("p")[0];
var progress = exports.progress = create("progress")[0];
var script = exports.script = create("script")[0];
var section = exports.section = create("section")[0];
var select = exports.select = create("select")[0];
var span = exports.span = create("span")[0];
var style = exports.style = create("style")[0];
var sub = exports.sub = create("sub")[0];
var sup = exports.sup = create("sup")[0];
var svg = exports.svg = create("svg")[0];
var table = exports.table = create("table")[0];
var tbody = exports.tbody = create("tbody")[0];
var td = exports.td = create("td")[0];
var textarea = exports.textarea = create("textarea")[0];
var th = exports.th = create("th")[0];
var tr = exports.tr = create("tr")[0];
var ul = exports.ul = create("ul")[0];
var video = exports.video = create("video")[0];

<|start_filename|>src/docrel.js<|end_filename|>
export function create(...tags) {
  return tags.map(tag => (options, children) => {
    return createElement(tag, options, children)
  })
}

export function createElement(tag, options = {}, children) {
  let result = document.createElement(tag)

  if (options.textContent) result.textContent = options.textContent
  if (options.innerHTML) result.innerHTML = options.innerHTML
  if (options.id) result.id = options.id
  if (options.class) result.className = options.class

  setAttributes(result, options.attrs)
  setClassList(result, options.classList)
  setEventListeners(result, options.events)

  appendChildren(result, children)

  return result
}

export function setAttributes(el, attrs = {}) {
  Object.keys(attrs).forEach(attrName => {
    if (attrs[attrName]) {
      el.setAttribute(attrName, attrs[attrName])
    }
  })
}

export function setClassList(el, classList = []) {
  classList.forEach(className => el.classList.add(className))
}

export function setEventListeners(el, events = {}) {
  Object.keys(events).forEach(eventName => {
    let eventFn = events[eventName]
    el.addEventListener(eventName, eventFn)
  })
}

export function appendChildren(node, children = []) {
  children.forEach(child => node.appendChild(child))
}

export let a = create("a")[0]
export let article = create("article")[0]
export let aside = create("aside")[0]
export let b = create("b")[0]
export let br = create("br")[0]
export let button = create("button")[0]
export let canvas = create("canvas")[0]
export let div = create("div")[0]
export let fieldset = create("fieldset")[0]
export let footer = create("footer")[0]
export let form = create("form")[0]
export let h1 = create("h1")[0]
export let h2 = create("h2")[0]
export let h3 = create("h3")[0]
export let h4 = create("h4")[0]
export let h5 = create("h5")[0]
export let h6 = create("h6")[0]
export let header = create("header")[0]
export let hr = create("hr")[0]
export let i = create("i")[0]
export let iframe = create("iframe")[0]
export let img = create("img")[0]
export let input = create("input")[0]
export let label = create("label")[0]
export let li = create("li")[0]
export let link = create("link")[0]
export let main = create("main")[0]
export let meta = create("meta")[0]
export let nav = create("nav")[0]
export let ol = create("ol")[0]
export let option = create("option")[0]
export let p = create("p")[0]
export let progress = create("progress")[0]
export let script = create("script")[0]
export let section = create("section")[0]
export let select = create("select")[0]
export let span = create("span")[0]
export let style = create("style")[0]
export let sub = create("sub")[0]
export let sup = create("sup")[0]
export let svg = create("svg")[0]
export let table = create("table")[0]
export let tbody = create("tbody")[0]
export let td = create("td")[0]
export let textarea = create("textarea")[0]
export let th = create("th")[0]
export let tr = create("tr")[0]
export let ul = create("ul")[0]
export let video = create("video")[0]


<|start_filename|>package.json<|end_filename|>
{
  "name": "docrel",
  "version": "1.7.0",
  "description": "Slightly better document.createElement",
  "main": "dist/docrel.js",
  "scripts": {
    "dev": "babel -w src -d dist",
    "test": "jasmine test/docrel.spec.js"
  },
  "repository": "svileng/docrel",
  "author": {
    "name": "<NAME>",
    "url": "s2g.io"
  },
  "license": "MIT",
  "keywords": [
    "DOM",
    "createElement",
    "document",
    "utility",
    "wrapper"
  ],
  "devDependencies": {
    "babel-cli": "6.16.0",
    "babel-preset-es2015": "6.16.0",
    "jasmine": "2.5.2"
  }
}


<|start_filename|>test/docrel.spec.js<|end_filename|>
let {
  create,
  createElement,
  setAttributes,
  setClassList,
  setEventListeners,
  appendChildren,
  div
} = require("../dist/docrel.js")

describe("docrel", () => {

  describe("create", () => {
    beforeEach(() => {
      global.document = {
        createElement: jasmine.createSpy().and.returnValue({})
      }
    })

    it("calls createElement", () => {
      const [span] = create("span")
      span()
      expect(global.document.createElement).toHaveBeenCalledWith("span")
    })
  })

  describe("tag helpers", () => {
    beforeEach(() => {
      global.document = {
        createElement: jasmine.createSpy().and.returnValue({})
      }
    })

    it("calls createElement", () => {
      div()
      expect(global.document.createElement).toHaveBeenCalledWith("div")
    })
  })

  describe("createElement", () => {
    beforeEach(() => {
      global.document = {
        createElement: jasmine.createSpy().and.returnValue({})
      }
    })

    it("calls document.createElement with given param", () => {
      createElement("foo")
      expect(global.document.createElement).toHaveBeenCalledWith("foo")
    })

    it("sets textContent", () => {
      var el = createElement("foo", {textContent: "yo"})
      expect(el.textContent).toBe("yo")
    })

    it("sets id", () => {
      var el = createElement("foo", {id: "yo"})
      expect(el.id).toBe("yo")
    })

    it("sets className", () => {
      var el = createElement("foo", {class: "yo sup"})
      expect(el.className).toBe("yo sup")
    })
  })

  describe("setAttributes", () => {
    beforeEach(() => {
      this.node = {
        setAttribute: jasmine.createSpy()
      }
    })

    it("calls setAttribute for every given attribute", () => {
      setAttributes(this.node, {attrA: "foo", attrB: "bar"})
      expect(this.node.setAttribute).toHaveBeenCalledWith("attrA", "foo")
      expect(this.node.setAttribute).toHaveBeenCalledWith("attrB", "bar")
    })

    it("does nothing when no attributes given", () => {
      setAttributes(this.node, undefined)
      expect(this.node.setAttribute).not.toHaveBeenCalled()
    })
  })

  describe("setClassList", () => {
    beforeEach(() => {
      this.node = {
        classList: {add: jasmine.createSpy()}
      }
    })

    it("calls classList.add for every given string", () => {
      setClassList(this.node, ["foo", "bar", "baz"])
      expect(this.node.classList.add).toHaveBeenCalledWith("foo")
      expect(this.node.classList.add).toHaveBeenCalledWith("bar")
      expect(this.node.classList.add).toHaveBeenCalledWith("baz")
    })

    it("does nothing when no params given", () => {
      setAttributes(this.node, undefined)
      expect(this.node.classList.add).not.toHaveBeenCalled()
    })
  })

  describe("setEventListeners", () => {
    beforeEach(() => {
      this.node = {
        addEventListener: jasmine.createSpy()
      }
    })

    it("calls addEventListener for every given event", () => {
      setEventListeners(this.node, {ev1: "fn1", ev2: "fn2"})
      expect(this.node.addEventListener).toHaveBeenCalledWith("ev1", "fn1")
      expect(this.node.addEventListener).toHaveBeenCalledWith("ev2", "fn2")
    })

    it("does nothing when no params given", () => {
      setAttributes(this.node, undefined)
      expect(this.node.addEventListener).not.toHaveBeenCalled()
    })
  })

  describe("appendChildren", () => {
    beforeEach(() => {
      this.node = {
        appendChild: jasmine.createSpy()
      }
    })

    it("calls appendChild for every given child element", () => {
      appendChildren(this.node, ["foo", "bar"])
      expect(this.node.appendChild).toHaveBeenCalledWith("foo")
      expect(this.node.appendChild).toHaveBeenCalledWith("bar")
    })

    it("does nothing when no params given", () => {
      appendChildren(this.node, undefined)
      expect(this.node.appendChild).not.toHaveBeenCalled()
    })
  })

})
