Skip to content

Instantly share code, notes, and snippets.

View djanowski's full-sized avatar

Damian Janowski djanowski

View GitHub Profile
function useMatchMedia(query) {
const mediaQuery = useMemo(() => window.matchMedia(query), [ query ]);
const [ matches, setMatches ] = useState(mediaQuery.matches);
useEffect(() => {
function onChange({ matches: newMatches }) {
setMatches(newMatches);
}
if (mediaQuery.addEventListener)
@djanowski
djanowski / throttle_async.js
Created February 1, 2019 16:49
Throttle an async (promise-returning) function in JavaScript/Node.js
// Throttles an async function.
function throttleAsync(fn, wait) {
let lastRun = 0;
let currentRun = null;
async function throttled(...args) {
if (currentRun)
return currentRun;
const currentWait = lastRun + wait - Date.now();
@djanowski
djanowski / each_async_update.js
Created November 29, 2017 15:28
Concurrently transform a MongoDB cursor into a stream of bulk MongoDB updates.
// Concurrently transform a MongoDB cursor into a stream of bulk MongoDB updates.
//
// Usage:
//
// const users = db.collection('users');
// const usersToUpdate = users.find({ email: null });
//
// await eachAsyncUpdate(users, usersToUpdate, async function(user) {
// const userEmail = await findUserEmail(user);
//
@djanowski
djanowski / timezones.txt
Created November 29, 2017 14:49
Good-enough default IANA time zones by US state
AL America/Chicago
AK America/Anchorage
AS Pacific/Samoa
AZ America/Denver
AR America/Chicago
CA America/Los_Angeles
CO America/Denver
CT America/New_York
DE America/New_York
DC America/New_York
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
const Mongoose = require('mongoose');
Mongoose.Promise = Promise;
Mongoose.connect(`mongodb://localhost/test-${Date.now()}`);
const PersonSchema = new Mongoose.Schema({
name: {
first: { type: String },
last: { type: String }
}
#!/usr/bin/env bash
# `nvm use` the version specified in package.json.
source $(brew --prefix nvm)/nvm.sh
set -eo pipefail
die() {
echo "$@" >&2
@djanowski
djanowski / autotest
Last active February 1, 2016 20:17
ag -l + entr = autotest!
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# Run `make`, `npm test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#