Skip to content

Instantly share code, notes, and snippets.

View pedropedruzzi's full-sized avatar

Pedro Pedruzzi pedropedruzzi

View GitHub Profile
@pedropedruzzi
pedropedruzzi / generic-type-bug.ts
Created February 2, 2022 17:47
Generic Type Bug in TypeScript
interface Shape {
name: string;
}
interface Square extends Shape {
length: number;
}
interface Circle extends Shape {
radius: number;
// https://noticias.uol.com.br/eleicoes/2022/apuracao/2turno/votos-por-estado/presidente/
function compute() {
const states = document.querySelectorAll('.president-by-state')
const estimates = {};
let computedVotes = 0, minComputePercentPerState = 100, minComputePercentState;
for (const state of states) {
const stateName = state.querySelector('.president-by-state-label').innerText;
const stateVoters = Number(state.querySelector('.voters').innerText.replace(/\D/g, ''));
const computedPercent = parseFloat(state.querySelector('.president-by-state-apuration .percentage').innerText.replace(',', '.'));
@pedropedruzzi
pedropedruzzi / runWithLimitedConcurrency.ts
Created March 2, 2023 17:35
TypeScript Experiment: run async functions with limited concurrency
// Copyright (c) 2023 Vendah Atividades de Internet LTDA. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// Get a copy at https://opensource.org/licenses/MIT.
export async function runWithLimitedConcurrency<T>(concurrency: number, jobs: (() => Promise<T>)[]): Promise<T[]> {
const setsOfJobs = batchify(jobs, Math.ceil(jobs.length / concurrency));
const promises: Promise<T[]>[] = [];
for (const [setIndex, setOfJobs] of setsOfJobs.entries()) {
const instrumentedSetOfJobs = setOfJobs.map((job, jobIndex) => () => {