Skip to content

Instantly share code, notes, and snippets.

Paul "Joey" Clark joeytwiddle

Block or report user

Report or block joeytwiddle

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View get_url_scheme_for_ios_app.sh
#!/usr/bin/env bash
set -e
cache_dir="$HOME/Library/Group Containers/K36BKF7T3D.group.com.apple.configurator/Library/Caches/Assets/TemporaryItems/MobileApps"
if command -v find >/dev/null && command -v mktemp >/dev/null && command -v unzip >/dev/null && command -v grep >/dev/null
then :
else
echo "This script requires the follow executables to run: find mktemp unzip grep"
@joeytwiddle
joeytwiddle / asyncWrappers.js
Last active Jul 1, 2019
Helper to use promises in restify (might also work for express)
View asyncWrappers.js
/**
* Use restify with promises
*
* Restify middleware and route handlers are passed a callback function `next`
* which must be called on success, or on an error.
*
* But you may prefer to define your middleware and routes using promises or
* async functions instead of using callbacks.
*
* To achieve that, simply wrap your middleware functions in `middleware()` and
View cooldown-monitor.js
"use strict";
/*
Simulates an object which can heat up and overheat, but with a constant rate of cooling over time.
Useful for throttling when you want to provide an initial allowance.
For example, it could be used to manage the overheating of a weapon in a game, or to avoid errors from generating huge logfiles, but without throttling the first few errors.
@joeytwiddle
joeytwiddle / move_dropbox_to_ext4_fs.sh
Created Nov 6, 2018
Creates a file with an ext4 filesystem inside it, and moves Dropbox into that filesystem
View move_dropbox_to_ext4_fs.sh
#!/bin/bash
set -e
set -x
# You could put the fs file and the mountpoint inside your home folder
#new_fs_file="$HOME/dropbox_partition.ext4fs"
#mountpoint="$HOME/Dropbox.mnt"
# But that may not work if you are using a fuse-mounted homefolder (e.g. encrypted with encfs)
new_fs_file="/home/dropbox_for_$USER.ext4fs"
mountpoint="/mnt/dropbox.$USER"
@joeytwiddle
joeytwiddle / optional.js
Last active May 2, 2019
Getting properties from an object which might be null
View optional.js
// A common situation in Javascript:
//
// We have a variable `obj`, and we want to get `obj.foo.bar`
//
// But `obj` might be null or undefined, or `obj.foo` might be null or undefined
//
// So how can we try to get `obj.foo.bar` without risking an error?
// Here are a few different ways to do that. Which one is clearest?
@joeytwiddle
joeytwiddle / Basic_React_Gotchas.md
Last active Feb 26, 2019
Some things we learned in the first few months of using React
View Basic_React_Gotchas.md

Basic React Gotchas

Note: This document was written for someone who already knows React fairly well, because it was initially a list of suggested talking points for a presentation they were planning. Ideally this document should be expanded to include clear examples for the final target audience, which is React beginners.

setState does not apply the update immediately

// Constructor
this.state = { foo: 0 };
@joeytwiddle
joeytwiddle / objUtils.js
Created Jun 21, 2018
Some Array functions for Objects
View objUtils.js
// The functional way
const objUtils = {
mapProps(obj, mapFn) {
return Object.entries(obj).reduce((a, [key, value]) => (
{ ...a, [key]: mapFn(value) }
), {});
},
filterProps(obj, filterFn) {
return Object.entries(obj).reduce((a, [key, value]) => (
@joeytwiddle
joeytwiddle / async-await-forEach-alternatives.md
Last active Sep 30, 2018
Do not use forEach with async-await
View async-await-forEach-alternatives.md

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

@joeytwiddle
joeytwiddle / converting_js_types.md
Last active Jun 23, 2018
Converting between types in Javascript
View converting_js_types.md

When you have a variable of one type but you want to convert it to another type, there are a few options available to you.

Here are some of the most popular methods:

+=================+===================+=========================+
|                 |                   |                         |
|    Goal         |    Short form *   |    Long form            |
|                 |                   |                         |
@joeytwiddle
joeytwiddle / Use_throw_instead_of_reject.md
Last active Jun 9, 2018
Using throw instead of Promise.reject()
View Use_throw_instead_of_reject.md

I recommend using throw over return Promise.reject() because:

  1. It is shorter, which makes it easier to read.
  2. It retains some parity between synchronous and asynchronous code. (It will continue to work as expected if those lines were extracted into a synchronous function.)
  3. It can be used to break out of a stack of deeply nested synchronous function calls. (Returning a rejection only jumps up one level.)

Although throwing an error is not safe inside callback functions (which are often called from the engine, without a surrounding try-catch wrapper), it is entirely safe to throw from inside an async function (or from within a .then() handler function), because in those cases the throw will be automatically converted into a rejected promise.

Situations where it is safe to use throw

You can’t perform that action at this time.