Skip to content

Instantly share code, notes, and snippets.

@danallison
danallison / useWorkerToDo.coffee
Last active August 29, 2015 14:01
Create a Web Worker on the fly
useWorkerToDo = (fn, data) ->
window.URL or = window.webkitURL
workerSupported = window.Worker and window.URL and window.Blob
callbacks = []
done = false
returnVal = undefined
resolve = ->
while callbacks.length
returnVal = callbacks.shift()(returnVal)
return
@danallison
danallison / Trie.coffee
Created November 17, 2013 20:05
Trie data structure in CoffeeScript
class Trie
constructor: (strings = [], @root = {}) ->
@addString string for string in strings
addString: (string) ->
chars = string.split ""
string = ""
node = @root
for char in chars
string += char
@danallison
danallison / paginationTemplate.coffee
Created May 23, 2013 19:43
A template function for twitter bootstrap pagination.
paginationTemplate = (data) ->
url = data.url
currentPage = +data.currentPage
totalPages = +data.totalPages
lis = []
if totalPages < 7
for i in [1..totalPages]
li = if i is currentPage then "<li class='active'><a>#{i}</a></li>" else "<li><a href='#{url}/page#{i}'>#{i}</a></li>"
lis.push(li)
@danallison
danallison / gist:5606496
Last active December 17, 2015 11:59
Number of years, months, and days between today and some date in the past. Avoids problems associated with leap years and inconsistent total days in a given month.
var yearsMonthsDaysBetween = function(date) {
var andString, days, daysString, months, monthsString, s, today, years, yearsString;
today = new Date();
date = new Date(date);
years = today.getFullYear() - date.getFullYear();
months = today.getMonth() - date.getMonth();
days = today.getDate() - date.getDate();
if (days < 0) {
months -= 1;
days = Math.floor((today - new Date(today.getFullYear(), today.getMonth() - 1, date.getDate())) / 1000 / 60 / 60 / 24);