Skip to content

Instantly share code, notes, and snippets.

@klovadis
klovadis / gist:5170485
Created March 15, 2013 15:03
A Lua script for Redis that allows you to set hash values based on a dictionary table
-- sets all fields for a hash from a dictionary
local hmset = function (key, dict)
if next(dict) == nil then return nil end
local bulk = {}
for k, v in pairs(dict) do
table.insert(bulk, k)
table.insert(bulk, v)
end
return redis.call('HMSET', key, unpack(bulk))
end
@klovadis
klovadis / gist:2549131
Created April 29, 2012 10:03
How to use optional arguments in node.js
// example function where arguments 2 and 3 are optional
function example( err, optionalA, optionalB, callback ) {
// retrieve arguments as array
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// first argument is the error object
@klovadis
klovadis / gist:5170446
Created March 15, 2013 14:59
Two Lua scripts for Redis to turn HGETALL and HMGET into dictionary tables
-- gets all fields from a hash as a dictionary
local hgetall = function (key)
local bulk = redis.call('HGETALL', key)
local result = {}
local nextkey
for i, v in ipairs(bulk) do
if i % 2 == 1 then
nextkey = v
else
result[nextkey] = v
@klovadis
klovadis / newlinestream.js
Created May 26, 2012 11:53
Helper function to handle messages from a newline separated stream
// returns a function that spits out messages to a callback
// function; those messages have been split by newline
function newLineStream(callback) {
var buffer = '';
return (function (chunk) {
var i = 0, piece = '', offset = 0;
buffer += chunk;
while ( (i = buffer.indexOf('\n', offset)) !== -1) {
piece = buffer.substr(offset, i - offset);
offset = i + 1;
@klovadis
klovadis / gist:1293127
Created October 17, 2011 17:12
Private class methods using the commonjs module system
// ----- throw your class into one single file = a seperate scope
// constructor and export
var myClass = module.exports = function () {
// ..
}
// a public method
myClass.prototype.publicMethod = function () {
@klovadis
klovadis / gist:5170386
Created March 15, 2013 14:51
Lua script for redis to turn a dictionary table into a bulk reply
-- turns a dictionary table into a bulk reply table
local dict2bulk = function (dict)
local result = {}
for k, v in pairs(dict) do
table.insert(result, k)
table.insert(result, v)
end
return result
end
@klovadis
klovadis / gist:2549052
Created April 29, 2012 09:42
How to correctly raise errors in node.js
// example function that raises an error
function example (callback) {
// correct approach
return callback ( new Error('an error occurred') );
// NO! BAD KITTY!
return callback ('an error occurred');
@klovadis
klovadis / gist:5099102
Created March 6, 2013 12:51
Configuration of your JavaScript component
Namespace.ModuleName = function (opt) {
// default configuration values
var defaults = {
moduleContextSelector: '.select-an-awesome > .module',
moduleTitle: 'Some Arbitrary Title',
moduleTimeoutSpeed: 3000
}
@klovadis
klovadis / index.js
Created February 19, 2013 18:04
A basic webserver using the connect framework.
// to install the connect framework, go to your applications directory
// and type "npm install connect" in your command prompt.
// to launch the webserver, place this file in your application
// directory and execute it using "node index.js"
var connect = require('connect')
, app = connect()
, webserver = require('http').createServer(app);
@klovadis
klovadis / robot.js
Created December 5, 2012 14:10 — forked from Shipow/robot.js
Shipow#001
var Robot = function(robot) {
robot.rotateCannon(-90);
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.ahead();
//i'll add a clone but i need to refactor collision
//robot.clone();
};