Skip to content

Instantly share code, notes, and snippets.

@alvarogfn
alvarogfn / filename.tsx
Last active March 20, 2025 14:42
Evitando usar waitFor ou findBy para melhorar a performance dos testes com mocks.
// App.test.tsx
import { describe, it } from "vitest";
import { render, screen } from "@testing-library/react";
import { App } from "./App";
// Comente ou descomente essa linha para ver a diferença entre mockado e não mockado.
// vi.mock(import("./useFetch"));
@alvarogfn
alvarogfn / squareRoot.ts
Created February 18, 2025 20:23
SquareRoot
const squareRoot = (n, g = 1, lg) => g === lg ? g : squareRoot(n, (g + (n / g)) / 2, g);
@alvarogfn
alvarogfn / install-deps.yaml
Created September 22, 2024 15:46
Github action for pnpm with cache and install deps
name: Reusable install deps
description: This workflow installs dependencies using PNPM and caches the store directory. It is meant to be used as a reusable workflow.
inputs:
NODE_VERSION:
description: 'The version of Node.js to use'
required: true
default: '18.x'
PNPM_VERSION:
description: 'The version of PNPM to use'
required: true
@alvarogfn
alvarogfn / index.ts
Last active September 20, 2024 20:44
Accept any string but keep auto complete
// Intersect string with {}:
export type StateWords = | 'first' | 'second' | 'third' | string & {}
// Record<never, never> can also work:
export type StateWords = | 'first' | 'second' | 'third' | string & Record<never, never>
// These intersections trick TypeScript into thinking that it is a "different" string type that literal string types are not assignable to (and thus the union won't get reduced to only string).
@alvarogfn
alvarogfn / useCurrentTime.tsx
Last active September 1, 2024 23:52
useCurrentTime
export const getCurrentTime = () => {
return 1000 * Math.floor(Date.now() / 1000 + 0.1)
}
export const getNextHourTimestamp = () => {
const date = new Date();
const currentHours = date.getHours();
date.setHours(currentHours + 1)
date.setMinutes(0)
@alvarogfn
alvarogfn / cachedFetch.ts
Created June 20, 2024 23:06
caches a fetch call based on the parameters passed to fetch and a unique identifier.
const memoryCache = new Map<string, any>()
/**
* `resetCachedFetch` is a function that clears the cachedFetch cache.
* It does not take any parameters and does not return any value.
*/
export const resetCachedFetch = () => memoryCache.clear()
/**
* `cachedFetch` is a generic function that performs a fetch operation and caches the result.
@alvarogfn
alvarogfn / useElementLineCount.ts
Created May 6, 2024 21:09
Hook que computa quantidade de linhas de um element com lineClamp
import { useEffect, useRef, useState } from 'react'
const countElementLines = (target: HTMLElement) => {
const style = window.getComputedStyle(target, null)
const fontSize = parseInt(style.getPropertyValue('font-size'))
let height = parseInt(style.getPropertyValue('height'))
const boxSizing = style.getPropertyValue('box-sizing')
if (boxSizing === 'border-box') {
@alvarogfn
alvarogfn / useLocalStorage.md
Last active July 4, 2023 01:44
Hook useLocalStorage
import React from "react";

function useLocalStorage<T>(key: string) {
  const [data, setData] = React.useState<T | null>(() => {
    const backup = localStorage.getItem(key);
    return typeof backup === "string" ? (JSON.parse(backup) as T) : backup;
  });

 function updateState() {
@alvarogfn
alvarogfn / useDragToScroll.md
Created July 3, 2023 21:56
Horizontal Click and Drag Scrolling with Vue Composable
export const useDragToScroll = (ref: Ref<HTMLElement | null>) => {
  const position = {
    left: 0,
    x: 0
  }

  const mouseDown = (event: MouseEvent) => {
    if (ref.value === null) return
    position.x = event.clientX
@alvarogfn
alvarogfn / README.md
Created March 23, 2023 17:21
Debug Functions for Javascript/Typescript

Tap

A function that displays the content of a variable or function return on the console without compromising the flow.

Javascript:

  function tap(x, fn) {
    console.log(fn(x));
    return x;
 }