Skip to content

Instantly share code, notes, and snippets.

View huttj's full-sized avatar

Joshua Hutt huttj

  • Seattle, WA
View GitHub Profile
@huttj
huttj / dateStringParser.js
Last active August 29, 2015 14:21
Date string parser
function toDate(str) {
// To map months to their numeric equivalent
var months = {
January : 0,
Feburary : 1,
March : 2,
April : 3,
May : 4,
June : 5,
@huttj
huttj / flatten.js
Created May 13, 2015 22:13
Flatten nested arrays
function flatten(arry) {
var result = [];
for (var i = 0; i < arry.length; i++) {
if (typeof arry[i] === 'number') {
result.push(arry[i]);
} else {
result = result.concat(flatten(arry[i]));
}
}
return result;
@huttj
huttj / log.js
Created May 24, 2015 21:38
Automatic line number logging in JavaScript
function log() {
var stack;
try {
throw new Error();
} catch (e) {
stack = e.stack.split('\n')[2].match(/\s+at\s+(.+)/)[1] + ':\n\t';
}
var args = Array.prototype.slice.call(arguments);
args.unshift(stack);
console.log.apply(console, args);
@huttj
huttj / log.js
Last active August 29, 2015 14:22
Log Module
var cwd = process.cwd();
var reg = new RegExp(/([^()]+)/);
var colors = {
black : '0',
red : '1',
green : '2',
yellow : '3',
blue : '4',
magenta : '5',
cyan : '6',
@huttj
huttj / selfAwarePromise.js
Created June 16, 2015 05:21
A promise that catches itself if no one else does.
var $q = require('q');
function catchIfNeeded(name) {
var caught = false;
var promise = $q.try(function() {
throw new Error('Catch me!');
}).catch(function(err) {
// This function is executed on the next "tick" of the event loop
if (caught) {
throw err;
@huttj
huttj / PubSub.js
Created June 16, 2015 19:50
Publish-subscribe implementation with special features.
var PubSub = (function PubSub() {
var subscriptions = {};
var publications = {};
var index = 0;
function sub(event, callback) {
// If already subbed, return the id
var subId = getSubId(callback);
{
"segredo": {
"key": "1234567890",
"secret": "asdfghjkl"
},
"port": 3000,
"db": {
"path": "db.connection.string"
}
}
@huttj
huttj / db_prep.js
Last active August 29, 2015 14:24
RethinkDB union performance example
r.dbCreate('test');
r.db('test').tableCreate('one');
r.db('test').tableCreate('two');
r.db('test').tableCreate('three');
r.range(300).forEach(function(i) {
return r.db('test').table('one').insert({ uid: 'a1' })
});
r.range(300).forEach(function(i) {
@huttj
huttj / index.html
Created October 10, 2015 00:48
Redis pubsub
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Redis pubsub</title>
</head>
<body>
<button id="add">Add 1</button>
<p><span id="count">?</span> click(s)</p>
@huttj
huttj / index.html
Last active October 11, 2015 04:36
Quick and dirty CSV upload and parse to JSON
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="upload-file">
<input type="submit" value="Upload">