Created
May 19, 2015 23:46
-
-
Save bloodyowl/05a1b3497dddd32a5adf to your computer and use it in GitHub Desktop.
ES7 bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function first() { | |
if(!this.length) { | |
return this[0] | |
} | |
throw new Error("empty list") | |
} | |
export default Array.prototype | |
export function $(selector, root = document) { | |
return Array.from(root.querySelectorAll(selector)) | |
} | |
export function getAttribute(attributeName) { | |
return this::first().getAttribute(attributeName) | |
} | |
export function setAttribute(attributeName, value) { | |
this.forEach((item) => { | |
item.setAttribute(attributeName, value) | |
}) | |
return this | |
} | |
export function setProperties(properties) { | |
this.forEach((item) => { | |
Object.assign(item, properties) | |
}) | |
return this | |
} | |
export function getProperty(property) { | |
return this::first()[attributeName] | |
} | |
export function setInnerHTML(string) { | |
this.forEach((item) => { | |
item.innerHTML = string | |
}) | |
return this | |
} | |
export function remove() { | |
this.forEach((item) => { | |
const parent = item.parentNode | |
if(parent) { | |
parent.removeChild(item) | |
} | |
}) | |
return this | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {$, setAttribute, setInnerHTML} from "./dom" | |
$("div") | |
::setAttribute("data-foo", "bar") | |
::setInnerHTML("hey") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment