Skip to content

Instantly share code, notes, and snippets.

@alexsad
alexsad / random.ts
Last active February 11, 2025 17:57
andar-aleatorio-com-rxjs
import { interval, merge } from 'rxjs';
import { map, takeWhile } from 'rxjs/operators';
//visual renders
const drawCell = (text: string) => {
const finalText = text || ''
const padText = ' '
return `${padText.substring(0, padText.length - finalText.length)}${finalText}`
}
<style>
body{
margin:0;
height: 500px;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}
@alexsad
alexsad / observer-element-resize.tsx
Created September 5, 2024 14:01
observer element resize
const setResizeDebounced = useDebouncedCallback(() => {
setResizeCount(curr => curr + 1)
}, 400)
useEffect(() => {
const rObs = new ResizeObserver(() => {
setResizeDebounced()
})
if (boardDrawRef.current) {
rObs.observe(boardDrawRef.current)
@alexsad
alexsad / list-example.js
Created June 29, 2024 13:16
rxjs list example to codepen
import { fromEvent, scan, Subject } from "https://cdn.skypack.dev/[email protected]";
import {
switchMap,
map,
combineLatestWith
} from "https://cdn.skypack.dev/[email protected]/operators";
let items = [];
const subjectList = new Subject();
@alexsad
alexsad / cpf_validation.js
Created June 27, 2024 13:03
cpf validation
const sumCPF = (cpf, digitos) => {
const sum = cpf
.split('')
.filter((...[,index]) => index < digitos)
.reduce((sum, curr, index) => sum + (curr * (1+digitos - index)), 0);
const total = (sum * 10) % 11;
return [10,11].includes(total) ? 0 : total;
}
@alexsad
alexsad / gen_uuid.ts
Last active June 17, 2024 14:01
gen uid
import MD5 from "crypto-js/md5";
const appendToString = (text: string, appendText: string, positionIndex: number) => {
return [text.slice(0, positionIndex), appendText, text.slice(positionIndex)].join('');
};
const genUUID = () => {
if (globalThis?.crypto && 'randomUUID' in globalThis.crypto) {
return `${globalThis.crypto.randomUUID()}`.replaceAll("-", "_");
}
@alexsad
alexsad / async_with_for.ts
Last active September 18, 2024 12:50
loops with promise
(async () => {
const fakeRefreshComp = (compName: string) => console.log('refresh-comp1:', compName);
const fakeRequest = (durationInSecs: number) => {
return new Promise<number>(success => {
const timeOutId = setTimeout(() => success(timeOutId), durationInSecs * 1000);
})
}
const elementList = [
@alexsad
alexsad / get_first_last_intital_names.js
Created June 6, 2024 18:59
get first and last intial letters from a full name
(() => {
const [, firstChar, lastChar] = /^(\w).*?(\w)\w+$/gm.exec('Izabella da Silva Xauro');
return {
firstChar,
lastChar,
}
})()
@alexsad
alexsad / fps_gen.ts
Created June 3, 2024 12:06
a FPS generator control utility
async function* nextFrame(fps: number) {
let then = performance.now();
const interval = 1000 / fps;
let delta = 0;
while (true) {
let now = await new Promise(requestAnimationFrame);
if (now - then < interval - delta) {
continue;
};
@alexsad
alexsad / use-mount.ts
Created June 3, 2024 12:04
a use mount utilility to simulate old react didmount
import { useEffect, useRef } from "react";
const useMount = (fun: () => Promise<void>, condition = true) => {
const firstFlow = useRef(true);
useEffect(() => {
if (condition && firstFlow.current) {
firstFlow.current = false;
fun();
}
}, [fun, condition]);