Skip to content

Instantly share code, notes, and snippets.

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

Travis Wagner trvswgnr

:octocat:
hardly workin'
View GitHub Profile
@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({
@trvswgnr
trvswgnr / Database.ts
Created January 2, 2024 00:15
npm sqlite3 db promises
import sqlite3 from "sqlite3";
export class Database {
private db: sqlite3.Database;
constructor(path: string) {
this.db = new sqlite3.Database(path);
}
public static open(path: string): Database {
return new Database(path);
}
@trvswgnr
trvswgnr / TrackablePromise.ts
Created December 30, 2023 09:47
trackable promise in typescript
class TrackablePromise<T> extends Promise<T> {
private _status: PromiseStatus = "pending";
private _promise: Promise<T>;
private _value: T | undefined;
constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
@trvswgnr
trvswgnr / escape-html.ts
Created December 29, 2023 20:30
escape html in a string - typescript
export function escapeHtml(str: string) {
let html = '';
let matchIndex: number | undefined;
let lastIndex = 0;
let char: string;
while (true) {
matchIndex = regex.exec(str)?.index;
if (matchIndex === undefined) break;
html += str.slice(lastIndex, matchIndex);
char = str[matchIndex];
@trvswgnr
trvswgnr / htmx-internal.d.ts
Last active December 7, 2023 03:39
htmx internal api type definitions + missing types
import type * as _htmx from "htmx.org";
declare global {
var htmx: typeof _htmx;
}
declare module "htmx.org" {
interface HtmxExtension {
init(apiRef: HtmxInternalApi): void;
}
@trvswgnr
trvswgnr / tw-dialog.html
Created November 13, 2023 08:30
no js dialog with tailwind
<label for="modal1">clicky boi</label>
<input id="modal1" class="peer" type="checkbox" hidden />
<label for="modal1" class="hidden peer-checked:block bg-black bg-opacity-25 fixed h-full w-full top-0 left-0"></label>
<dialog class="hidden peer-checked:block">
<p>lgtm ¯\_(ツ)_/¯</p>
<label for="modal1">close</label>
</dialog>
@trvswgnr
trvswgnr / hash.rs
Last active November 3, 2023 06:35
Fast hashing of file contents in CrabLang with xxHash
use std::{
hash::Hasher,
io::{self, BufReader, Read},
};
/// The size of the buffer used when reading the file.
const BUFFER_SIZE: usize = 8 * 1024; // 8 KB
/// Hashes the contents of the given readable object (e.g. a file).
#[inline]