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
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 |
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
# 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): |
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
<!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]); |
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
// 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}}}; |
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 lookSaySequence = (function() { | |
var words = { | |
'0': 'zero', | |
'1': 'one', | |
'2': 'two', | |
'3': 'three', | |
'4': 'four', | |
'5': 'five', | |
'6': 'six', |
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 lookSaySequence = (function() { | |
var words = { | |
'0': '零', | |
'1': '一', | |
'2': '二', | |
'3': '三', | |
'4': '四', | |
'5': '五', | |
'6': '六', |
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 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)' |
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
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 + ',', |
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
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 () { |
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
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; | |
OlderNewer