Skip to content

Instantly share code, notes, and snippets.

View usefulthink's full-sized avatar

Martin Schuhfuss usefulthink

  • Hamburg / Germany
  • 03:58 (UTC +02:00)
View GitHub Profile
@usefulthink
usefulthink / README.md
Created March 12, 2014 11:10
Beispiele zu der Session "EVENT-LOOPS UND 
ASYNCHRONE PROGRAMMIERUNG*" (JSdays München 2014)

... Will update this page soon ...

@usefulthink
usefulthink / Gruntfile.js
Last active August 29, 2015 13:57
grunt-task lazyloading
module.exports = function (grunt) {
grunt.initConfig({
// as usual, just make sure that – for multitasks – there always needs to be
// an `options`-entry to correctly detect them
copy: {
options: {},
dist: {
files: [{
// ....
}]
@usefulthink
usefulthink / commit-timesheet.js
Created May 5, 2014 18:03
practicing node streams – script generates a git commit-log, parses it and reformats into a day-by-day-log useful for time-tracking.
#!/usr/bin/env node
var fs = require('fs'),
util = require('util'),
spawn = require('child_process').spawn,
stream = require('stream');
function dateString(date) { return date.toISOString().split('T')[0]; }
function timeString(date) { return date.toTimeString().slice(0,5); }
@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