Skip to content

Instantly share code, notes, and snippets.

@ChaseWest
ChaseWest / react-table-component.js
Created May 24, 2014 07:25
React Table Component for creating a very basic html table
var Table = React.createClass({
render: function render() {
var _self = this;
var thead = React.DOM.thead({},
React.DOM.tr({},
this.props.cols.map(function (col) {
return React.DOM.th({}, col);
})));
@ChaseWest
ChaseWest / parse-HTML-without-DOMParser.js
Created May 10, 2014 10:18
Parse HTML string without DOMParser API
var HTML = "<div><span>Test</span></div>";
var body = document.querySelector("body");
function parseHTML(htmlString){
var frag = document.createDocumentFragment(),
el = document.createElement( "div" );
el.setAttribute("id", "wrapper");
el.innerHTML = HTML;
frag.appendChild(el);
@ChaseWest
ChaseWest / HTML-parse.js
Created May 10, 2014 09:48
Parse HTML string to document object and insert into DOM
var HTML = "<div></span>Test</span></div>";
var body = document.querySelector("body");
function parseHTML(htmlString){
var parser = new DOMParser();
var fragment = document.createDocumentFragment();
fragment.appendChild(parser.parseFromString(htmlString, "text/html").querySelector("body"));
body.appendChild(fragment);
function is(val, type){
switch(type){
case "finite":
return typeof val === "number" && Number.isFinite(val);
case "integer":
return typeof val === "number" && Number.isInteger(val);
case "NaN":
return typeof val === "number" && Number.isNaN(val);
case "positive":
@ChaseWest
ChaseWest / random-key.js
Last active August 29, 2015 14:01
Generate a random string (ex: "5ons00tkwfsnstt9")
function getRandomKey(){
Math.random().toString(36).slice(2)
}
@ChaseWest
ChaseWest / attach-function.js
Created May 10, 2014 07:09
Generic pure js function to attach event listeners to DOM nodes
var is_attachEvent = !!document.attachEvent,
is_addEventListener = !!document.addEventListener;
function attach(target, event, fn){
switch(true){
case is_addEventListener:
target.addEventListener(event, fn);
break;
case is_attachEvent:
target.attachEvent("on"+event, fn);
@ChaseWest
ChaseWest / loop-function.js
Last active August 29, 2015 14:01
Loop function to determine the loop type by the object passed in. Set - (Array/Object), fn - (function to call on each iteration), order - (if order doesn't matter and set is an array, then use a while loop)
function loop(set, fn, order){
var is_array = Object.prototype.toString.call(set) === '[object Array]',
i, l;
if(is_array){
l = set.length;
if (!order) {
while(l--){ fn(l); }
}
else {