This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----- 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 () { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Namespace.ModuleName = function (opt) { | |
// default configuration values | |
var defaults = { | |
moduleContextSelector: '.select-an-awesome > .module', | |
moduleTitle: 'Some Arbitrary Title', | |
moduleTimeoutSpeed: 3000 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
NewerOlder