Skip to content

Instantly share code, notes, and snippets.

View fostyfost's full-sized avatar
🤪
All you need is Half-Life 3

Fosty Fost fostyfost

🤪
All you need is Half-Life 3
View GitHub Profile
@fostyfost
fostyfost / index.ts
Created June 25, 2024 15:34
Distributive Pick
type DistributivePick<T, K extends keyof T> = T extends any ? Pick<T, K> : never
@fostyfost
fostyfost / example.ts
Created June 25, 2024 15:33
Distributive Omit
type Album = {
id: string // same per type
title: string // same per type
genre: string // different
}
type CollectorEdition = {
id: string // same per type
title: string // same per type
limitedEditionFeatures: string[] // different
@fostyfost
fostyfost / index.ts
Created June 3, 2024 20:21
Union to tuple
export type UnionToTuple<U extends string, R extends any[] = []> = {
[S in U]: Exclude<U, S> extends never ? [...R, S] : UnionToTuple<Exclude<U, S>, [...R, S]>
}[U]
@fostyfost
fostyfost / property-descriptor-stack.ts
Created September 15, 2023 16:59 — forked from imdfl/property-descriptor-stack.ts
Change property descriptors on an object with an undo stack
interface IPropertyDescriptorStack {
/**
* set the property descriptor of the member prop on the target object
* Fails silently, returning false
* @param props
*/
push(props: Partial<PropertyDescriptor>): boolean;
/**
* reset the property descriptor of the member prop on the target object, either to
* the state it was before the last set, or to the initial state, resetting the state stack.
@fostyfost
fostyfost / promise-delegate.ts
Created May 5, 2023 15:29
Promise delegate helper
export type PromiseDelegateResolveCallback<ResolveType> = (value: ResolveType | PromiseLike<ResolveType>) => void
export type PromiseDelegateRejectCallback<RejectType> = (reason?: RejectType) => void
export type PromiseDelegateResult<ResolveType, RejectType> = {
promise: Promise<ResolveType>
resolve: PromiseDelegateResolveCallback<ResolveType>
reject: PromiseDelegateRejectCallback<RejectType>
}
@fostyfost
fostyfost / recursive-partial.ts
Created April 24, 2023 12:38
TypeScript: Recursive partial
export type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? RecursivePartial<U>[]
: T[P] extends (...args: any) => any
? T[P] | undefined
: T[P] extends object
? RecursivePartial<T[P]>
: T[P]
}
@fostyfost
fostyfost / README.md
Created April 5, 2023 12:45 — forked from vicapow/README.md
getStyles function from NYT's Crowbar

Get all the styles as a string from a given document. useful for creating a PNG from an SVG. SVG's need to have all of their style information embedded within their document to be properly exported.

Example usage:

var styles = getStyles(window.document);

Try it by going to [http://bl.ocks.org/vicapow/raw/9904319/](this block) and running the above usage example in the console.

@fostyfost
fostyfost / index.ts
Created April 3, 2023 08:14
D3 force rectangle
import { quadtree } from 'd3'
type Rectangle = {
x: number
y: number
width: number
height: number
}
export const forceRectangle = <T extends Rectangle>(gapX: number, gapY: number) => {
@fostyfost
fostyfost / get-circle-path.ts
Created November 30, 2022 14:08
SVG: Get circle path
export const getCirclePath = (cx: number | undefined = 0, cy: number | undefined = 0, r: number | undefined = 0): string => {
return `M ${cx - r}, ${cy} a ${r}, ${r} 0 1,0 ${r * 2},0 a ${r},${r} 0 1,0 ${r * -2},0`
}
@fostyfost
fostyfost / vosually-hidden.css
Created November 11, 2022 14:06
Visually hidden CSS
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
border: 0;
padding: 0;
white-space: nowrap;
clip-path: inset(100%);
clip: rect(0 0 0 0);