Skip to content

Instantly share code, notes, and snippets.

View xxzefgh's full-sized avatar

Mirza Brunjadze xxzefgh

  • Tbilisi, Georgia
View GitHub Profile
type MapFn<T> = (x: T) => unknown;
type MapSchema<T> = { [K in keyof T]?: MapFn<T[K]> | MapSchema<T[K]> };
type MapReturnType<T, TMap extends MapSchema<T>> = {
[K in keyof T]: TMap[K] extends Function
? ReturnType<Extract<TMap[K], MapFn<T[K]>>>
: MapReturnType<T[K], TMap[K]>;
};
function isMapFn<T = any>(x: unknown): x is MapFn<T> {
return typeof x === "function";
enum QuerySelectors {
Eq = "$eq",
Gt = "$gt",
Gte = "$gte",
Lt = "$lt",
Lte = "$lte",
Ne = "$ne"
}
enum LogicalOperators {
interface ReduceResult<T> {
done: true;
value: T;
}
export function reduceLargeArray<T, R>(
arr: T[],
processEachFn: (acc: R, x: T) => ReduceResult<R> | R,
initialValue: R,
onCompleteFn: (r: R) => void
export function exponentialRandomBackoff(
initialIntervalMillis: number,
multiplier: number,
randomizationFactor: number
) {
checkInterval(initialIntervalMillis);
checkMultiplier(multiplier);
checkRandomizationFactor(randomizationFactor);
return (attemptsMade: number) => {
const interval = Math.round(Math.pow(multiplier, attemptsMade) * initialIntervalMillis);
/**
* Reimplemented python's `random.choices` function
* https://github.com/python/cpython/blob/3.9/Lib/random.py#L473
*/
export function randomChoice<T>(population: T[], weights: number[], random: () => number): T {
let n = population.length;
let cum_weights = [];
for (let i = 0; i < weights.length; i++) {
cum_weights[i] = i === 0 ? weights[i] : weights[i] + cum_weights[i - 1];
}
function repeat(x: number, n: number): number[] {
return Array(n).fill(x);
}
/**
* Reimplemented Python's itertools.combinations_with_replacement
* https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement
*/
export function* combinationsWithReplacement<T>(pool: T[], r: number) {
const n = pool.length;
import {Multipart, MultipartFile, MultipartValue} from '@fastify/multipart';
import {FastifyRequest} from 'fastify';
function isMultipartFile(m: Multipart): m is MultipartFile {
return !!m['toBuffer'];
}
async function getValueOrBuffer(m: Multipart): Promise<string | Buffer> {
if (isMultipartFile(m)) {
return m.toBuffer();