Skip to content

Instantly share code, notes, and snippets.

View xxzefgh's full-sized avatar

Mirza Brunjadze xxzefgh

  • Tbilisi, Georgia
View GitHub Profile
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();
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;
/**
* 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];
}
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);
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
enum QuerySelectors {
Eq = "$eq",
Gt = "$gt",
Gte = "$gte",
Lt = "$lt",
Lte = "$lte",
Ne = "$ne"
}
enum LogicalOperators {
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";
import {Directive, Input, HostListener} from '@angular/core';
@Directive({
selector: 'img[fallback]'
})
export class ImageFallbackDirective {
private _fallback = 'assets/images/404_image.png';
@Input() set fallback(value: string) {
if (value && value.length > 0) {
function formatMoney(value: any, thousandSeparator: string | false = false, fractionDigits = 2): string {
let number = Number(value);
if (isNaN(number)) {
number = 0;
}
// Number#toFixed will throw runtime exception if fractionDigits isn't within 0-100 range
if (fractionDigits < 0 || fractionDigits > 100) {
fractionDigits = 0;
}

Keybase proof

I hereby claim:

  • I am xxzefgh on github.
  • I am xxzefgh (https://keybase.io/xxzefgh) on keybase.
  • I have a public key ASCP4gS21C15pcLXjdoq6908QlL0X_OOProk4k2DY6o-_wo

To claim this, I am signing this object: