Skip to content

Instantly share code, notes, and snippets.

View drbr's full-sized avatar

Andrew Brandon drbr

View GitHub Profile
@drbr
drbr / svgFromFont
Created August 15, 2024 00:11
Render text in a font as SVG
#!/usr/bin/env node
const { program } = require('commander');
const opentype = require('opentype.js');
const fs = require('fs');
program
.name('svgFromFont')
.usage("'text' [options]")
.description(
'Renders the given text using a specified font and outputs it as an SVG, which can be used as an image on the web.'
@drbr
drbr / useScopedSelectionCallback.ts
Last active August 4, 2024 00:29
useScopedSelectionCallback
import { useEffect } from 'react';
/**
* Using the browser's Selection API, sets up a listener to detect when the user selects text that
* resides completely within a specified element.
*
* @param containerRef The element to detect selections within.
* @param callback The function to call when the selection changes. It may be called additional
* times, particularly with `null`. If the selected text resides only partially within the container
* element, the callback will be called with `null`.
@drbr
drbr / spellingBee.sh
Created November 27, 2023 05:44
Bash script to solve the NYT Spelling Bee
#!/bin/bash
dictPath="/usr/share/dict/words"
middleLetter=$1
allLetters=$2
# The following argument validations must be true in order for the script to be correct
[[ -n "$middleLetter" && -n "$allLetters " ]] || { echo "Usage: ./spellingBee.sh <middle letter> <all letters>"; exit 0; }
[[ "$middleLetter" =~ ^[a-zA-z]$ ]] || { echo "Usage: Middle letter must be a single letter"; exit 0; }
@drbr
drbr / printFocusedElement.js
Last active February 8, 2023 01:18
Bookmarklet to console log the active element on a timer
javascript: (() => {
/*
* This script is meant to be run as a JavaScript bookmarklet. "Install" it by pasting the script
* into the URL field of a bookmark in your browser.
* https://www.freecodecamp.org/news/what-are-bookmarklets/
*
* When invoked the first time, it will set a timer that prints the currently focused element to the
* console every 2 seconds. When invoked again, it will cancel that timer, and will continue to
* toggle the timer on each subsequent invocation.
*
@drbr
drbr / wordle.sh
Last active November 27, 2023 05:48
Simple shell script to help solve Wordle. It will tell tell you what the answer could be based on the yellow/green/black squares you have discovered so far.
#!/bin/zsh
# Get all the 5 letter words. This only needs to be generated once,
# so do it outside the script and leave the file on disk.
# grep '^.....$' /usr/share/dict/words > 5LetterWords.txt
# All the known green letters as a regex, e.g. `greens 'AB.C.'`
greens() {
grep -i $1
}
@drbr
drbr / robotDraw.css
Created January 20, 2022 02:40
Simple library to draw a robot on a grid using JavaScript/HTML/CSS
body {
font-family: sans-serif;
}
.grid {
display: inline-flex;
flex-direction: column;
border-right: 3px solid black;
border-bottom: 3px solid black;
background-color: beige;
@drbr
drbr / reactMemoDemo.test.tsx
Last active November 17, 2021 01:50
Unit tests that demonstrate how `useMemo` in a parent is equivalent to `React.memo` on a child component
import { cleanup, render, screen } from '@testing-library/react';
import * as React from 'react';
type MemoOption = 'none' | 'hook' | 'HOC';
function ParentComponent({ log, memo }: { log: (msg: string) => void; memo: MemoOption }) {
log('parent');
// This value is not sent into the child, it's just used to rerender the parent.
const [value, setValue] = React.useState(0);
@drbr
drbr / groupAnagrams.ts
Created March 17, 2021 22:51
My solution to a leetcode problem
// Originally from https://leetcode.com/problems/group-anagrams/
/* Prompt */
// #------------------#
// # Coding question: #
// # Group anagrams #
// #------------------#
//
// Given an array of strings, group the strings that are anagrams of each other.
@drbr
drbr / coursePrerequisites.ts
Last active March 17, 2021 22:45
My solution to a Leetcode problem
// Taken from https://leetcode.com/problems/course-schedule/
/* Problem statement */
// There are a total of numCourses courses you have to take, labeled from 0 to
// numCourses - 1. You are given an array prerequisites where prerequisites[i] =
// [a_i, b_i] indicates that you must take course b_i first if you want to take
// course a_i.
//
// For example, the pair [0, 1], indicates that to take course 0 you have to
@drbr
drbr / useAsyncFetch.ts
Last active December 19, 2020 03:35
Hook to fetch async data, a cheap knockoff of react-query
import * as React from 'react';
export type AsyncFetchState<T, E = unknown> = {
data: T | null;
loading: boolean;
error: E | undefined;
};
/**
* This hook performs an async operation and stores its result in component state. It protects