Skip to content

Instantly share code, notes, and snippets.

View trvswgnr's full-sized avatar
:octocat:
hardly workin'

Travis A. Wagner trvswgnr

:octocat:
hardly workin'
View GitHub Profile
@trvswgnr
trvswgnr / index.ts
Created August 26, 2024 16:00
bun build and serve with browser hmr
import path from "path";
import chokidar from "chokidar";
import fs from "fs";
import type { ServerWebSocket } from "bun";
const CONFIG = Object.freeze({
srcDir: "./src",
outDir: "./public",
entrypoints: ["index.ts"],
});
@trvswgnr
trvswgnr / compress_video
Last active September 17, 2024 13:38
portable shell script to compress videos with ffmpeg
#!/bin/sh
print_usage() {
echo "usage: compress_video <input_file>"
echo "supported formats: mp4, webm, mkv, mov, avi, flv"
}
get_extension() {
f="${1##*/}"
case "$f" in
@trvswgnr
trvswgnr / main.go
Created August 15, 2024 08:16
raycasting multiple layers in go with ebitengine
package main
import (
"image/color"
"log"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
@trvswgnr
trvswgnr / mergesort.ml
Created August 14, 2024 09:03
ocaml in-place merge sort
let merge arr l m r max =
let i = ref l in
let j = ref (m + 1) in
let k = ref l in
while !i <= m && !j <= r do
if arr.(!i) mod max <= arr.(!j) mod max then (
arr.(!k) <- arr.(!k) + (arr.(!i) mod max * max);
incr k;
incr i)
else (
@trvswgnr
trvswgnr / djb2.c
Last active July 24, 2024 04:00
various hash algorithms in c
#include <stdint.h>
uint32_t hash_djb2(unsigned char *str) {
uint32_t hash = 5381;
uint32_t c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
@trvswgnr
trvswgnr / fetchJson.test.ts
Last active September 6, 2024 00:00
nice lil typescript fetch wrapper with errors as values
import { describe, it, expect, spyOn } from "bun:test";
import { fetchJson } from "./fetchJson";
class MockResponse {
static instanceCount = 0;
constructor(
public readonly ok: boolean,
private jsonSuccess: boolean | "bad parse",
) {
MockResponse.instanceCount++;
@trvswgnr
trvswgnr / fileContains.test.ts
Last active July 31, 2024 19:47
check if a file contains a specified phrase, without loading the entire file into memory
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { fileContains, BoyerMooreSearcher } from "./fileContains";
import fs from "fs";
import path from "path";
describe("fileContains", () => {
const testDir = path.join(__dirname, "test-files");
beforeAll(() => {
if (!fs.existsSync(testDir)) {
@trvswgnr
trvswgnr / tramp.ts
Last active July 11, 2024 18:59
trampoline typescript
/**
* Transforms a function that returns either a direct value or a thunk (a
* no-argument function that returns a value) into a function that only returns
* a direct value. It does this by repeatedly evaluating the function if it
* returns a thunk, until a direct value is obtained.
*
* @template T The type of the value to be returned by the trampoline function.
* @template A The type tuple representing the argument types accepted by the
* function f.
* @param f A function that takes arguments of type A and returns either a
@trvswgnr
trvswgnr / Timeout.test.ts
Created June 27, 2024 20:39
custom setTimeout implementation in TypeScript
import { describe, it, expect, mock } from "bun:test";
import { Timeout } from "./shared";
describe("Timeout", () => {
it("should call the callback after a specific time", async () => {
expect.hasAssertions();
return await new Promise<void>((resolve) => {
const start = Date.now();
Timeout.set(() => {
expect(Date.now() - start).toBeGreaterThanOrEqual(100);
@trvswgnr
trvswgnr / map.ts
Last active June 26, 2024 11:45
custom ts map with eq and cmp
export enum Ordering {
Less = -1,
Equal = 0,
Greater = 1,
}
export interface Eq<T> {
eq(other: T): boolean;
}