Skip to content

Instantly share code, notes, and snippets.

View nebarf's full-sized avatar
🎨
Coding

Francesco Benedetto nebarf

🎨
Coding
View GitHub Profile
@nebarf
nebarf / union-to-tuple-type.ts
Created August 23, 2024 10:18
Union to tuple type
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never;
type UnionToTuple<U> = UnionToIntersection<
U extends any ? (u: U) => void : never
> extends (v: infer V) => void
? [...UnionToTuple<Exclude<U, V>>, V]
const { createWriteStream } = require('fs');
const { join } = require('path');
const { EOL } = require('os');
const logLevels = {
off: -1,
debug: 0,
info: 1,
warn: 2,
error: 3,
const http = require('http');
class Queue {
constructor(executor) {
this._queue = [];
this._commandQueue = [];
const enqueue = (item) => {
this._queue.push(item);
@nebarf
nebarf / 5_4_async_map.js
Created May 14, 2021 16:10
Node JS Design Patterns book
async function mapAsync(iterable, callback, concurrency) {
if (!concurrency || !Number.isInteger(concurrency)) {
throw new Error('"Concurrency" param must be a positive integer.');
}
// Split the iterable into a set of chunks. The execution of chunks is then
// done in series to enforce the provided concurrency level.
let slices = [];
for (const [index, item] of iterable.entries()) {