Skip to content

Instantly share code, notes, and snippets.

View blakek's full-sized avatar
✝️

Blake Knight blakek

✝️
View GitHub Profile
@blakek
blakek / lru-cache.js
Last active January 29, 2025 18:13
An LRU Cache for Zapier… Add to a Code step and fill in `keyToCheck` and `storageId`. I basically use this as an imperfect-but-workable solution for skipping duplicates.
/**
* @typedef {Object} InputData
* @property {string} keyToCheck
* @property {string} storageId
*
* @typedef {string | number | boolean | null | undefined | Record<string, JSONValue> | JSONValue[]} JSONValue
*
* @typedef {Object} StoreClient
* @property {() => Promise<void>} clear
* @property {(key: string) => Promise<void>} delete
@blakek
blakek / css.ts
Created January 9, 2025 17:31
Simple CSS-in-JS helpers (e.g. a super short `transparentize`, `cover`, etc.)
import type { CSSObject } from "@emotion/react";
/**
* Clamp a number between a minimum and maximum value.
*/
export const clamp = (value: number, min: number, max: number) =>
Math.min(Math.max(value, min), max);
/**
* Fully cover an element. Can optionally add an offset to the edges.
@blakek
blakek / simple-cache.ts
Last active February 11, 2025 15:15
A very simple cache that stores a value and its expiration time in a file
// Uses Bun, but this could quite easily use any storage (Node.js fs, in-browser `localStorage`, etc.)
import * as Bun from "bun";
import * as fs from "node:fs/promises";
export interface Expirable<T> {
value: T;
expires: number | null;
}
export interface CacheOptions {
@blakek
blakek / index.ts
Created November 25, 2024 16:47
POC for getting burn bans in Mississippi
import { inspect } from "bun";
import { SimpleCache } from "./simple-cache";
interface BurnBanData {
counties: string;
issued: string;
expires: string;
exemptions: string;
}
@blakek
blakek / helpers.js
Created September 24, 2024 20:08
Temporary UserScript helpers until I migrate to something more permanent
/**
* Prevents an attribute from being changed on an element, meaning once the
* attribute is changed, it will be immediately set back to the original value.
* @param {string} selector
* @param {string} attributeName
*/
function preventAttributeChange(selector, attributeName) {
const element = document.querySelector(selector);
const originalValue = element.getAttribute(attributeName);
@blakek
blakek / combine-audio
Last active May 14, 2024 13:08
Audio file tools
#!/usr/bin/env bash
set -eo pipefail
directory="$1"
cd "$directory"
files=()
# Get `wav` files, ignoring `other.wav`
for file in *.wav; do
if [[ $file != "other.wav" ]]; then
@blakek
blakek / fix-junk.js
Created October 28, 2023 00:41
Fix Node.js 17+ "digital envelope routines::initialization error"
const crypto = require("crypto");
// HACK: Fix Node.js 17+ "digital envelope routines::initialization error"
// https://stackoverflow.com/q/69692842/1130172
const origCreateHash = crypto.createHash;
crypto.createHash = (algorithm, options) => {
const newAlgorithm = algorithm === "md4" ? "md5" : algorithm;
return origCreateHash(newAlgorithm, options);
};
@blakek
blakek / fontElementMap.js
Created October 18, 2023 18:32
Find font usage on a page and what elements use it
function fontElementMap(selector = "*", onlyVisible = true) {
const allElements = document.querySelectorAll(selector);
const fontElements = {};
for (element of allElements) {
const style = window.getComputedStyle(element);
const fontFamily = style.getPropertyValue("font-family");
if (!fontFamily) {
continue;
@blakek
blakek / example-limit-stream-output.bash
Last active September 19, 2023 20:07
Stream output limit in Bash
#! /usr/bin/env bash
# Formatting helpers
bold() {
printf "\033[1m%s\033[0m\n" "$*"
}
dim() {
printf "\033[2m%s\033[0m\n" "$*"
}
@blakek
blakek / runtime.bash
Created September 15, 2023 21:07
Bash module idea
#!/usr/bin/env bash
declare -A IMPORTS
# Returns the path of the script of the caller
function relative_path() {
dirname "${BASH_SOURCE[1]}"
}
# Imports a module