Skip to content

Instantly share code, notes, and snippets.

View stephenjwatkins's full-sized avatar

Stephen Watkins stephenjwatkins

View GitHub Profile
@stephenjwatkins
stephenjwatkins / browser.js
Created June 12, 2018 19:58
Simple S3 file uploads with Koa and FormData
// Select the file input and add an onchange handler
const fileInput = document.querySelector("input[type='file']");
imageUpload.onchange = async e => {
const files = e.target.files;
const fileToUpload = files[0];
let data = new FormData();
data.append("file", fileToUpload);
// Send as multipart/form-data
@stephenjwatkins
stephenjwatkins / 00-JsonApiStore.js
Last active August 8, 2018 19:55
React store for handling API state.
import React from "react";
import { StoreHandler } from "../StoreHandler";
import { SubscriberHandler } from "../SubscriberHandler";
import { GiveDotChurchApi } from "../GiveDotChurchApi";
const createJsonApiStore = () => {
const ApiStoreContext = React.createContext({
change: 0,
data: () => {},
methods: () => {},
@stephenjwatkins
stephenjwatkins / giving.test.js
Last active August 8, 2018 19:44
Puppeteer helpers for more powerful DOM querying.
const { loadPath } = require("./loadPath");
const { setupPage } = require("./setupPage");
const {
waitForText,
getByText,
getComputedStyles,
} = require("./helpers");
test("successfully login as org admin", async () => {
const page = await setupPage(global.browser);
@stephenjwatkins
stephenjwatkins / DottedUnderline.js
Last active August 8, 2018 19:59
Wrap text with an aesthetically-pleasing dotted underline.
import React, { Component } from "react";
import throttle from "lodash/throttle";
class DottedUnderline extends Component {
constructor(...args) {
super(...args);
this.state = { width: null };
this.attach = this.attach.bind(this);
@stephenjwatkins
stephenjwatkins / shallowEqual.js
Created August 8, 2018 19:58
Shallow-equal using lodash.
import isEqualWith from "lodash/isEqualWith";
const shallowEqual = (value, other) =>
isEqualWith(value, other, (v, o) => {
for (let key1 in v) {
if (!(key1 in o) || v[key1] !== o[key1]) {
return false;
}
}
for (let key2 in o) {
@stephenjwatkins
stephenjwatkins / deepEqual.js
Created August 8, 2018 20:00
Deep-equal using lodash.
import isEqual from "lodash/isEqual";
const deepEqual = (a, b) => isEqual(a, b);
exports { deepEqual };
@stephenjwatkins
stephenjwatkins / loadScript.js
Created August 8, 2018 20:03
Load a script programmatically.
// Programmatically loads external scripts:
//
// await loadScript(
// window.document,
// "https://unpkg.com/react@16/umd/react.development.js",
// { onlyLoadOnce: true }
// );
const defaultOptions = {
timeout: null,
@stephenjwatkins
stephenjwatkins / blend.js
Created August 8, 2018 20:08
Color utility functions for working with blend modes.
// Color blending modes:
//
// import { blend } from "blend";
//
// const blended = blend.overlay("#aaa", "#f00");
//
// blended === "rgba(187, 8, 8, 1.0)";
const matchesToRgba = ([a = 1.0, r, g, b], base = 10) => {
return {
@stephenjwatkins
stephenjwatkins / mediaQueryWatcher.js
Created August 8, 2018 20:12
Simple event-based media query watcher.
// Watch media queries:
//
// createMediaQueryWatcher({ big: "(min-width: 400px)" }, {
// window,
// onUpdate: (matches) => {
// if (matches.big) {
// console.log("Big matches");
// }
// }
// });
@stephenjwatkins
stephenjwatkins / searchParams.js
Last active August 8, 2018 20:14
Utilities for working with search params (without needing to polyfill URLSearchParams).
function getAllSearchParamsFromSearch(search) {
let massagedSearch = search;
if (massagedSearch.charAt(0) === "?") {
massagedSearch = massagedSearch.substring(1);
}
// Filter out the search parameters that have been passed in
return massagedSearch.split("&").map(part => {
return part.split("=");