This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum QuerySelectors { | |
Eq = "$eq", | |
Gt = "$gt", | |
Gte = "$gte", | |
Lt = "$lt", | |
Lte = "$lte", | |
Ne = "$ne" | |
} | |
enum LogicalOperators { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
OlderNewer