Skip to content

Instantly share code, notes, and snippets.

@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/rxjs@7.3.0";
import {
switchMap,
map,
combineLatestWith
} from "https://cdn.skypack.dev/rxjs@7.3.0/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
Created June 10, 2024 12:20
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]);
const sleep = (secs: number) => new Promise<NodeJS.Timeout>(success => {
const timeOutId = setTimeout(() => {
success(timeOutId);
}, secs * 1000);
});
export { sleep };