Skip to content

Instantly share code, notes, and snippets.

View alii's full-sized avatar
🗿
compuoter

Alistair Smith alii

🗿
compuoter
View GitHub Profile
@alii
alii / validator.ts
Last active April 13, 2022 20:58
Small schema validator inspired by zod. Built so I could learn to some degree how zod works under the hood.
interface Schema<Out> {
parse(value: unknown, path: string): Out;
}
type Resolve<T> = T extends Schema<infer R> ? Resolve<R> : T;
function bool(): Schema<boolean> {
return {
parse(value, path) {
const valid = typeof value === "boolean";
@alii
alii / create-modal.tsx
Created March 10, 2022 15:00
HeadlessUI Modal Factory
import {Dialog, Transition} from '@headlessui/react';
import React, {Fragment, ReactNode} from 'react';
import {BiX} from 'react-icons/bi';
interface Props<Y> {
isOpen: boolean;
close: () => unknown;
// Required because used in the callback
// eslint-disable-next-line react/no-unused-prop-types
@alii
alii / scalable-fizzbuzz.ts
Created February 2, 2022 12:09
Overengineering a basic interview question by making the a scalable version of FizzBuzz.
interface Interceptor {
condition(i: number): boolean;
execute(i: number): void;
}
abstract class FizzBuzzUtils {
protected isMultipleOf(a: number, multiplier: number) {
return a % multiplier === 0;
}
}
@alii
alii / is-instance.ts
Created January 19, 2022 14:00
Useful error checking for promise chaining
type Constructor<T> = new (...args: any[]) => T;
type Then<T, R> = (value: T) => R;
export function ifInstance<T, R>(instance: Constructor<T>, then: Then<T, R>) {
return (value: unknown) => {
if (value instanceof instance) {
return then(value);
}
throw value;
@alii
alii / multiply.ts
Created December 31, 2021 12:00
Multiplication math in TypeScript's type system
// Multiplication in raw TypeScript. Please, please do not ever ever ever use this
type TupleOfLength<
T,
L extends number,
R extends T[] = []
> = R["length"] extends L ? R : TupleOfLength<T, L, [...R, T]>;
type FlattenResult<
T extends 0[][],
@alii
alii / handler-for.ts
Last active December 8, 2021 21:47
An easy way to not have to remember React event handlers types in TypeScript
type RemoveOn<T extends string> = T extends `on${infer R}` ? R : T;
type PropsFor<El extends keyof JSX.IntrinsicElements> = JSX.IntrinsicElements[El];
type OnProps<El extends keyof JSX.IntrinsicElements> = Uncapitalize<RemoveOn<Extract<keyof PropsFor<El>, `on${string}`>>>;
type HandlerFor<El extends keyof JSX.IntrinsicElements, Ev extends OnProps<El>> = NonNullable<PropsFor<El>[`on${Capitalize<Ev>}` & keyof PropsFor<El>]>;
// Creating with a callback
function handlerFor<El extends keyof JSX.IntrinsicElements, Ev extends OnProps<El>>(event: [El, Ev], handler: HandlerFor<El, Ev>) {
return handler;
}
@alii
alii / map-order.ts
Created November 14, 2021 15:12
Sort an array of objects with known keys
export function mapOrder<T, Key extends keyof T>(array: T[], order: Array<T[Key]>, key: Key) {
return array.sort((a, b) => {
const A = a[key];
const B = b[key];
if (order.indexOf(A) > order.indexOf(B)) {
return 1;
}
return -1;
@alii
alii / is.ts
Last active October 14, 2021 13:16
Checks if a value exists in an array in a type safe manner
/**
* Checks if a value exists in an array in a type safe manner
* @param value Value to check
* @param arr An array of possible matches
* @returns A boolean indicating if a passed value is one of the items in the array
*/
export function is<V extends string | number, Arr extends readonly [V, ...V[]]>(
value: unknown,
arr: Arr,
): value is Arr[number] {
@alii
alii / timers.ts
Created October 13, 2021 04:01
Experimenting with Node `timers/promises`
import { setInterval } from "timers/promises";
const createCounter = (initial = 0) => {
let value = initial;
return () => ++value;
};
const start = Date.now();
for await (const incr of setInterval(1000, createCounter())) {
@alii
alii / binary-tree.ts
Created September 30, 2021 00:41
binary tree implementation so you can win those code interviews 😎
import y from "yaml";
export class TreeNode<T> {
public readonly left?: TreeNode<T>;
public readonly right?: TreeNode<T>;
public readonly value: T;
constructor(
value: T,
left?: TreeNode<T>,