Skip to content

Instantly share code, notes, and snippets.

@torgeir
torgeir / bean.js
Created March 25, 2011 09:13
nodejs java bean creator - takes -n for name and a comma separated -p for properties
var argv = require('optimist').argv;
var name = argv.n;
var fields = argv.p.split(',');
console.log('public class %s {', name);
eachField(function (field) {
createMember(field);
})
eachField(function (field) {
createGetter(field);
@torgeir
torgeir / unique.test.js
Created April 11, 2011 15:40
mongoose unique test
var assert = require('assert');
var mongoose = require('mongoose');
module.exports = {
'Model save callback should receive error on non unique names': function () {
var Schema = mongoose.Schema;
mongoose.model('User', new Schema({
name: { type: String, unique: true }
}));
@torgeir
torgeir / FizzBuzz.scala
Created April 25, 2011 16:39
FizzBuzz scala implementation
object FizzBuzz {
def apply(n: Int): String = {
(n % 3, n % 5) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => n toString
}
@torgeir
torgeir / fizzbuzz.js
Created April 25, 2011 16:55
FizzBuzz commonjs implementation
module.exports = function (n) {
var ret = '';
if (n % 3 === 0) ret += 'Fizz';
if (n % 5 === 0) ret += 'Buzz';
return ret || n;
};
@torgeir
torgeir / fizzbuzz.test.js
Created April 25, 2011 17:04
FizzBuzz expresso tests
var fizzbuzz = require('fizzbuzz')
, should = require('should');
module.exports = {
'should be a function' : function () {
fizzbuzz.should.be.an.instanceof(Function);
}
};
[
@torgeir
torgeir / FizzBuzzSpec.scala
Created April 25, 2011 17:05
FizzBuzz scala tests
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.WordSpec
class FizzBuzzSpec extends WordSpec with ShouldMatchers {
"FizzBuzz" should {
List(
(1, "1")
, (2, "2")
@torgeir
torgeir / watch.sh
Last active September 25, 2015 22:48
The only file-changed-watcher you'll ever need!
#!/bin/sh
#
# watch.sh - the only file-changed-watcher you'll ever need!
#
# Usage:
# $ sh watch.sh "echo File changed"
# $ sh watch.sh make "-not -name *.swp"
#
# one liner variant:
@torgeir
torgeir / parallelping.js
Created June 15, 2011 10:43
Ping series of ips in parallel with node
var spawn = require('child_process').spawn;
var i,
result = {},
host = '192.168.0.%s'
start = 1,
end = 255,
total = end - start;
for (i = start; i < end; i += 1) {
connect(host.replace('%s', i), result, done);
@torgeir
torgeir / roman.tests.js
Created June 17, 2011 06:48
Roman numerals expresso tests
var roman = require('../lib/roman')
, should = require('should');
module.exports = {
'test .version': function(){
roman.version.should.match(/^\d+\.\d+\.\d+$/);
},
'should expose toRoman' : function () {
roman.toRoman.should.be.a('function');
},
@torgeir
torgeir / roman.js
Created June 17, 2011 06:50
Roman numerals implementation
exports.version = '0.0.1';
var lookup = [
[1, 'i']
, [4, 'iv']
, [5, 'v']
, [9, 'ix']
, [10, 'x']
, [40, 'xl']
, [50, 'l']