Skip to content

Instantly share code, notes, and snippets.

View ecasilla's full-sized avatar
🏠
Working from home

Ernie Casilla ecasilla

🏠
Working from home
  • This Crust Planet
  • Washington,DC
View GitHub Profile
@ecasilla
ecasilla / Mock Module
Created November 13, 2014 19:30
Node Mock Module Loader
var vm = require('vm');
var fs = require('fs');
var path = require('path');
/**
* Helper for unit testing:
* - load module with mocked dependencies
* - allow accessing private state of the module
*
* @param {string} filePath Absolute path to module (file to load)
@ecasilla
ecasilla / Random.js
Created November 19, 2014 16:58
Pure JS Random Function
function random(m_w, m_z) {
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
}
@ecasilla
ecasilla / curry.js
Created November 19, 2014 17:15
Currying In Js
function curry(fn) {
//create sunt double function to check artiy of the calling function i.e fn
return function() {
//check the length of args and if its greater copy them and return a new function
if (fn.length > arguments.length) {
//copy logic
var slice = Array.prototype.slice;
var args = slice.apply(arguments)
//new function with args copied
return function() {
@ecasilla
ecasilla / http.js
Created November 21, 2014 23:52
Express HTTPS
//run in the command line one at a time
//openssl genrsa -out privatekey.pem 1024
//openssl req -new -key privatekey.pem -out certreq.csr
//openssl x509 -req -days 3650 -in certreq.csr -signkey -private.pem -out newcert.pem
var express = require('express'),
https = require('https'),
fs = require('fs');
privateKey = fs.readFileSync('path/to/privateKey.pem'),
cert = fs.readFileSync('path/to/newCert.pem'),
@ecasilla
ecasilla / md5-path.js
Created December 22, 2014 13:57
append md5 to path for browser caching
var calculateMD5String, filepath, md5;
md5 = require('MD5');
calculateMD5String = function(path) {
return '-' + md5(fs.readFileSync(path));
};
filepath = "app" + calculateMD5String("app.js") + ".js";
@ecasilla
ecasilla / batman.js
Created December 23, 2014 01:20
Batman Joke
Array(21).join('lol'-2)+" Batman"
@ecasilla
ecasilla / hello.b
Created December 25, 2014 23:25
First Hello World
main(){
extrn a,b,c;
putchar(a); putchar(b); putchar(c); putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';
//First appeard in 1972
@ecasilla
ecasilla / clear_mongo.js
Last active December 25, 2021 20:44
Clear mongo db before mocha specs
var config = require('path/to/config');
var mongoose = require('mongose');
process.env.NODE_ENV = 'test';
before(function (done) {
function clearCollections() {
for (var collection in mongoose.connection.collections) {
mongoose.connection.collections[collection].remove(function() {});
@ecasilla
ecasilla / Range.js
Created February 3, 2015 03:33
Simple Range Fn for numbers
var Range = {
create: function (start, end) {
var results = [],
current = start,
step = start < end ? 1 : -1;
results.push(current);
while (current !== end) {
current += step;
@ecasilla
ecasilla / hex2rgb.js
Last active October 1, 2019 19:30
Hex2Rgb.js
function hex (hex){
if(/^#/.test(hex)){
hex = hex.slice(1);
}
if(hex.length !== 3 && hex.length !== 6 ){
throw new Error("Invaild hex String");
}
var digit = hex.split("");