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 / angular-pub-sub.js
Last active August 29, 2015 14:04
AngularJS pub/sub example
angular.module('app')
.service('dataSvc', function($q) {
var dataStore = [];
// Slight abuse of promises to achieve pub/sub pattern
var updater = $q.defer();
function addData(data) {
// Add the data to the dataStore
dataStore.push(data);
// Tell all subscribers about the update, and
@huttj
huttj / to_dict.py
Last active August 29, 2015 14:08
to_dict: Turn variables into a dictionary
# Turn a list of variables into a dictionary, keyed on the variable names.
# e.g., apple = 2
# to_dict(apple) == {'apple': 2}
def to_dict(*args):
dict = {}
for key in globals().keys():
for var in args:
if (var is not None) and (eval(key) == var):
dict[key] = var
if len(args) == len(dict):
@huttj
huttj / index.html
Created December 26, 2014 04:25
Simple implementation of the 'heartbeat' pattern in NodeJS.
<!doctype html>
<html>
<head>
<title>Heartbeat test.</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<script>
// Let the user set the ID in the query variable
var id = parseInt((window.location.search.match(/id=(\d{2})/) || []) [1]);
@huttj
huttj / safe_object_access.js
Created December 31, 2014 18:49
Safe property access in JavaScript
// Takes the first value after the last true value
var reverseCoalesce1 = true && 555 && 123;
console.log(reverseCoalesce1); // 123
// If a value is null, it stops there
var reverseCoalesce2 = true && null && 123;
console.log(reverseCoalesce2); // null
// This is great for object access
var obj = {a: {b: {c: 2}}};
@huttj
huttj / lookSaySequence.js
Last active August 29, 2015 14:12
Turn a sequence of numbers into a sequence of words
var lookSaySequence = (function() {
var words = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
@huttj
huttj / lookSaySequenceZh.js
Last active August 29, 2015 14:12
Chinese version of lookSaySequence--for kicks.
var lookSaySequence = (function() {
var words = {
'0': '零',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
@huttj
huttj / parseTimestamp.js
Created January 13, 2015 09:26
A simple method to parse a PostgreSQL time stamp in Safari.
// Returns the timestamp as a Date object (from the host's timezone),
// in GMT. If timestamp is *not* GMT, pass in a truthy value for isLocal.
function parseTimestamp(timestamp, isLocal) {
var z = isLocal ? 0 : new Date().getTimezoneOffset() / 60;
var t = timestamp.split(/[\- :\.]/).map(Number);
return new Date(t[0], t[1]-1, t[2], t[3]-z, t[4], t[5], t[6], t[7]);
}
var input = "2015-01-13 08:52:05.1234";
console.log(parseTimestamp(input).toString()); // 'Tue Jan 13 2015 00:52:06 GMT-0800 (Pacific Standard Time)'
@huttj
huttj / typeEnforcement.js
Created February 20, 2015 22:16
Type enforcement in JavaScript
function User(data) {
this.name = is(String)(data.name);
}
function is(obj) {
return function(val) {
if (val === null || val.constructor !== obj) {
throw [
'Invalid type specified; expected:',
obj.name + ',',
@huttj
huttj / SocketSvc.js
Created March 1, 2015 22:07
Basic socket usage in Angular/Ionic
angular.module('app')
.service('Socket', function($rootScope) {
var socket = io.connect('http://www.foo.com:5000');
var S = {};
S.on = function(eventName, callback) {
socket.on(eventName, function () {
@huttj
huttj / indexOf.js
Last active August 29, 2015 14:17
indexOf
function indexOf(haystack, needle) {
// If either is undefined, quit
if (!haystack || !needle) return -1;
for (var i = 0; i < haystack.length; i++) {
// If there's not enough chars left in
// `haystack` for a match, quit
if (haystack.length - i < needle.length) return -1;