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 / sub-add.c
Created April 19, 2024 06:48
sub and add repr in c
#include <stdio.h>
#include <stdint.h>
uint8_t da(uint8_t value) {
uint8_t lo = value & 0x0F;
uint8_t hi = value >> 4;
if (lo > 9) lo += 6;
if (hi > 9) hi += 6;
return (hi << 4) | lo;
}
@trvswgnr
trvswgnr / README.md
Last active April 17, 2024 20:13
fizzbuzz in nasm assembly
@trvswgnr
trvswgnr / getSixRandomFiles.go
Last active April 17, 2024 03:31
get 6 random files from a directory
package main
import (
"errors"
"math/rand"
"os"
"time"
)
// initialize rng
@trvswgnr
trvswgnr / fp.ts
Last active April 10, 2024 23:00
functional programming typescript extensions
type AnyFn = (...args: any[]) => any;
namespace ElementExt {
export function addEventListener<
El extends Element | Document,
const S extends string,
const H extends AnyFn,
>(name: S, cb: H): (el: El) => void;
export function addEventListener<
El extends Element | Document,
@trvswgnr
trvswgnr / .gitignore
Created April 6, 2024 04:10
c# gitignore
# Standard .NET and C# ignores
bin/
obj/
out/
build/
buildscript/
*.sln.cache
*.suo
*.user
*.userprefs
@trvswgnr
trvswgnr / deep.test.ts
Last active February 5, 2024 21:39
typescript deep merge and deep clone
import { describe, it, expect } from "bun:test";
import { deepClone, deepMerge, isObject } from "./lib.ts";
describe("isObject", () => {
it("returns false for null", () => expect(isObject(null)).toBe(false));
it("returns false for undefined", () => expect(isObject(undefined)).toBe(false));
it("returns false for numbers", () => expect(isObject(0)).toBe(false));
it("returns false for strings", () => expect(isObject("")).toBe(false));
it("returns false for booleans", () => expect(isObject(false)).toBe(false));
it("returns true for objects", () => expect(isObject({})).toBe(true));
@trvswgnr
trvswgnr / dst.ts
Last active January 23, 2024 07:11
check if a date is within dst
function isDST(date: Date, timeZone: string = 'America/New_York'): boolean {
const year = date.getFullYear();
const startDST = getNthDayOfMonth(2, 0, 2, year); // sec sun in mar
const endDST = getNthDayOfMonth(1, 0, 10, year); // first sun in nov
const localDate = new Date(date.toLocaleString('en-US', { timeZone }));
const utcOffset = localDate.getTimezoneOffset() * 60000; // in milliseconds
const utcDate = new Date(localDate.getTime() + utcOffset);
return utcDate >= startDST && utcDate < endDST;
}
@trvswgnr
trvswgnr / README.md
Last active January 8, 2024 01:20
spinners.ts - some simple but cool text spinners, mostly taken from https://wiki.tcl-lang.org/page/Text+Spinner
@trvswgnr
trvswgnr / pipe.ts
Last active January 4, 2024 22:26
pipe function for typescript for chaining (very similar to Rust's `Option` type)
export type pipe<T> = {
<U>(x: T, id?: string): pipe<U>;
map: <U>(fn: (x: T) => U) => pipe<U>;
unwrap: () => NonNullable<T>;
unwrapOr: (defaultValue: NonNullable<T>) => NonNullable<T>;
unwrapOrElse: <T>(fn: () => T) => NonNullable<T>;
unwrapOrDefault: () => NonNullable<T>;
};
export const pipe = <T>(x: T): pipe<T> => _pipe(x, x);
@trvswgnr
trvswgnr / Enum.ts
Last active January 3, 2024 05:26
better ts enums
export type Enum<T> = {
[key in keyof T]: T[key] extends (...args: any[]) => any ? ReturnType<T[key]> : T[key];
}[keyof T];
export const Enum = <const T>(obj: T) => obj;
export function EnumVariant<const F extends (...args: any[]) => T, const T>(fn: F): F;
export function EnumVariant<const T>(arg: T): T;
export function EnumVariant(arg: unknown) {
return arg;
}
const DataTypes = Enum({