Skip to content

Instantly share code, notes, and snippets.

View codebycarlos's full-sized avatar
🚢
Shipping

Carlos Vieira codebycarlos

🚢
Shipping
View GitHub Profile
@codebycarlos
codebycarlos / debounce.ts
Created February 20, 2024 09:56
debounce
/**
* Creates a debounced function that delays invoking `func` only for subsequent
* calls within the `debounceDelayMs` period after the first call.
*
* - `func`: Function to debounce. Can be asynchronous.
* - `debounceDelayMs`: Milliseconds to delay subsequent calls.
* - Returns: A function returning a promise with `func`'s result.
*
* The first call to the debounced function is executed immediately, while
* subsequent calls within the `debounceDelayMs` period are debounced.
@codebycarlos
codebycarlos / withPreventDefault.ts
Created January 29, 2024 19:10
withPreventDefault
/**
* A higher-order function that wraps a given function with a call to
* `preventDefault` on the event.
*/
export function withPreventDefault<T extends (...args: any[]) => any>(
originalFunction: T
) {
return function (...args: Parameters<T>): ReturnType<T> {
const event = args[0];
@codebycarlos
codebycarlos / connectRouter.tsx
Last active May 17, 2024 15:48
connectRouter
import type { ReactElement, ComponentType } from "react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
export type Router = {
location: ReturnType<typeof useLocation>;
navigate: ReturnType<typeof useNavigate>;
params: ReturnType<typeof useParams>;
};