Skip to content

Instantly share code, notes, and snippets.

View liamnewmarch's full-sized avatar
🤖
beep boop

Liam Newmarch liamnewmarch

🤖
beep boop
View GitHub Profile
@liamnewmarch
liamnewmarch / app.yaml
Last active February 24, 2024 13:10
App Engine config for static sites with 404
# This app.yaml file assumes your static site is built into a directory called dist.
# Make sure dist only contains files you want to be made public. At a minimum, dist
# should contain an index.html and a 404.html.
# This can be any standard environment https://cloud.google.com/appengine/docs/standard/
runtime: python312
handlers:
# Handle files
- url: /(.+)
@liamnewmarch
liamnewmarch / CancellablePromise.js
Created July 29, 2021 17:48
Promise subclass which uses AbortController to provide a cancel() method.
export class CancellablePromise extends Promise {
#abortController;
constructor(callback) {
const abortController = new AbortController();
super((resolve, reject) => {
callback(resolve, reject);
abortController.signal.addEventListener('abort', reject);
});
this.#abortController = abortController;
@liamnewmarch
liamnewmarch / $
Last active May 18, 2021 09:50
Copy and paste commands from the internet worry free*!
#!/bin/bash
printf 'Are you sure you want to run this command? [Y/n] '
read -r yn
case $yn in
[Yy]* | '' )
"$@"
;;
* )
exit 1
@liamnewmarch
liamnewmarch / html.js
Last active February 24, 2024 13:03
A (tiny!) tagged template literal function for HTML strings with support for async values
/**
* Merges multiple arrays using a zip strategy.
*
* @example ƒ(['a', 'b'], [1, 2, 3]) → ['a', 1, 'b', 2, 3]
* @param {...[]} iterables
* @yields {*}
*/
//
function* zipMerge(...iterables) {
const length = Math.max(...iterables.map(it => it.length));
@liamnewmarch
liamnewmarch / temp.sh
Created April 27, 2020 17:18
Small utility to start a shell in a temporary directory and delete it afterwards
#!/usr/bin/env bash
BLUE='\033[1;34m'
RESET='\033[0m'
WHITE='\033[1;37m'
echo -e "Creating temp dir..."
tempdir=$(mktemp -d)
pushd $tempdir &>/dev/null
echo -e "${BLUE}!${WHITE} Starting a new shell. Type ${BLUE}exit${WHITE} to return.${RESET}"
@liamnewmarch
liamnewmarch / watchMediaQuery.js
Created April 23, 2020 16:34
Get the initial state of a media query and watch for changes with one easy function.
function watchMediaQuery(media, truthy = true, falsy = false) {
const listeners = [];
const mediaQuery = matchMedia(`(${media}: ${truthy})`);
mediaQuery.addListener(() => {
for (const listener of listeners) listener();
});
return (fn) => {
const listener = () => fn(mediaQuery.matches ? truthy : falsy)
listeners.push(listener);
listener();
@liamnewmarch
liamnewmarch / finder-show-hide.md
Last active November 8, 2021 16:26
Show and hide files and folders in macOS Finder

Aliases

Copy the following lines into your terminal to register the hide and show aliases.

alias hide='xattr -w -x com.apple.FinderInfo "$(xattr -p -x com.apple.FinderInfo ~/Library)"'
alias show='xattr -d com.apple.FinderInfo 2> /dev/null'

Usage

@liamnewmarch
liamnewmarch / brew-upgrade.sh
Created February 3, 2020 11:46
Interactive script to run brew doctor, brew upgrade and brew cleanup on macOS
#!/bin/bash
BLUE='\033[1;34m'
GREEN='\033[1;32m'
PURPLE='\033[1;35m'
RED='\033[1;31m'
RESET='\033[0m'
WHITE='\033[1;37m'
YELLOW='\033[1;33m'
@liamnewmarch
liamnewmarch / overflow.js
Last active February 24, 2024 13:13
Quick script to find elements that are causing horizontal scroll because the overflow the viewport.
function test(which, value) {
if (value > 0) throw new Error(`${which}: ${value}px`)
}
for (const element of document.querySelectorAll('*')) {
const { left, right } = element.getBoundingClientRect()
try {
test('right', right - window.innerWidth)
test('left', 0 - left)
} catch ({ message }) {
@liamnewmarch
liamnewmarch / store.js
Created January 14, 2020 15:59
Simple event driven store based on Proxy
/**
* Usage example
*
* ```
* const myStore = new Store({
* count: 1,
* });
*
* myStore.addEventListener('change', () => {
* console.log(store.state.count);