Skip to content

Instantly share code, notes, and snippets.

View gerardpaapu's full-sized avatar
🏠
Working from home

Gerard Paapu gerardpaapu

🏠
Working from home
View GitHub Profile
@gerardpaapu
gerardpaapu / gist:1258044
Created October 2, 2011 22:18
Make an 8-bit png and strip the gamma headers
#!/bin/sh
filename=$(basename $1)
outputdir=$(dirname $1)
tempdir=$(mktemp -d -t 'png')
tempfile1="$tempdir/$filename"
tempfile2=$(mktemp -t 'png')
outputfile="$outputdir/$filename"
pngnq -f -e '.png' -d $tempdir $outputfile &&
@gerardpaapu
gerardpaapu / gist:1432072
Created December 5, 2011 02:41
Remove non-ascii characters from javascript strings
function toAscii(src) {
var ch, str, i, result = '';
// `str` should now be 'double-escaped' e.g. a newline in the
// original `src` should be represented by '\\n' in `str`
str = JSON.stringify(str);
// we start from str[1] and stop at str[-1] to
// drop the opening and closing quote marks when
// using JSON.stringify
@gerardpaapu
gerardpaapu / elements.py
Created December 6, 2011 20:25
A filter to extract elements from an html file/stream
#!/usr/bin/python
import sys
from optparse import OptionParser
from lxml import html
from lxml.cssselect import CSSSelector
from lxml.etree import XPath
def main():
parser = OptionParser(description="Select elements from an html file with css selectors",
// This is a port of an old scheme implementation of a Mathematica style 'Take' function
// (https://gist.github.com/379285)
//
// It takes the same arguments documented here: http://reference.wolfram.com/mathematica/ref/Take.html
//
// `take(arr, n);` returns the first `n` elements of `arr`
// `take(arr, -n);` returns the last `n` elements of `arr`
// `take(arr, [m, n]);` returns elements `m` through `n` of `arr`
// `take(arr, pat1, pat2, ...)` gives a nested array in which elements specified by `pat-i` are
// taken at level i in arr
@gerardpaapu
gerardpaapu / tests.js
Created December 9, 2011 11:45
UTF-8 encoding in javascript
var assert = require('assert'),
toBytes = require('./utf8.js').toBytes,
toUTF8Bytes = require('./utf8.js').toUTF8Bytes,
fromUTF8Bytes = require('./utf8.js').fromUTF8Bytes;
assert.deepEqual(toBytes(0x0), [0]);
assert.deepEqual(toBytes(0xff), [0xff]);
assert.deepEqual(toBytes(0x01ff), [0x01, 0xff]);
assert.deepEqual(toBytes(0xffff), [0xff, 0xff]);
@gerardpaapu
gerardpaapu / cleanString.js
Created December 9, 2011 12:08
Get a clean ASCII string from a JavaScript string
function cleanString(str, method, r) {
var result = '',
max = str.length,
i, ch;
for (i = 0; i < max; i++) {
ch = str.charCodeAt(i);
if (ch < 128) {
result += str.charAt(i);
@gerardpaapu
gerardpaapu / match.js
Created December 14, 2011 00:58
Simple recursive pattern matching in Javascript
// Simple recursive pattern matching
// =================================
//
// The provided `match` function matches recursively against
// javascript values, and returns a set of bindings made against
// the source.
//
// in these examples `match._` is aliased as `_`.
//
// `match(_, data)` succeeds
@gerardpaapu
gerardpaapu / ColorTest.js
Created January 19, 2012 02:52
Testing Color classes that construct colors from HSLA or RGBA
require(["src/Color"], function (Color) {
var getContext, runTests, width = 150, height = 150;
getContext = function (width, height) {
var c = document.createElement('canvas');
c.width = width;
c.height = height;
return c.getContext('2d');
};
#!/bin/sh
# uglifyjs as a beautifier https://github.com/mishoo/UglifyJS
# install with npm, `npm install -g uglifyjs`
uglifyjs --beautify --no-squeeze --no-mangle "$*"
// Me circa April 2009
Array.implement({
"collect": function collect(fn, bind) {
// fn.calls that throw an exception are
// excluded and the index is not incremented
// so that it's like they were never there.
// can be used instead of a.map( ... ).filter( ... )
// --------------------------------------------------
// Throwing is quite slow though, so you'd only really
// want to use it when fn is already throwing errors IMO