Skip to content

Instantly share code, notes, and snippets.

View censuradho's full-sized avatar
🔓
OpenToWork

Gustavo Leite censuradho

🔓
OpenToWork
View GitHub Profile
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
class BarberShop {
constructor(
chairsAvailable
) {
this.chairsAvailable = chairsAvailable;
this.queue = [];
this.barberAttending = false;
}
@censuradho
censuradho / gist:32e106fe66a6068c80cf9c73ccb62dd6
Created October 9, 2024 17:30
Regexs para identificar as principais bandeiras de cartões
const cards = {
visa : /^4[0-9]{12}(?:[0-9]{3})/,
mastercard : /^5[1-5][0-9]{14}/,
diners : /^3(?:0[0-5]|[68][0-9])[0-9]{11}/,
amex : /^3[47][0-9]{13}/,
discover : /^6(?:011|5[0-9]{2})[0-9]{12}/,
hipercard : /^(606282\d{10}(\d{3})?)|(3841\d{15})/,
elo : /^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})/,
jcb : /^(?:2131|1800|35\d{3})\d{11}/,
aura : /^(5078\d{2})(\d{2})(\d{11})$/
@censuradho
censuradho / useDebounce.ts
Created April 15, 2024 14:21
executa um callback após os milisegundos especificados para alterar a string e retornar o valor, útil quando estamos realizando algum tipo de filtro por string e não queremos chamar a API a cada letra digitada pelo usuário
import { useEffect, useState } from "react";
export function useDebounce<T>(
initialValue: T,
time: number
): [T, T, React.Dispatch<T>] {
const [value, setValue] = useState<T>(initialValue);
const [debouncedValue, setDebouncedValue] = useState<T>(initialValue);
useEffect(() => {
const usePDF = (conteudo: string, arquivo: string) => {
const linkVisualizacao = `data:application/pdf;base64,${conteudo}`;
const pdfBytes = Uint8Array.from(atob(conteudo), (c) => c.charCodeAt(0));
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const onDownloadPDF = () => {
const url = URL.createObjectURL(blob);
const element = document.createElement('a');
element.href = url;
element.download = arquivo;
@censuradho
censuradho / gist:db7c6bf7386555be5f518540478c3baa
Created November 27, 2023 19:24
usePreventClickJacking.tsx
import { useEffect } from 'react';
export function usePreventClickJacking() {
useEffect(() => {
const script = document.createElement('script');
script.async = true;
script.innerHTML = `
if (top != window) {
top.location = window.location;
@censuradho
censuradho / classGroupe.ts
Created June 30, 2023 15:57
função utilitária para agrupar (join) quantas classes CSS forem necessárias
/**
* classGroupe('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]') // → 'hover:bg-dark-red p-3 bg-[#B91C1C]'
*/
export function classGroupe (...args: any[]) {
return args.join(' ')
}
@censuradho
censuradho / emojiParse.ts
Created June 22, 2023 13:59
function to only accept emojis, pass a string as the first parameter and it returns an array of found emojis
/**
* getEmojisFromString('This 😃 is an Emoji exemple ❤️') // ['😃', '❤️']
*/
function getEmojisFromString (str: string) {
return str.match(/\p{Emoji}+/gu)
}
@censuradho
censuradho / slugfy.ts
Created February 2, 2023 17:58
a slug functon generator
export const slugify = (...args: (string | number)[]): string => {
const value = args.join(' ')
return value
.normalize('NFD') // split an accented letter in the base letter and the acent
.replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
.toLowerCase()
.trim()
.replace(/[^a-z0-9 ]/g, '') // remove all chars not letters, numbers and spaces (to be replaced)
.replace(/\s+/g, '-') // separator
@censuradho
censuradho / useLocalStorage.ts
Last active January 19, 2023 12:23
a simple custom hook to handle local storage
import { useState } from 'react'
export function useLocalStorage<T> (
key: string,
initialValue: T
): [T, (value: T | ((val: T) => T)) => void] {
// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState<T>(() => {
try {
@censuradho
censuradho / Flex.tsx
Created June 10, 2022 13:39
Flex component for React applications
import { CSSProperties, memo, ReactNode } from 'react'
type ViewStyle = Pick<CSSProperties,
'alignItems'
| 'alignSelf'
| 'alignContent'
| 'justifyContent'
| 'flex'
| 'marginBottom'
| 'marginRight'