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
type Result<T, E = Error> = [T, null] | [null, E]; | |
export async function tryCatch<T, E = Error>( | |
promise: PromiseLike<T> | (() => PromiseLike<T>) | |
): Promise<Result<T, E>> { | |
try { | |
const data = await (typeof promise === 'function' ? promise() : promise); | |
return [data, null]; | |
} catch (error) { | |
return [null, error as E]; |
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 seedrandom from 'seedrandom'; | |
export function shuffleArray<T>(array: T[], seed: string): T[] { | |
const rng = seedrandom(seed); // Create a seeded random generator | |
const shuffledArray = array.slice(); // Create a copy of the array | |
// Fisher-Yates shuffle using the seeded random function | |
for (let i = shuffledArray.length - 1; i > 0; i--) { | |
const j = Math.floor(rng() * (i + 1)); | |
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; |
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
useHotkeys('p', () => { | |
selectPositionRef.current?.focus(); | |
setTimeout(() => { | |
// Create an 'ArrowDown' key event to open the dropdown | |
const openEvent = new KeyboardEvent('keydown', { | |
key: 'ArrowDown', | |
code: 'ArrowDown', | |
bubbles: true, | |
cancelable: 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
import React from 'react'; | |
/** | |
* Hook that prevents auto-focus behavior while still maintaining focus management. | |
* Useful for modal dialogs where you want to control the initial focus. | |
* | |
* @template E - Element type, defaults to HTMLDivElement | |
* @returns Object containing: | |
* - ref: React ref to attach to the element | |
* - onOpenAutoFocus: Event handler to prevent default focus behavior |
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
/** | |
* Copied from Mongo's https://github.com/mongodb/leafygreen-ui/blob/main/packages/hooks/src/useDynamicRefs/index.ts | |
* https://github.com/mongodb/leafygreen-ui/blob/main/packages/hooks/README.md#usedynamicrefs | |
*/ | |
import * as React from 'react'; | |
export interface UseDynamicRefsArgs { | |
prefix?: string; | |
} |
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 { ReactNode } from 'react'; | |
/** | |
* Component that conditionally renders children based on a value | |
* @param props.value - Value to check | |
* @param props.children - Content to render, either direct ReactNode or function that receives the value | |
* @param props.condition - Optional custom condition function | |
* @returns Rendered children or null | |
* @example | |
* <When value={user}> |
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 { | |
FormikState, | |
getIn, | |
FormikValues, | |
FieldConfig, | |
FieldInputProps, | |
FormikErrors, | |
FormikTouched, | |
FormikProps, | |
useFormik, |
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 { expect, test, describe } from "bun:test"; | |
import { | |
retry, | |
stopAfterAttempt, | |
stopAfterDelay, | |
stopBeforeDelay, | |
waitCombine, | |
waitExponential, | |
waitExponentialJitter, | |
waitFixed, |
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* enumerate<T>( | |
iterable: Iterable<T>, | |
start = 0, | |
): Generator<[number, T]> { | |
let index = start; | |
for (const value of iterable) { | |
yield [index, value]; | |
index++; | |
} | |
} |
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 range( | |
end: number, | |
config?: { step?: number; direction?: "asc" | "desc" }, | |
): Generator<number>; | |
export function range( | |
start: number, | |
end: number, | |
config?: { step?: number; direction?: "asc" | "desc" }, | |
): Generator<number>; | |
export function* range( |
NewerOlder