Skip to content

Instantly share code, notes, and snippets.

View ryangoree's full-sized avatar
🌎

Ryan Goree ryangoree

🌎
View GitHub Profile
@ryangoree
ryangoree / ByIndex.ts
Last active January 20, 2025 23:17
ByIndex
/**
* Converts array or tuple `T` to an object keyed by index.
*
* Example:
* type ExampleTuple = ByIndex<[string, number]>; // { 0: string, 1: number }
* type ExampleArray = ByIndex<(string | number)[]>; // { [x: number]: string | number }
*/
type ByIndex<T extends any[]> = {
[K in Extract<keyof T, `${number}`>]: T[K];
};
@ryangoree
ryangoree / WordMap.ts
Last active April 29, 2024 22:53
A utility type that converts a string of words into a map of values.
/**
* A utility type that returns a union of space-separated words in a string.
*
* @example
* ```ts
* type Foo = Words<'foo bar baz'>;
* // => 'foo' | 'bar' | 'baz'
*/
type Words<TString extends string> =
TString extends `${infer Word} ${infer Rest}` ? Word | Words<Rest> : TString;
@ryangoree
ryangoree / UnionToIntersection.ts
Created June 4, 2024 18:14
Convert members of a union to an intersection.
/**
* Convert members of a union to an intersection.
*
* @example
* ```ts
* type Union = { a: number } | { b: string };
* type Intersection = UnionToIntersection<Union>;
* // { a: number } & { b: string }
* ```
*
@ryangoree
ryangoree / useIntersecting.ts
Last active February 20, 2025 22:44
A react hook to observe the intersection of elements and return the IDs of those that are intersecting.
import { useEffect, useState } from "react";
/**
* A hook to observe the intersection of elements and return the IDs of those
* that are intersecting.
* @param ids The IDs of the elements to observe
* @param options The options to pass to the IntersectionObserver
*
* @example
* ```tsx
@ryangoree
ryangoree / OneOf.ts
Last active February 20, 2025 00:06
Construct a type in which only a single member of `T` is valid at a time.
/**
* Construct a type in which only a single member of `T` is valid at a time.
*
* @example
* ```ts
* type U = OneOf<
* | {
* a: string;
* }
* | {
@ryangoree
ryangoree / FirstSpecific.wip.ts
Last active February 20, 2025 21:19
🚧 WIP 🚧 - Get the first member of a tuple that is more or as specific as the member following it.
// 🚧 WIP 🚧 //
/**
* Get the first member of a tuple, {@linkcode T}, that is more or as specific
* as the member following it.
*
* @example
* ```ts
* type Status = "success" | "error" | "idle"
* type ActiveStatus = "success" | "error"
@ryangoree
ryangoree / Mask.ts
Last active June 27, 2025 17:44
🚧 WIP 🚧 - Create a subset of an object type based on a mask/template type.
/**
* Creates a new type that only includes properties from `T` that match the
* structure defined in `M`. Useful for creating a subset of an object type
* based on a mask/template type. Handles special cases for built-in objects
* (Functions, Maps, Sets, Arrays) by preserving their structure rather than
* attempting to deeply map their properties.
*
*
* @example
* ```ts
@ryangoree
ryangoree / s3reporter.js
Last active January 22, 2025 18:20
A node.js test reporter that formats results as CSV and uploads to S3.
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
const currentPath = [];
const csvRows = ["test_name,duration,passed,error,skipped,skip_message,logs"];
let pendingRow;
let diagnostics = [];
/**
* A test reporter that formats results as CSV and uploads to S3.
* @see https://nodejs.org/api/test.html#custom-reporters
@ryangoree
ryangoree / Extended.ts
Last active January 24, 2025 21:28
Get a superset of `T` that allows for arbitrary properties.
/**
* Get a superset of `T` that allows for arbitrary properties.
*
* @example
*
* ```ts
* interface Order {
* account: `0x${string}`;
* amount: bigint;
* }
@ryangoree
ryangoree / Length.ts
Created January 24, 2025 01:39
Get the length of a string.
/**
* Get the length of a string.
*
* @example
* ```ts
* type L = Length<"hello">; // 5
* ```
*/
type Length<
T extends string,