Skip to content

Instantly share code, notes, and snippets.

@arian
arian / events.js
Created August 17, 2012 12:09
Multiple Event handlers idea with next()
var slice = Function.prototype.call.bind(Array.prototype.slice);
var forEach = Function.prototype.call.bind(Array.prototype.forEach);
var Event = function(event){
this.event = event || window.event;
this.locals = {};
};
Event.prototype.target = function(){
@arian
arian / gist:3345801
Created August 14, 2012 02:20
Make Jade templates requirable
./node_modules/.bin/jade --no-debug --client --out ./public/templates ./views/partials/*
sed -i 's/^function anonymous/require("jade\/runtime");module.exports = function/' ./public/templates/*
var jsdom = require('jsdom')
var fs = require('fs')
var nodes = fs.readFileSync("../nodes.js").toString();
jsdom.env({
features: {
QuerySelector: true
},
src: [nodes],
@arian
arian / parentify.js
Created March 22, 2012 20:02
Parentify primes
var parentify = function(prime){
var parent = prime.parent
if (!parent) throw new Error('Cannot parentify this prime')
var method = prime.prototype.parent = function(name){
var fn = parent[name]
if (typeof fn != 'function') throw new Error('This method called with parent() does not exist')
delete this.parent
var ret = fn.apply(this, array.slice(arguments, 1))
var mocha = require('mocha');
// setup
var suite = new mocha.Suite('', new mocha.Context),
utils = mocha.utils,
Reporter = mocha.reporters.Spec,
ui = mocha.interfaces['bdd'];
ui(suite);
suite.emit('pre-require', global);
def(emi.prototype, "emit", function(event){
var listeners = this._listeners, events, off = []
this.off = function(_event, fn){
if (_event == event) off.push(fn)
else emi.prototype.off.call(this, _event, fn)
return this
};
if (listeners && (events = listeners[event])){
var args = Array.prototype.slice.call(arguments, 1)
for (var i = 0, l = events.length; i < l; i++) events[i].apply(null, args)
@arian
arian / npm-data.js
Created March 1, 2012 23:18
Analyze NPM package data
var fs = require('fs');
var packages = JSON.parse(fs.readFileSync('/home/arian/.npm/-/all/.cache.json'));
var total = 0, zeros = 0, alphas = 0;
for (var name in packages){
var package = packages[name],
@arian
arian / FlowAsync.js
Created February 25, 2012 01:06
Flow Control for Node
var slice = Array.prototype.slice;
return function(){
var queue = [];
this.push = function(fn){
if (typeof fn != 'function'){
throw new Error('the passed argument is not a function');
describe('getStyle height / width / margin with CSS', function(){
var style, element;
it('[beforeAll]', function(){
var className = String.uniqueID();
style = $(document.createElement('style'));
style.type = 'text/css';
var definition = [
'.' + className + '{',
var re = /([\d.]+)(s|ms)?/, units = {ms: 1, s: 1e3};
var parseDuration = function(value){
var match = value.toString().match(re);
return match ? parseFloat(match[1]) * (units[match[2]] || 1) : parseFloat(value)
};
console.log(parseDuration(200));
console.log(parseDuration('200'));
console.log(parseDuration('200ms'));
console.log(parseDuration('.2s'));