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 / 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 / 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 / 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 / format-relative.js
Last active November 3, 2023 15:29
Relative time strings using the web platform
/**
* The target language. [Browser support is good][1] but "en-US" is a safe default.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language
*
* @type {string}
*/
const { language = "en-US" } = navigator;
/**
@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 / 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 / git-fix-author
Last active August 13, 2021 14:04
Correct git author name and email for a branch by rewriting its history.
#!/bin/bash
# shellcheck disable=SC2016
# Rewrite git history.
readonly VERSION='2.0.5'
# Exit immediately if a command exits with a non-zero status.
set -e
@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 / dom-parse.js
Last active July 1, 2021 16:23
Utility function parse a string as HTML, SVG or XML.
/**
* Utility function parse a string as HTML, SVG or XML.
*
* Example usage:
* parse('<a href="https://www.google.com">')[0].click();
*
* @param {string} string The string to parse [1]
* @param {type='text/html'} string The mime type to parse the string as [2]
* @returns {HTMLCollection} A HTML collection of the parsed elements
*
@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