Skip to content

Instantly share code, notes, and snippets.

View rjz's full-sized avatar
👋
Hey there!

RJ Zaworski rjz

👋
Hey there!
View GitHub Profile
@rjz
rjz / timed.js
Created March 3, 2017 17:19
JS promise execution time
const log = console.log;
function timed(title, promiseFactory) {
return (...args) => {
const t0 = Date.now();
log(title, 'start');
return promiseFactory(...args)
.then((results) => {
const dt = Date.now() - t0;
log(title, 'end', `(${dt} ms)`);
@rjz
rjz / flaky-api.ts
Created February 2, 2017 16:35
Simulate flaky API
// Extracted from https://github.com/rjz/typescript-react-redux
// Simulate a flaky API around otherwise an otherwise synchronous `f()`.
const flakify = <T>(f: () => T): Promise<T> =>
new Promise((resolve, reject) =>
// We'll always take 200 * (1d10 + 1) ms to respond
window.setTimeout(() => {
try {
// And ~20% of the time we'll fail, because flaky.
@rjz
rjz / find-unused.sh
Created January 11, 2017 16:37
Find unused package.json dependencies
#!/bin/bash
# Check local javascript files for references to dependencies appearing in
# `package.json`.
#
# `require`-only: **does not** account for ESNext `import` syntax (yet!)
js_files=$(find . -path ./node_modules -prune -o -name '*.js' -type f \
| grep -v node_modules)
@rjz
rjz / update_typings.sh
Last active January 6, 2017 21:43
Updating TypeScript typings to latest versions
#!/bin/bash
# Update TypeScript `typings` to their latest versions.
#
# Running this ad-hoc may break your project; be prepared to resolve issues by
# hand.
#
# Released under the terms of the MIT License
set -e
@rjz
rjz / pre-commit-lint
Created November 14, 2016 17:45
pre-commit hook to lint JS/X
#!/bin/bash
# pre-commit hook to lint JS/X
#
# Assumes that `eslint` is installed via npm as usual.
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
@rjz
rjz / bootstrap-dynamodb.sh
Created November 8, 2016 18:32
Bootstraps setup of dynamodb_local
#!/bin/bash
# Bootstraps setup of dynamodb_local
# Set download path if needed
DOWNLOAD_PATH=http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUNDLE_FILE="${DIR}/dynamodb_local_latest.tar.gz"
INSTALL_DIR="${DIR}/.dynamo"
@rjz
rjz / projects_spec.ts
Created September 30, 2016 19:22
Testing with fetch, fetchmock, and TypeScript
// Example of mocking out global `fetch`
//
// Spec works using Jasmine (via Karma) and _either_ tsc or babel.
//
// $ npm install --save-dev fetch-mock whatwg-fetch
//
// If using `tsc`, grab the type definitions as well:
//
// $ typings --save --global dt~fetch-mock dt~whatwg-fetch
@rjz
rjz / redux-actions-with-typescript-discriminated-unions.ts
Created September 29, 2016 16:00
Redux actions using TypeScript discriminated unions
// Redux actions using TypeScript discriminated unions
//
// Source: https://github.com/rjz/typescript-react-redux/
// Actions
export type Action =
{ type: 'INCREMENT_COUNTER', delta: number }
| { type: 'RESET_COUNTER' }
@rjz
rjz / ngrok_hostname.sh
Created August 9, 2016 16:20
Get ngrok hostname from command line
#!/bin/sh
# ngrok's web interface is HTML, but configuration is bootstrapped as a JSON
# string. We can hack out the forwarded hostname by extracting the next
# `*.ngrok.io` string from the JSON
#
# Brittle as all get out--YMMV. If you're still reading, usage is:
#
# $ ./ngrok_hostname.sh <proto> <addr>
#
@rjz
rjz / has_content_type.go
Last active October 31, 2023 06:55
Validate golang http.Request content-type
import (
"mime"
"net/http"
"strings"
)
// Determine whether the request `content-type` includes a
// server-acceptable mime-type
//
// Failure should yield an HTTP 415 (`http.StatusUnsupportedMediaType`)