Skip to content

Instantly share code, notes, and snippets.

View davidmfoley's full-sized avatar

Dave Foley davidmfoley

View GitHub Profile
@davidmfoley
davidmfoley / dumb_merge.ts
Created December 18, 2021 00:11
Simplest typescript merge
const merge = (a: any, b: any): any => {
if ((typeof b !== "undefined" && typeof b !== "object") || Array.isArray(b))
return b;
if ((typeof a !== "undefined" && typeof a !== "object") || Array.isArray(a))
return a;
a ||= {};
b ||= {};
const keys = [...new Set([...Object.keys(a), ...Object.keys(b)])];
return keys.reduce(
@davidmfoley
davidmfoley / useTodos.js
Last active November 9, 2021 17:03
Example of using react context for dependency inversion
import { createTodo, updateTodo, deleteTodo } from './real-todo-api-client'
import React from 'react'
// create a context where the default value is the "real" production implementation
export const TodoApi = useContext({
getTodos,
createTodo,
updateTodo,
deleteTodo
})
@davidmfoley
davidmfoley / convert_all_to_mp3
Created June 10, 2020 14:37
convert all wavs to mp3
for file in *.wav ; do ffmpeg -i "$file" -b:a 320k "${file%.*}.mp3" ; done
/* Conway's Life in pl/pgsql */
/* The 8 directions for finding neighbors of a cell */
create temp table directions(dx int, dy int);
insert into directions(dx, dy) values(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1);
/* alive cells, one row per */
create temp table cells(x int, y int);
/* setup seed cells */
@davidmfoley
davidmfoley / with_query.js
Created December 6, 2018 17:18
easy access to query params with react-router v4
// @flow
import * as React from 'react';
import queryString from 'query-string';
import { withRouter } from 'react-router-dom';
export default function withQuery(WrappedComponent: any): any {
function QueryWrapper (props: Props) {
const query = queryString.parse(props.location.search) || {};
const pushQuery = (query) => {
@davidmfoley
davidmfoley / rps.js
Created October 16, 2018 22:45
RPS in JS
const precedence = ['rock', 'scissors', 'paper'];
const results=['TIE', 'FIRST_PLAYER', 'SECOND_PLAYER'];
const ordinal = x => precedence.indexOf(x);
const circularDirection = (a, b) => (ordinal(b) - ordinal(a) + 3) % 3;
const rps = (a, b) => results[circularDirection(a, b)];
DB_URLS = ( \
postgres://localhost/test_1 \
postgres://localhost/test_2 \
)
for db_url in "${DB_URLS[@]}"; do
echo "creating database `basename $db_url`"
createdb `basename $db_url` > /dev/null 2>&1
psql $db_url -c 'drop schema public cascade; create schema public;' -o /dev/null 2>/dev/null
psql $db_url -f "path/to/your/schema.sql" > /dev/null 2>/dev/null
@davidmfoley
davidmfoley / fetch-data.sh
Created May 4, 2018 18:26
Fetch data from remote WP mysql instance to local, companion of https://gist.github.com/davidmfoley/74586660d7cb70165b8196b54973543d
mysqldump \
-u $REMOTE_USER \
-p$REMOTE_PASS \
-h $REMOTE_HOST \
-P $REMOTE_PORT $REMOTE_DB > ./test-dump
mysql -h host.docker.internal -P 3306 --password=wordpress -u wordpress wordpress < ./test-dump
# Make local-dev-specific modifications
mysql -h host.docker.internal -P 3306 --password=wordpress -u wordpress wordpress \
-e "update wp_options set option_value='http://localhost:8080' where option_name in ('siteurl', 'home');"
@davidmfoley
davidmfoley / docker-compose.yml
Last active May 4, 2018 18:46
Sample wordpress docker compose setup for local development
version: '3'
services:
db:
image: mysql:5.7
volumes:
- site_db_data:/var/lib/mysql
restart: always
ports:
# bind to host port so we can run mysql tools on host
@davidmfoley
davidmfoley / flow-stderr-with-exit-code.sh
Created December 22, 2017 18:44
Run flow (flowtype) check, errors sent to stderr, exit non-zero on failure
#! /bin/bash
# flow check prints all of its output to stdout.
# In CI, we want to print only failures to stderr to make them easier to find in the build log.
# ignore flow diag messages, get rid of 'success' message', then redirect to stderr
# grep -v exits non-zero on match match, so we flip the exit code
! flow check 2>/dev/null | grep -v 'Found 0 errors' 1>&2