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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / service-request.js
Last active August 29, 2015 14:21
Consume an ajax request in Node.js without blocking the browser request
var cache = require("./cache-service");
var http = require("./http-monitor");
var NOT_READY = { statusCode:202, data:"" };
function doRequest(serviceID, httpReq, serviceResults){
var httpRes = { //overrides httpRes.json() dispatches data without blocking the browser
json:function(statusCode, data){
if (arguments.length === 1){
data = statusCode;
@ernestlv
ernestlv / cache-service.js
Last active September 1, 2015 03:59
Node.js to retrieve data from a cache object
var cache = require("./cache");
function doRequest(url, httpRes){
var data = cache.get(url);
if (data){
httpRes.json(200, JSON.parse(data));
}
return !!data;
}
@ernestlv
ernestlv / http-monitor.js
Last active September 1, 2015 04:05
http - module to monitor an http request from Node.js
var http = require("http");
var log = require("./log");
var cache = require("./cache");
var getRequestTime = require("./request-time");
var requestList = []; //request buffer
//register a Node.js http request then monitor the request
//monitor means to check if the request is in process, a duplicate or it finished
function registerRequest(url){