Skip to content

Instantly share code, notes, and snippets.

View davidchambers's full-sized avatar

David Chambers davidchambers

View GitHub Profile
cheerio = require 'cheerio'
request = require 'request'
tutor = require 'tutor'
request 'http://www.wizards.com/magic/tcg/events.aspx?x=mtg/event/playerschamp/2012/cubepool',
(err, res, body) ->
throw err if err?
$ = cheerio.load body
names = $('a.nodec').map -> @text()

flatter

flatter is a command-line tool for determining a source file's "indentedness", which provides a rough indication of how well factored the code is.

$ flatter src

A woman has two children, (at least) one of whom is a boy. What's the probability that she has two boys?

       B    B-B   1/4
      /
     /
    B
   / \
  /   \
 /     G    B-G   1/4

/

@davidchambers
davidchambers / cuti.py
Last active December 11, 2015 02:58
Print known Uniform Type Identifiers as JSON
#!/usr/bin/env python
import json
import re
import subprocess
import sys
if len(sys.argv) > 1:
lsregister = sys.argv[1]
else:
@davidchambers
davidchambers / decorate.coffee
Created December 10, 2012 23:19
Effortlessly extend class methods in CoffeeScript
decorate = (klass, methodName, fn) ->
method = klass::[methodName]
klass::[methodName] = (args...) ->
method.apply this, args
fn.apply this, args
# Example
decorate Array, 'push', (args...) ->
@davidchambers
davidchambers / stats
Created November 2, 2012 18:47
Visualize significant lines of code for a directory of source files
#!/usr/bin/env bash
echo
for filename in $(find $1); do
loc=$(sed /^\s*$/d $filename | wc -l)
printf %24s $(echo $filename | sed 's!.*/!!')
printf %8s $loc
echo -n ' '
indent=$(seq -s ' ' 36 | sed s/[0-9]//g)
seq -s '+' $loc | sed -e s/[0-9]//g -e "s/.\{100\}/&,$indent/g" | tr , '\n'
@davidchambers
davidchambers / relative-timestamps.coffee
Created November 1, 2012 15:26
Relative timestamp formatting for jQuery.localize
{abs, floor, round} = Math
# > @localize = (timestamp) ->
# . $.localize.format (new Date timestamp), new Date "1 Mar 2012 00:30:45"
#
# > localize "30 Jan 2012 23:59:59"
# "2012-01-30"
# > localize "31 Jan 2012 00:00:00"
# "30 days ago"
# > localize "28 Feb 2012 23:59:59"
@davidchambers
davidchambers / cli-tricks.md
Created October 2, 2012 00:23
Handy command-line combos

Apply a patch on the clipboard to the current Git/Hg repo:

pbpaste | patch -p1

Or, if the trailing "\n" is missing:

pbpaste | xargs -0 echo | patch -p1
@davidchambers
davidchambers / paths.coffee
Created September 18, 2012 03:33
Problem: Determine all the paths for a given node.
# Problem: Determine all the paths for a given node.
#
# A
# / \
# / \
# B C
# / \ \
# / \ \
# C D D
# \
@davidchambers
davidchambers / languages.coffee
Created July 1, 2012 20:58
Attach language codes to request object
(req, res) ->
# Attach language codes to request object.
h = req.headers['accept-language']
req.languages = h and (/[^\s;-]+/.exec(l)[0] for l in h.split ',') or []