Skip to content

Instantly share code, notes, and snippets.

View xantiagoma's full-sized avatar

Santiago Montoya A. xantiagoma

View GitHub Profile
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];
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]];
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,
});
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
/**
* 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;
}
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}>
import {
FormikState,
getIn,
FormikValues,
FieldConfig,
FieldInputProps,
FormikErrors,
FormikTouched,
FormikProps,
useFormik,
import { expect, test, describe } from "bun:test";
import {
retry,
stopAfterAttempt,
stopAfterDelay,
stopBeforeDelay,
waitCombine,
waitExponential,
waitExponentialJitter,
waitFixed,
export function* enumerate<T>(
iterable: Iterable<T>,
start = 0,
): Generator<[number, T]> {
let index = start;
for (const value of iterable) {
yield [index, value];
index++;
}
}
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(