Skip to content

Instantly share code, notes, and snippets.

@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
@drbr
drbr / KeyOn.ts
Last active December 16, 2020 01:39
React HOC to render a wrapped component with a given key
/**
* Higher Order Component that renders the wrapped component with the given key (derived from its
* props), which allows us to easily "reset" a component if certain props change.
* @param component
* @param getKey
*/
export function KeyOn<P>(component: React.ComponentType<P>, getKey: (props: P) => string) {
const displayName = `KeyOn(${component.displayName})`;
const hoc = (props: P) => {
const key = getKey(props);
@drbr
drbr / XStateViz.tsx
Created November 26, 2020 07:26
XState Visualizer (XState Inspector) as a React component with a standalone state machine
import { useEffect, useState } from 'react';
import { StateMachine } from 'xstate';
import { useMachine } from '@xstate/react';
import { inspect } from '@xstate/inspect';
let iFrameElement: HTMLIFrameElement | null = null;
function createIFrame() {
if (!iFrameElement) {
const iframe = document.createElement('iframe');