Skip to content

Instantly share code, notes, and snippets.

View blakek's full-sized avatar
✝️

Blake Knight blakek

✝️
View GitHub Profile
export enum KnownPaths {
GetHelp = "/app/get-help",
RunDetail = "/app/history/:runId"
}
/** Parses a string type into an object of route params */
type ExtractRouteParams<T extends string> = string extends T
? Record<string, string>
: T extends `${infer _Start}:${infer Param}/${infer Rest}`
? { [K in Param | keyof ExtractRouteParams<Rest>]: string }
@blakek
blakek / import-url.js
Created April 10, 2023 19:14
Run basic external code from a Zapier code step
async function importURL(url) {
const vm = require("vm");
const res = await fetch(url).then((r) => r.text());
return vm.runInThisContext(res, { filename: url });
}
// Example usage:
//
// await importURL(
// "https://gist.githubusercontent.com/blakek/660a8881ae56641d8804971b848df17e/raw/0fb0ec0646d5c8dc353dd9dadc9f5d8ccb7821b2/queryStringParse.js"
@blakek
blakek / transpose.bash
Created March 30, 2023 14:43
Music file manipulation
#!/usr/bin/env bash
set -eo pipefail
##
# Transpose a song from one key to another.
##
version='0.0.1'
# Formatting functions
@blakek
blakek / math-homework-check.ts
Created March 3, 2023 02:13
7th grader homework checker - TypeScript functions that can be used to calculate the area and perimeter of a circle
const PI = 3.14;
const CircleSize = {
Quarter: 0.25,
Half: 0.5,
Full: 1,
};
type WithRadiusOrDiameter = { r?: number; d?: number };
type WithPercent = { percent: typeof CircleSize[keyof typeof CircleSize] };
@blakek
blakek / raspberry-pi-print-server.md
Created January 4, 2023 17:11
WIP: Setting up an ancient Dell 1130 printer using Samsung printer drivers

Raspberry Pi - Print Server

Installing print server and drivers.

Print Server

  1. Install CUPS:
sudo apt install cups
@blakek
blakek / exec.ts
Created October 17, 2022 16:05
Easy Node.js exec
function exec(command: string, args?: string[]): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: "inherit" });
child.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command failed with code ${code}`));
}
@blakek
blakek / exampleUsage.bash
Last active October 6, 2022 12:45
Simple command to test if a branch was squashed/rebased and then merged into another. Not tested very much.
#! /usr/bin/env bash
merged=()
unmerged=()
for branch in $(git for-each-ref --format="%(refname:short)" refs/heads); do
if wasBranchMerged "$branch" staging; then
merged+=("$branch")
else
unmerged+=("$branch")
@blakek
blakek / launch.json
Created September 1, 2022 12:47
VS Code - debug current TypeScript file using `ts-node`
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run current file",
"type": "node",
"request": "launch",
@blakek
blakek / parse-date.ts
Created August 16, 2022 12:50
Covert an unknown value to a Date (or `undefined`)
function parseDate(dateString: unknown): Date | undefined {
// No date string was passed
const isKnownType =
typeof dateString === "string" ||
typeof dateString === "number" ||
dateString instanceof Date;
if (!isKnownType || !dateString) {
return undefined;
}
@blakek
blakek / usePoll.ts
Last active August 18, 2022 19:13
`usePoll` hook
import * as React from "react";
import { useTabVisibility } from "./useTabVisibility";
export interface UsePollOptions {
/** Calculate what the wait time (in ms) should be between polls */
backoffFunction: (pollCount: number) => number;
}
export interface UsePollCallbackParameters {