Skip to content

Instantly share code, notes, and snippets.

View usefulthink's full-sized avatar

Martin Schuhfuss usefulthink

  • Hamburg / Germany
  • 11:35 (UTC +02:00)
View GitHub Profile
@usefulthink
usefulthink / runlength-encoding.js
Created April 19, 2011 18:30
Simple implementation of a runlength-encoding algorithm
function encodeFrame(frame, idx) {
// RLE: MSB indicates a run-length (2-127) for the subsequent character
return frame.replace(/(.)\1{2,127}/g, function(s) {
return String.fromCharCode(128 + s.length) + s[0];
});
}
var decodeCache = {};
function decodeFrame(frame) {
return frame.replace(/[\u0080-\u00FF]./g, function replaceCallback(s) {
@usefulthink
usefulthink / gist:958595
Created May 6, 2011 08:07
Javascript WTF - clicktracking.
// this is an anonymized but otherwise unmodified copy from the
// documentation of a renowned tracking-provider.
var toSleep = true;
function stopTimer(){toSleep=false;}
function clickTracker(LinkName){
var secureID="XXX";
var redirectUrl="http://wehave.teh-trackingz.com/1px.gif";
@usefulthink
usefulthink / huffman.coffee
Created July 30, 2011 12:45
huffman-tree builder in coffeescript
class Node
constructor: (@p, @value, @childs=[]) ->
class HuffmanTreeBuilder
_nodes: []
_rootNode: null
# probabilities = value1: probability1, value2: probability2, ...
constructor: (probabilities = {}) ->
(@_add new Node p, value) for value, p of probabilities
@usefulthink
usefulthink / gist:1253397
Created September 30, 2011 10:37
rewrite /*.html -> /*/
RewriteEngine On
# alles, was keine datei ist, auf .html endet und nicht index.html ist...
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} \.html$
RewriteCond ${REQUEST_FILENAME} !^index\.html$
# ... umschreiben zu /.../ und redirecten
RewriteRule ^(.*)\.html$ /$1/ [R=301,QSA,L]
@usefulthink
usefulthink / async-queue.js
Created December 6, 2011 09:54
simple queueing of async-functions
var _queue = [];
function enqueue(data, itemProcessor, callback) {
for(var i=0, n=data.length; i<n; i++) {
(function(item) {
_queue.push(function(next) {
itemProcessor(item, function() {
window.setTimeout(next, 0);
});
});
} (data[i]));
@usefulthink
usefulthink / bt-search.coffee
Created January 27, 2012 09:14
backtracking combinator
# searches a combination of the given elements that matches toMatch
# @param c array - initially empty, carries the current combination throught
# the recursion
# @param els array - the predefined set of elements
# @param toMatch string - the rest-string to be matched with the elements
#
# @return an array of combinations that recursively match
searchCombination = (c, els, rest) ->
return c if rest.length==0
@usefulthink
usefulthink / .bashrc
Created January 27, 2012 10:51
dotfile-snippets - some pretty useful things to have in your dotfiles
# compute the current git branch, if any
ps1_git_extra() {
local branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ -z "$branch" ] ; then return ; fi
echo -n "[$branch]"
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[33m\]$(ps1_git_extra)\[\033[00m\]\$ '
@usefulthink
usefulthink / .htaccess
Created March 13, 2012 11:31
a micro bash-library for unit-testing RewriteRules
RewriteEngine On
RewriteBase /
# redirect anything from /common/* to /common/prefix/*, except of course
# /common/prefix/* itself (and except eventually existing files).
# ... dont rewrite if a file with that name exists
RewriteCond %{REQUEST_FILENAME} !-f
# ... dont rewrite URIs starting with /common/prefix/ (/foo/common/prefix
@usefulthink
usefulthink / dabblet.css
Created May 25, 2012 16:19
Scrolling shadows by @kizmarh and @LeaVerou
/**
* Scrolling shadows by @kizmarh and @leaverou
* Only works in browsers supporting background-attachment: local; & CSS gradients
* Degrades gracefully
*/
html {
background: white;
font: 120% sans-serif;
}
@usefulthink
usefulthink / tpl.js
Created June 5, 2012 10:48
JS µTemplating
(function(global) {
var tpl = function(template, data) {
var proc = function(data) {
var ret = template;
for (var k in data) {
if (data.hasOwnProperty(k)) {
ret = ret.replace('#{'+k+'}', data[k]);
}
}