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 / 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 / get-array-range.ts
Last active April 21, 2023 10:12
Array Range
export const getArrayRange = (length: number, startFrom = 0): number[] => {
return Array.from({ length }, (_, index) => index + startFrom)
}
@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);
@fostyfost
fostyfost / is-in-app.ts
Created October 21, 2022 09:52
Check `WebView` user agent for in-app cases
// @see https://stackoverflow.com/questions/71459572/not-possible-to-detect-webview-with-javascript-and-user-agent-on-old-android-ver
const rules = [
// If it says it's a webview, let's go with that.
'WebView',
// iOS webview will be the same as safari but missing "Safari".
'(iPhone|iPod|iPad)(?!.*Safari)',
// https://developer.chrome.com/docs/multidevice/user-agent/#webview_user_agent
'Android.*Version/[0-9].[0-9]',
// Also, we should save the wv detected for Lollipop.
// Android Lollipop and Above: webview will be the same as native,
@fostyfost
fostyfost / index.ts
Created October 13, 2022 18:38
Required Field
export type RequiredField<T, K extends keyof T> = T & Required<Pick<T, K>>