Skip to content

Instantly share code, notes, and snippets.

View ernestlv's full-sized avatar
Hello World!

Ernest Leyva ernestlv

Hello World!
View GitHub Profile
@ernestlv
ernestlv / test-socket.html
Last active August 29, 2015 14:21
test-socket - page to test a socket
<html>
<body>
<h1>Socket.io Test!!! :)</h1>
<form id="myForm">
<input id="firstname" type="text">
<input id="lastname" type="text">
<input id="submit" type="submit">
</form>
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
<script>
@ernestlv
ernestlv / reactjs-index.js
Last active August 29, 2015 14:27
ReactJS module to compose a page and its different sections and components
(function(){
function doComponent(data){
return <div id={data.id}>
<h3>{data.title}</h3>
<div className="clients">{data.content.clients}</div>
<div className="sales"}>{data.content.sales}</div>
</div>;
}
@ernestlv
ernestlv / walk-dom.js
Last active August 29, 2015 14:27
script walks The DOM from any node and prints any text content
function walkDOM(node, cb){
cb(node);
node = node.firstChild;
while(node){
walkDOM(node, cb);
node = node.nextSibling;
}
}
function walkUp(node, cb){
var parent = node.parentElement;
@ernestlv
ernestlv / array.js
Last active August 29, 2015 14:27
converts and array like object to an array
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);
function listToArray() {
return slice(arguments);
}
function listToArray2() {
return Array.prototype.slice.call(arguments);
}
@ernestlv
ernestlv / compare2.js
Last active August 29, 2015 14:27
compare two DOM trees walking the dom
function compare(nodeA, nodeB) {
if ( !nodeA && !nodeB ) return true; //edge case
if ( nodeA.nodeType === nodeB.nodeType && nodeA.tagName === nodeB.tagName && nodeA.nodeValue === nodeB.nodeValue ){
var i = 0;
var childA = nodeA.childNodes[i];
var childB = nodeB.childNodes[i];
while(childA && childB){
if (!compare(childA, childB)) return false; //some child is not equal
i++;
childA = nodeA.childNodes[i];
@ernestlv
ernestlv / dropdowns.js
Last active September 27, 2015 22:15
Times mouse events to display mega dropdowns smooth and fast. By default waits half a sec to trigger the menu.
function desktop($trigger, id){
var $menu = $trigger.find('+ .dropdown-menu'), delay=st.delay, delay2 = ~~delay/3;
//bootstrap catches the click event so we need to jump to the link here
$trigger.on('click', function(e){
clearTimeout(waiting[id]);
if (id === inside){
inside = 0;
//we need to trigger click tracking since click event is not fired
@ernestlv
ernestlv / search-recipe.html
Created September 30, 2015 17:30
HTML snippet of a recipe in a food network search result
<article class="recipe">
<div class="pull-right">
<a href="#"><img data-src="holder.js/161x121/industrial" width="161" height="121" alt=""></a>
</div>
<header>
<h6><a href="#"><b class="key">Chicken</b> Piccata</a></h6>
<p>Recipe courtesy of <a href="#">Giada De Laurentiis</a></p>
</header>
<div class="media">
<div class="pull-left">
@ernestlv
ernestlv / staircase.js
Created November 16, 2015 21:58
Stair-case Problem
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
// now we can read/parse input
var n = parseInt(input, 10);
var str = [];
@ernestlv
ernestlv / toArray.js
Created December 16, 2015 18:56
function to convert an Array-Like object to true array
var toArray = Function.prototype.call.bind(Array.prototype.slice);
@ernestlv
ernestlv / safeInvoke
Last active April 16, 2016 02:58
wraps the given function p in a try catch so is safer to call it in some contexts. e.g. analytic functions in unit tests.
/*
wraps the given function p in a try catch
so is safer to call it in some contexts. e.g. analytic functions in unit tests.
*/
Object.prototype.try = function (p){
var fn = function(){};
if (typeof this[p] === 'function'){
fn = function(){
try {
this[p].apply(this, [].slice(arguments));