Skip to content

Instantly share code, notes, and snippets.

@nicokruger
nicokruger / async-callback-bad.js
Created February 29, 2012 07:09
async-callback-bad
before(function (done) {
server.server(options, function (s, db, providers) {
//clear db and add a test user - "testuser"
db.user.remove({}, function () {
db.notification.remove({}, function () {
providers.provider1.insertBulk(item1, item2, item3],
function (err, result) {
providers.provider2.insert([item1, item2, item3]
function (err, result) {
providers.provider3.insert([item1, item2, item3]
@nicokruger
nicokruger / async-sync-pattern.js
Created February 29, 2012 07:13
async-sync-pattern
var actions = [
function () {
/* code for step 1 */
callback();
}, function () {
/* code for step 2 */
callback();
}, function () {
/* code for step 3 */
callback();
@nicokruger
nicokruger / synchelper.js
Created February 29, 2012 07:16
synchelper
var _ = require("underscore");
/**
* First argument is the callback to call when all are done
* all other callbacks are functions of the form
* function (callback, ...) { }
* and must call callback when they are done
* the ... will be any additional parameters from the previous step as
* the first parameter of all functions is partially applied to be the callback
* on creation of sequence.
@nicokruger
nicokruger / synchelper.js
Created February 29, 2012 07:21
synchelper-test.js
ar _ = require("underscore"),
assert = require("assert"),
synchelper = require("../js/synchelper.js");
describe("Sync helper", function () {
it("should preserve the arguments", function (done) {
synchelper.waitForAll(
done,
function (callback) {
callback(1);
@nicokruger
nicokruger / async-better.js
Created February 29, 2012 07:30
async-better
before(function (done) {
server.server(options, function (s, db, providers) {
synchelper.waitForAll(
function () {
s.listen();
done();
},
function (callback) { db.notification.remove({}, callback); },
function (callback) { providers.provider1.insertBulk([item1, item2, item3], callback); },
function (callback) { providers.provider2.insertBulk([item1, item2, item3], callback); },
@nicokruger
nicokruger / find-the-bug1.py
Created March 1, 2012 19:49
find the bug part one
import calendar
import sys
def ConvertDays(days):
year = 1980
while days > 365:
if calendar.isleap(year):
if days > 366:
days -= 366
year += 1
@nicokruger
nicokruger / js-proto.js
Created March 4, 2012 07:45
js-proto-deprecated
var NotAuthenticated = function () {
this.name = "Not authenticated";
Error.call(this, "Not authenticated");
Error.captureStackTrace(this, arguments.callee);
};
NotAuthenticated.prototype.__proto__ = Error.prototype;
... later on ....
if (error instanceof NotAuthenticated) {
@nicokruger
nicokruger / spd-povray.sh
Created March 13, 2012 11:07
spd-povray
for F in `find . -executable`; do ./$F -r 4 > tmp.pov; povray +O$F.png +A0.5 +AM2 +W1600 +H1200 tmp.pov; done
@nicokruger
nicokruger / inotify-test.js
Created March 13, 2012 18:57
inotify-test.js
var Inotify = require('inotify').Inotify;
var inotify = new Inotify(); //persistent by default, new Inotify(false) //no persistent
var data = {}; //used to correlate two events
var callback = function(event) {
var mask = event.mask;
var type = mask & Inotify.IN_ISDIR ? 'directory ' : 'file ';
event.name ? type += ' ' + event.name + ' ': ' ';
@nicokruger
nicokruger / git-foreach.sh
Created March 18, 2012 16:42
Execute a command for all commits in a git repository.
#!/bin/bash
if ! [ -d ".git" ]; then
echo "Script must be run from within a git repository"
exit 1
fi
if [ $# -ne 1 ]; then
echo "Please specify a script to execute for each commit"
echo "Script will recieve <repo_dir> <commit ref> as arguments"