Skip to content

Instantly share code, notes, and snippets.

View bitmage's full-sized avatar

Brandon Mason bitmage

View GitHub Profile
@bitmage
bitmage / EventEmitter.coffee
Created June 11, 2013 23:04
EventEmitter in coffeescript Just as an exercise in understanding, here's a simple EventEmitter class written in coffeescript. This may help you to understand the fundamentals without getting lost in the specifics of a more robust implementation.
define ->
class EventEmitter
constructor: ->
@listeners = {}
@middleware = []
on: (event, action) ->
throw new Error "'#{event}' arg is not a function" if typeof action isnt 'function'
@bitmage
bitmage / memtools.coffee
Created June 5, 2013 21:44
These are some helper functions for interacting with memwatch. The goal is to make it easier to decorate your code with memory snapshots with as little interference and noise in the existing code structure as possible. You'll need Mozilla's memwatch in order to use it. The most useful things you'll want to look at are memdiff and postMortem.
sugar = require 'sugar'
memwatch = require 'memwatch'
logger = require './logger'
presetFormats =
raw: (stats) -> stats
default: (stats) -> stats.change.size
details: (stats) ->
top3 = stats.change.details.sortBy('size_bytes', true).slice(0, 3)
summary = {}
@bitmage
bitmage / map_reduce.coffee
Last active December 17, 2015 19:29
map/reduce example
# printout helper
counter = 1
println = (args...) -> console.log "#{counter++}:", args...
# example collection
names = ['Jim', 'Nancy', 'Sam', 'Cindy']
# example function
@bitmage
bitmage / oplog_mongo2.4.3.json
Created May 27, 2013 21:37
result of a mongoose push/save operation in mongodb v2.4.3
{ operation: 'set',
id: '51a0249adae44c01b0000003',
path: 'messages',
data:
[ { message: 'hey',
username: 'Bob',
timestamp: Fri May 24 2013 19:40:28 GMT-0700 (MST),
_id: 51a0249cdae44c01b0000005 },
{ message: 'hey',
username: 'Bob',
@bitmage
bitmage / oplog_mongo2.2.json
Created May 27, 2013 21:32
result of mongoose push/save operation in mongo 2.2.0
{ operation: 'pushAll',
id: '51a0249adae44c01b0000003',
path: 'messages',
data:
[ { message: 'asdf',
username: 'Bob',
timestamp: 'Mon May 27 2013 14:26:39 GMT-0700 (MST)',
_id: '51a3cf8f5f9a745a07000003' } ] }
curry: (fn, args...) -> fn.bind fn.prototype, args...
tandoor: (fn) ->
naan = (args...) ->
[_..., last] = args
unless (fn.length > 0 and args.length >= fn.length) or (fn.length == 0 and util.getType(last) is 'Function')
return util.curry naan, args...
fn args...
return naan
{EventEmitter} = require 'events'
eventChannel = new EventEmitter()
eventChannel.on 'error', (args...) -> console.log "Event Channel received error:", args...
module.exports = eventChannel
@bitmage
bitmage / replaceUrls.coffee
Created February 19, 2013 20:41
Asset Rack - asset post processing to replace urls with hashed urls
module.exports = (assets) ->
replaceUrls = (content) ->
result = content.toString()
for asset in assets.assets when asset.url and asset.specificUrl
#console.log "replacing: #{asset.url} with: #{asset.specificUrl}"
result = result.replace asset.url, asset.specificUrl
return result
for asset in assets.assets when asset.contents
@bitmage
bitmage / dbg.rb
Created January 16, 2013 17:02
helper for listening to a node.js app with node-webkit-agent: https://github.com/c4milo/node-webkit-agent
#!/usr/bin/ruby
puts "Need a process name to search for." && Process.exit() unless ARGV.count > 0
name = ARGV[0]
process = `ps aux | grep '#{name}' | grep -v grep | grep -v #{$$}`
if process.chomp.match "\n"
puts "Multiple processes found:\n #{process}"
Process.exit()
end
@bitmage
bitmage / destructure_test.coffee
Created August 8, 2012 00:41
destructuring removes binding context
# coffeescript version 1.3.3
should = require 'should'
nums =
current: 5
next: -> @current + 1
last: -> @current - 1
# passes