Skip to content

Instantly share code, notes, and snippets.

View JTRNS's full-sized avatar
💤

JT JTRNS

💤
  • The Netherlands
  • 16:14 (UTC +01:00)
View GitHub Profile
@JTRNS
JTRNS / custom-event-target.ts
Created September 30, 2024 10:12
CustomEventTarget, like EventTarget but with typed custom events.
type CustomEventMap<T> = {
[key in keyof T]: CustomEvent | Event;
};
interface CustomEventListener<M, K extends keyof M> {
(evt: M[K]): void;
}
interface CustomEventListenerObject<
M,
@JTRNS
JTRNS / wordlist.json
Created August 19, 2024 12:46
Just Words
[
{
"word": "a",
"level": "a1",
"type": "indefinite article"
},
{
"word": "abandon",
"level": "b2",
"type": "verb"
@JTRNS
JTRNS / AStarNavServer.gd
Created May 26, 2024 16:56
Simpler 2D TileMap navigation
class_name AStarNavServer
extends RefCounted
static var map: TileMap
static var astar: AStarGrid2D
static var cell_size: Vector2i
static func _static_init() -> void:
@JTRNS
JTRNS / base64.ts
Created April 13, 2024 07:27
base64 encoder and decoder
function base64ToBytes(base64: string): Uint8Array {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0) || 0);
}
function bytesToBase64(bytes: Uint8Array): string {
const binString = String.fromCodePoint(...bytes);
return btoa(binString);
}
@JTRNS
JTRNS / parse_formdata.ts
Created April 8, 2024 17:30
Zod FormData Helper
import { z } from "zod";
type ParsedFormData<T extends z.AnyZodObject> = {
data: null;
errors: z.typeToFlattenedError<z.infer<T>>['fieldErrors']
} | {
data: z.infer<T>;
errors: null;
}
@JTRNS
JTRNS / form_actions.ts
Created April 7, 2024 19:21
Remix Form Actions Helper
import type { ActionFunctionArgs, ActionFunction } from "@remix-run/node";
import type { AnyZodObject, z } from "zod"
export type FormActionHandler<T extends AnyZodObject> = (data: z.infer<T>, args: ActionFunctionArgs) => Promise<Response | null>
export type FormAction<T extends AnyZodObject> = {
schema: T
} & {
handler: FormActionHandler<T>
}
@JTRNS
JTRNS / template_string.ts
Last active June 28, 2024 13:09
Type Safe Templates
type Delimiter = ",\n"|"," | "." | "?" | "!" | "\n"
type IsTemplateTag<Word> = Word extends `{${infer TagName}}${Delimiter}${string}` ? TagName : never;
// type IsTemplateTag<Word> = Word extends `{${infer TagName}}` ? TagName : isTemplateTagWithComma<Word>;
// type isTemplateTagWithComma<Word> = Word extends `{${infer Tagname}},` ? Tagname : isTemplateTagWithPeriod<Word>;
// type isTemplateTagWithPeriod<Word> = Word extends `{${infer Tagname}}.` ? Tagname : never;
type TemplateTags<TemplateString> = TemplateString extends
`${infer PartA} ${infer PartB}` ? IsTemplateTag<PartA> | TemplateTags<PartB>
: IsTemplateTag<TemplateString>;
@JTRNS
JTRNS / rand.ts
Created September 19, 2023 09:41
deno random encoded bytes
import {toHashString} from 'https://deno.land/[email protected]/crypto/to_hash_string.ts';
import {parse} from 'https://deno.land/[email protected]/flags/mod.ts';
const args = parse(Deno.args, {
boolean: ['help'],
alias: {
help: 'h',
encoding: 'e',
},
string: ['encoding'],
@JTRNS
JTRNS / parse_markdown.ts
Created August 3, 2023 19:25
GitHub Flavoured Markdown Parsing in Deno
import { unified } from "https://esm.sh/[email protected]";
import remarkParse from "https://esm.sh/[email protected]";
import remarkFrontmatter from "https://esm.sh/[email protected]";
import remarkGfm from "https://esm.sh/[email protected]";
import remarkRehype from "https://esm.sh/[email protected]";
import rehypeStringify from "https://esm.sh/[email protected]";
import rehypeSlug from "https://esm.sh/[email protected]";
import rehypeAutolinkHeadings from "https://esm.sh/[email protected]";
import remarkGemoji from "https://esm.sh/[email protected]";
import rehypeStarryNight from "https://esm.sh/@microflash/[email protected]";
@JTRNS
JTRNS / router.ts
Last active July 19, 2023 22:57
URLPattern Router with basic pattern to parameter type inference
import type { ConnInfo, Handler } from "https://deno.land/[email protected]/http/server.ts";
type RemoveModifier<Pattern> = Pattern extends `${infer P}?` ? P
: Pattern;
type RemoveMatchingRule<Pattern> = Pattern extends `${infer P}(${infer R})` ? P
: Pattern;
type ExtractGroupName<Pattern> = RemoveModifier<RemoveMatchingRule<Pattern>>;
type PatternParamValue<Key> = Key extends `${infer PatternString}?`
? string | undefined