Skip to content

Instantly share code, notes, and snippets.

View christianvmm's full-sized avatar
💤

Christian Velez Medina christianvmm

💤
View GitHub Profile
@christianvmm
christianvmm / timeago-es.ts
Created March 4, 2024 04:47
timeago.js en español
import { type LocaleFunc, type TDate, format, register } from 'timeago.js'
const localeFunc: LocaleFunc = (_: number, idx: number, __?: number) => {
// number: the timeago / timein number;
// index: the index of array below;
// totalSec: total seconds between date to be formatted and today's date;
return [
['justo ahora', 'justo ahora'],
['hace %s segundos', 'en %s segundos'],
['hace 1 minuto', 'en 1 minuto'],
import { useCallback, useState } from 'react'
export type UseDisclosureReturn = {
open: boolean
onOpen: () => void
onClose: () => void
onToggle: () => void
onOpenChange: (v: boolean) => void
}
@christianvmm
christianvmm / js-to-jsx.js
Created December 17, 2023 21:30
Change JS to JSX file extension script
import fs from 'fs/promises'
import path from 'path'
const ignoredFiles = [
'.git',
'.vscode',
'node_modules',
'public'
]
function ignoreFile(filename) {
@christianvmm
christianvmm / useDebounce.ts
Created August 6, 2023 05:53
useDebounce.ts
import { useRef } from 'react'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useDebounce<T extends (...params: any[]) => void>(
fn: T,
delay = 1000
) {
const debounceRef = useRef<ReturnType<typeof setTimeout>>()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
import { useState } from 'react'
type FilterFunction<T> = (item: T) => boolean
export function useArray<T>() {
const [data, setData] = useState<Array<T>>([])
function push(value: T): void {
setData((prev) => [...prev, value])
}
@christianvmm
christianvmm / absolute-imports.ts
Created February 26, 2023 03:13
Configuration to use absolute imports in Vite ReactApp
// tsconfig.json
"references": [{ "path": "./tsconfig.node.json" }],
"extends": "./tsconfig.paths.json"
// create: tsconfig.paths.json
{
"compilerOptions": {
"baseUrl": ".",
@christianvmm
christianvmm / component-with-generics.tsx
Last active December 17, 2023 21:32
Generics in components. React Typescript
// Definition
type ComponentProps<Generic1, Generic2> = {
prop1: Generic1
prop2: Generic2
}
export const Component = <T, T1>(props: ComponentProps<T, T1>) => {
const { prop1, prop2 } = props
return <></>
}
export const debounce = (func) => {
let timer;
return function(...args) {
const context = this;
if(timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
func.apply(context, args);