Skip to content

Instantly share code, notes, and snippets.

View marshallswain's full-sized avatar
😄

Marshall Thompson marshallswain

😄
View GitHub Profile
@marshallswain
marshallswain / server.js
Last active December 16, 2015 10:29
Server.js file for LocomotiveJS.
var locomotive = require('locomotive'),
env = process.env.NODE_ENV || 'development',
port = process.argv[2] || process.env.PORT || 3000,
address = '0.0.0.0';
console.log(process.argv);
locomotive.boot(__dirname, env, function(err, server) {
if (err) { throw err; }
server.listen(port, address, function() {
@marshallswain
marshallswain / gist:5420924
Created April 19, 2013 15:03
Line to simplify ExpressJS logger output.
this.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' })); this.use(express.favicon());
@marshallswain
marshallswain / gist:7129990
Created October 24, 2013 01:48
Socket.io response options
// send to current request socket client
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.sockets.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
function md5cycle(x, k) {
var a = x[0], b = x[1], c = x[2], d = x[3];
a = ff(a, b, c, d, k[0], 7, -680876936);
d = ff(d, a, b, c, k[1], 12, -389564586);
c = ff(c, d, a, b, k[2], 17, 606105819);
b = ff(b, c, d, a, k[3], 22, -1044525330);
a = ff(a, b, c, d, k[4], 7, -176418897);
d = ff(d, a, b, c, k[5], 12, 1200080426);
c = ff(c, d, a, b, k[6], 17, -1473231341);
@marshallswain
marshallswain / 1_dpd-exposed.md
Last active January 1, 2016 03:49
A custom collection module that allows you to expose request properties to dashboard scripts by modifying the domain. In this example, I've added a headers property to the domain.

Installation

  1. Drop the dpd-exposed.js file into your node_modules directory. (You might have to create one if you installed Deployd using the dpd command line utility.)
  2. Open your Deployd dashboard and click the big green add button. You'll see the Exposed Collection type. Use it to create a collection like you would any other.

Usage

  1. Add some properties to your collection.
  2. Add some data to your collection. Remember that these scripts are run per instance on records from the database. It won't even run unless you add some data to the collection.
  3. In a dashboard script (ie. ON GET), use the headers variable directly.

console.log(headers);

@marshallswain
marshallswain / firebase.list.js
Created December 23, 2013 08:21
Firebase CanJS LIst Controller
var FirebaseListController = can.Control.extend({
defaults: {
// The main Firebase URL, including the trailing slash.
baseURL: 'https://bchm.firebaseIO.com/',
// The location of the list of data in Firebase.
location: "test_list",
// The HTML attribute that should be added to a template element in the list.
// It will need to be unique and will contain a single value, like an id, not a class.
// unused because I can't get can.Mustache to work with it right now. Keeps printing __!!__
// firebaseKeyName: "data-firebasekey",
@marshallswain
marshallswain / camelCase.js
Created December 26, 2013 02:47
camelCase a string. Originally found here: http://valschuman.blogspot.com/2012/08/javascript-camelcase-function.html?view=flipcard Thank you, Val Schuman;
Wrote a function to CamelCase a string in JavaScript. Because, AFAIK, CamelCasing applies only to programming, the function makes sure the string conforms to a legal variable name standard, removing all illegal characters, as well as making sure the string starts with a letter (not a number or an underscore.)
You can add the function to the String object prototype:
String.prototype.toCamelCase = function() {
// remove all characters that should not be in a variable name
// as well underscores an numbers from the beginning of the string
var s = this.replace(/([^a-zA-Z0-9_\- ])|^[_0-9]+/g, "").trim().toLowerCase();
// uppercase letters preceeded by a hyphen or a space
s = s.replace(/([ -]+)([a-zA-Z0-9])/g, function(a,b,c) {
@marshallswain
marshallswain / forever-start.js
Created December 29, 2013 00:55
Deployd custom start script and forever-monitor examples Install Deployd locally using npm install deployd --save. Then npm install forever-monitor --save.
var forever = require('forever-monitor');
var child = new (forever.Monitor)('server.js', {
max: 10,
silent: true,
options: []
});
child.on('exit', function () {
console.log('Your server has exited after 3 restarts');
@marshallswain
marshallswain / country-codes.json
Last active January 1, 2016 17:29
List of ISO 3166 Two-character Country Codes
[
{"code", "AF", "name", "AFGHANISTAN"},
{"code", "AX", "name", "ÅLAND ISLANDS"},
{"code", "AL", "name", "ALBANIA"},
{"code", "DZ", "name", "ALGERIA"},
{"code", "AS", "name", "AMERICAN SAMOA"},
{"code", "AD", "name", "ANDORRA"},
{"code", "AO", "name", "ANGOLA"},
{"code", "AI", "name", "ANGUILLA"},
{"code", "AQ", "name", "ANTARCTICA"},
@marshallswain
marshallswain / firemap.js
Created April 3, 2014 22:05
CanJS Firebase Map. Pass in a firebase snapshot and get a can.Map that syncs with Firebase when an attribute is updated.
'use strict';
var FireMap = can.Map.extend({
'setup':function(snapshot){
this.new = true;
// Get the data from the snapshot.
var data = snapshot.val();
// Save the fb ref with it.