Skip to content

Instantly share code, notes, and snippets.

@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);
@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 / 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 / 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 / dispatcher.coffee
Last active August 29, 2015 14:05
Bare bones event system
dispatcher = {}
dispatcher._subscribers = {}
dispatcher.on = (eventName, callback, context) ->
(dispatcher._subscribers[eventName] or = [])
.push {
callback: callback
context: context
}
@danallison
danallison / downloadString.js
Created September 29, 2014 16:44
download string as text file
function downloadString(text, fileType, fileName) {
var blob = new Blob([text], { type: fileType });
var a = document.createElement('a');
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
a.style.display = "none";
document.body.appendChild(a);
a.click();
@danallison
danallison / flowMap.js
Created January 7, 2016 23:09
Like _.flow, but also accepts arrays of functions.
function flowMap () {
var functions = [].slice.call(arguments); // convert arguments to array
return function () {
var _this = this; // maintain context
return functions.reduce(function (args, fn) {
// for each function or array of functions,
// pass in the previous return value.
if (_.isArray(fn)) {
return [fn.map(function (_fn) { return _fn.apply(_this, args); })];
} else {

Keybase proof

I hereby claim:

  • I am danallison on github.
  • I am danallison (https://keybase.io/danallison) on keybase.
  • I have a public key ASDUznCpicoQz-TtNd0WOBIMNXMAYktgZzXbIRT3TjIJlAo

To claim this, I am signing this object:

@danallison
danallison / 10kft_api_example.get_users_currently_assigned_to_a_project.ipynb
Last active July 21, 2017 19:07
10kft API Example. Get users currently assigned to a project
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import requests
import datetime
auth_token = '** replace this with your auth token **'
def get_time_entries_for_project(project_id, date_range=None):
'''
Fetches time entries from the API, including suggestions and phases
'''
base_url = 'https://api.10000ft.com'