Skip to content

Instantly share code, notes, and snippets.

@borgar
borgar / pre-commit
Created September 19, 2011 19:48
Git pre-commit hook to prevent JS trailing commas being checked in to projects.
#!/usr/bin/env python
"""
Git pre-commit hook to prevent JS trailing commas being checked in.
They have a tendency to sneak into the project and they crash MSIE7.
"""
import sys
import re
import subprocess
@borgar
borgar / each.js
Created November 15, 2010 22:18
A yielding each function for large collections.
/*
* A variation on a jQuery style each function for massive or labour intensive
* loops where the thread is yielded every so often to prevent script timeouts.
*
* (c) Borgar Thorsteinsson
* Licenced under the terms of the MIT/GPL software licenses.
*
* The function accepts these arguments:
*
* `obj`: an array, object, or string to be looped though.
@borgar
borgar / Tiny JavaScript tokenizer.js
Created June 24, 2010 12:33
A compact tokenizer written in JavaScript.
/*
* Tiny tokenizer
*
* - Accepts a subject string and an object of regular expressions for parsing
* - Returns an array of token objects
*
* tokenize('this is text.', { word:/\w+/, whitespace:/\s+/, punctuation:/[^\w\s]/ }, 'invalid');
* result => [{ token="this", type="word" },{ token=" ", type="whitespace" }, Object { token="is", type="word" }, ... ]
*
*/
@borgar
borgar / pngfix.sh
Created May 18, 2010 11:53
A bash script to run PNGs through both pngcrush and opticrush using unreasonably zealous settings.
#!/bin/bash
# This script attacks either all file arguments given to it, or all png's in a subtree (if
# a directory or no argument is given), and runs both pngcrush and optipng on them with liberal settings.
CRUSH=$(which pngcrush)
OPTI=$(which optipng)
OPTS1='-rem cHRM -rem gAMA -rem iCCP -rem sRGB -q'
OPTS2='-quiet -fix -o4'
TEMP="/tmp/temp.png"
BEFORE=0