Skip to content

Instantly share code, notes, and snippets.

View madbook's full-sized avatar

Matthew Lee madbook

  • reddit
  • Pittsburgh, PA
View GitHub Profile
@madbook
madbook / catchleaks.js
Created December 20, 2012 14:56
Function for detecting variable leaks at runtime. Could also be used to do 'dry run' method calls, to see what properties of their object they will change. Works by cloning the window object, calling the function, comparing, then reverting any changes to the window. Practical? Not really. Kind of neat though.
/**
* catches any modifications to the given object (usually the global/window)
* object, and places them in a 'trap' object. Can prevent variable leak
* from missing var statements, as well as any modifications to the 'this'
* object inside of the function.
* @param {Object} t - trap object to use. Its necessary to pass in instead
* of returning because we still might want the return
* value of the function we're calling.
* @param {Function} f - function to call
* @param {Object} c - context to call function in. this is also the object
/**
* creates an array-like list of items. each item can have multiple 'terms'
* associated with it, and you can filter the list of items down by accessing
* those terms as properties of the object. To get the results of the query
* just end with parens, like:
*
* myObj.foo.bar() = [all objects with foo and bar terms];
*
* passing in values into the function call will add them as values with the
* current query as their terms. so
@madbook
madbook / map.coffee
Created February 22, 2013 19:54
my own implementation of the es6 map spec, written in coffeescript
toObject = (val) ->
if !val?
throw new TypeError()
return Object(val)
@madbook
madbook / nodeobject.js
Last active December 16, 2015 22:28
Simple constructor function for creating tree-like object structures. Useful if you need simple inheritance on a tree-like data structure.
/**
* every NodeObject's prototype is another NodeObject. The lowest level
* NodeObject's prototype is the NodeObject prototype.
*
* Each NodeObject has two default properties
* {NodeObject} parent : reference to the NodeObject that created this, also
* will be this NodeObject's prototype
* {NodeObjects[]} childNodes : every node created from this node using the
* create method will be added to this array
*
@madbook
madbook / __compile__.js
Created July 27, 2013 05:19
A contrived example of a function for templating javascript files in javascript. The `__compile__` function extracts the function body of a passed in anonymous function, then (optionally) replaces keywords (e.g., __foo__) with properties from a passed in object.
/**
* given a function, strips out its body code and optionally replaces
* template tags (variable names surrounded by double-underscores,
* e.g., __foo__) replacing them with values from a passed in object
*/
__compile__ = function (fnc, obj) {
var body = fnc.toString().match(/^function\s*[0-9a-zA-Z_$]*\s*\([\w\s,\$_]*\)\s*\{(?:\s*\n?)?([\w\W\s]*)\n?\}$/m)[1]
if (obj === undefined)
return body
@madbook
madbook / debug-helper.js
Last active December 21, 2015 20:00
replace `obj` with a module object. This wraps every method of that object that dumps nested, collapsible console logs any time those methods are called. logs include arguments, return value, and execution time.
for (i in obj)
if (typeof obj[i] === 'function' && i !== 'toString')
(function (method) {
var fnc = obj[method]
obj[method] = function () {
var x
console.groupCollapsed(this + " -> " + method)
console.dir(this)
console.dir(arguments)
console.time(method + ' time')
@madbook
madbook / git blame all
Created September 17, 2013 17:47
gives you a line count per author, can take a while depending on size of project. modify (php|js|html|css) with whatever file extensions you want to check.
git ls-tree --name-only -r -z HEAD | egrep -z -Z -E '\.(php|js|html|css)$' | xargs -0 -n1 git blame --line-porcelain | grep "^author " | sort | uniq -c | sort -nr
@madbook
madbook / Brainluck.js
Created January 19, 2014 19:54
A brainfuck interpreter in javascript. My solution to CodeWars kata "My smallest code interpreter (aka Brainf**k)"
function brainLuck (code, input) {
var interpreter = new BrainLuck(code)
return interpreter.execute(input)
}
function BrainLuck (code) {
// data
this.i = null
this.data = null
// output
function* range(begin, end) {
for (let i = begin; i < end; i++)
yield i;
}
let buzzWord = (n) =>
'' + ((n % 3 ? '' : 'fizz') + (n % 5 ? '' : 'buzz') || n)
let fizzBuzz = (max = 100) =>
[for (n of range(1, max)) buzzWord(n)]
@madbook
madbook / gulpfile.coffee
Created August 7, 2014 16:42
coffescript gulpfile
gulp = require 'gulp'
browserify = require 'gulp-browserify'
react = require 'gulp-react'
gulp.task 'scripts', () ->
gulp.src 'src/jsx/app.jsx'
.pipe browserify()
.pipe react()
.pipe gulp.dest 'build/js'