This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Add to your .bashrc or .zshrc | |
| # Use like 'yat react' | |
| # Will install the package and its typings if needed | |
| # requires jq to be installed | |
| # for yarn (yarn add w/ types) | |
| function yat() { | |
| yarn add $1 | |
| if ! cat node_modules/$1/package.json | jq -e 'select((.types != null) or .typings != null)' > /dev/null; | |
| then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function get(obj: any, path: string[] | string, defaultValue?: any): any { | |
| const [key, ...rest] = Array.isArray(path) | |
| ? path | |
| : path.split(/\.|\[(\d+)\]/).filter(Boolean); | |
| const value = obj[key]; | |
| if (value === undefined) { | |
| return defaultValue; | |
| } | |
| return rest.length === 0 ? value : get(value, rest, defaultValue); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const handler: ProxyHandler<URLSearchParams> = { | |
| get(target, prop: string) { | |
| const values = target.getAll(prop); | |
| return values.length === 1 ? values[0] : values; | |
| }, | |
| set(target, prop: string, value) { | |
| target.delete(prop); | |
| Array.isArray(value) ? value.forEach(v => target.append(prop, v)) : target.set(prop, value.toString()) | |
| return true; | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const splice = (start: number) => (deleteCount = Infinity) => <T>( | |
| ...items: T[] | |
| ) => (array: T[]) => [ | |
| ...array.slice(0, start), | |
| ...items, | |
| ...array.slice(start + deleteCount) | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function* enumerate<T>(iterable: Iterable<T>) { | |
| let index = 0; | |
| for (const item of iterable) { | |
| yield [index++, item] as const; | |
| } | |
| } | |
| function findMap<T, S extends T, R>( | |
| iterable: Iterable<T>, | |
| predicate: (item: T, index: number) => item is S, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function* chunk<T>(iterable: Iterable<T>, size = 1): Iterable<Array<T>> { | |
| let buffer: T[] = []; | |
| for (const value of iterable) { | |
| buffer.push(value); | |
| if (buffer.length === size) { | |
| yield buffer; | |
| } | |
| buffer = []; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useEffect, useState, useCallback, DependencyList } from 'react'; | |
| function useLazy<T>(factory: () => T, deps: DependencyList | undefined): () => T { | |
| const [state, setState] = useState<{ used: false } | { used: true; value: T }>({ used: false }); | |
| useEffect(() => { | |
| setState({ used: false }) | |
| }, deps); | |
| return useCallback(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function all<T>( | |
| iterable: Iterable<T> | |
| ): iterable is Iterable<Exclude<T, Falsy>>; | |
| export function all<T, V extends T>( | |
| iterable: Iterable<T>, | |
| keyFn: TypePredicate<V, T> | |
| ): iterable is Iterable<V>; | |
| export function all<T>( | |
| iterable: Iterable<T>, | |
| keyFn: Predicate<T> | keyof T |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useEffect, useState, useMemo } from 'react'; | |
| export interface UseEventSourceOptions<T = string> { | |
| withCredentials?: boolean; | |
| extractor?: (value: any) => T; | |
| } | |
| export function useEventSource<T = string>(url: string, { withCredentials, extractor = JSON.parse }: UseEventSourceOptions<T> = {}) { | |
| const [data, setData] = useState<T>(); | |
| const [readyState, setReadyState] = useState < 'connecting' | 'open' | 'closed' | 'error'>('connecting') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Extension of the Map class that stores multiple values. | |
| */ | |
| export class MultiValueMap<K, V> extends Map<K, V[]> { | |
| /** | |
| * Add the given value to the current list of values for the given key. | |
| * | |
| * @param key - the key | |
| * @param value - the value to be added | |
| */ |