Skip to content

Instantly share code, notes, and snippets.

View madbook's full-sized avatar

Matthew Lee madbook

  • Missouri, USA
  • 08:26 (UTC -05:00)
  • LinkedIn in/mdleemd
View GitHub Profile
@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 / 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 / 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)
/**
* 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 / 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