Skip to content

Instantly share code, notes, and snippets.

View davidmfoley's full-sized avatar

Dave Foley davidmfoley

View GitHub Profile
const { expect } = require('chai');
//
// Conway's Life, in modern javascript
//
// - Built with node 8, mocha, chai; uses modern javascript features
// - All data immutable
// - No classes or other pap.
//
// github.com/davidmfoley
@davidmfoley
davidmfoley / gist:001914108513389032f5635e694fa27a
Created November 16, 2017 01:06
Seattle mayoral election district summary
# Note: based on elecion night data, from:
# http://www.kingcounty.gov/~/media/depts/elections/results/2017/201711/20171107-ecanvass-election-night.csv
# http://kingcounty.gov/depts/elections/results/2017/201711/reports.aspx
$ dists=(11 32 34 36 37 43 46); for dist in "${dists[@]}"; do echo "District $dist" && grep -E '(Seattle Mayor)' 20171107-ecanvass-election-night.csv | grep -E "^SEA $dist" | awk -F',' '{if ($8=="Jenny Durkan") { jd+=$9 } else if ($8=="Cary Moon") { cm+=$9 } else if ($8=="Registered Voters") { rv+=$9 } } END { print "Durkan " jd ", Moon " cm ", Durkan % " jd * 100 / (jd + cm) ", Moon % " cm * 100 / (jd + cm) ", Turnout % " (jd + cm) * 100 / rv }'; done
District 11
Durkan 741, Moon 846, Durkan % 46.6919, Moon % 53.3081, Turnout % 17.2033
District 32
Durkan 1766, Moon 1011, Durkan % 63.5938, Moon % 36.4062, Turnout % 24.7108
District 34
@davidmfoley
davidmfoley / keyboard_nav.js
Last active June 9, 2017 23:22
KeyboardNav react component example
// @flow
import React from 'react';
const { Component } = React;
let stack = [];
function addToStack(handler: Function) {
if (stack.length === 0) {
window.addEventListener('keydown', globalKeyHandler);
@davidmfoley
davidmfoley / middleware_example.js
Created December 15, 2016 18:01
Example express error handler middleware snippet
res.apiRespond = function(err, data) {
if (err) {
var code = (err.httpStatusCode || 500);
if (code === 500) {
logger.error('API error: ' + req.method + ' ' + req.originalUrl, err);
}
res.status(code).send(formatter.format(err));
} else if (!data && req.method === 'GET') {
res.notFound();
} else {
var util = require('util');
module.exports = {
RetryableError: error('RetryableError', 'retry_later', 400, "Retry this request later"),
UnauthorizedError: error('UnauthorizedError', 'unauthorized', 401, "Unauthorized"),
NotFoundError: error('NotFoundError', 'not_found', 404, "The requested record was not found"),
UniqueConstraintError: error('UniqueConstraintError','unique_constraint', 409, "Uniqueness constraint violated"),
InvalidDataError: error('InvalidDataError','invalid', 422, "Invalid data"),
InternalError: error('InternalError','internal', 500, "Internal error")
};
@davidmfoley
davidmfoley / snapshot.js
Created October 1, 2016 17:05
take snapshot of local html page w node-phantom-simple
'use strict';
var driver = require('node-phantom-simple');
var phantomPath = require('phantomjs-prebuilt').path;
// YMMV
const snapshotTimeout = 15 * 1000;
function capture(indexPath, size, snapshotPath, cb) {
driver.create({ path: phantomPath }, function (err, browser) {
def digit(number):
"""Add digits as long as there is more than one digit
>>> digit(2)
2
>>> digit(12)
4
>>> digit(99)
9
>>> digit(83928)
.
├── actions
├── stores
├── views
│   ├── Anonymous
│   │   ├── __tests__
│   │   ├── views
│   │   │   ├── Home
│   │   │   │   ├── __tests__
│   │   │   │   └── Handler.js

Keybase proof

I hereby claim:

  • I am davidmfoley on github.
  • I am davidmfoley (https://keybase.io/davidmfoley) on keybase.
  • I have a public key whose fingerprint is B17C 1D1D 0593 A657 73CE DAB1 AE9F C807 C2C1 B700

To claim this, I am signing this object:

@davidmfoley
davidmfoley / timestamps.js
Created January 29, 2015 16:17
Very kludgy PG timestamp to JS date
module.exports = {
// turns a postgres timestamptz field into a JS date
parse: function(s) {
//split on second fraction
var pieces = s.split('.');
// replace space with ISO T separator
pieces[0] = pieces[0].replace(' ', 'T');
// strip to milliseconds
pieces[1] = pieces[1].slice(0, 3) + pieces[1].slice(6);
// add minutes to TZ offset - doesn't handle non-hour TZs