Skip to content

Instantly share code, notes, and snippets.

View maradondt's full-sized avatar

VladimirZhigalev maradondt

View GitHub Profile
function measureExecutionTime(callback: () => void) {
return function () {
const startTime = performance.now();
callback();
const endTime = performance.now();
const executionTime = endTime - startTime;
console.log(`Execution time for '${callback.name}': ${executionTime} milliseconds`);
};
}
@maradondt
maradondt / typeguard.ts
Created April 27, 2024 14:13
Typeguard factory typescrypt
/* eslint-disable @typescript-eslint/no-explicit-any */
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
interface Constructor<T = unknown, P extends unknown[] = never> {
new (...args: P): T;
}
type Guard<P = any, T extends P = P> = (x: P) => x is T;
type SomeOf<T extends Guard[]> = T[number] extends Guard<infer P, infer R>
? (x: P) => x is R
@maradondt
maradondt / ImageProcessor.ts
Last active September 26, 2024 14:51
Helper for work with images
import dayjs from 'dayjs';
import { instanceOf } from '../typeguard';
export type Coordinates = { x1: number; y1: number; x2: number; y2: number };
const ResultType = {
blob: 'blob',
file: 'file',
url: 'url',
image: 'image',
@maradondt
maradondt / validation.ts
Created October 10, 2024 12:14
yup custom validation hook
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { set } from 'lodash';
import { useMemo } from 'react';
import { Schema, ValidationError } from 'yup';
/**
* Sets the `innerError.message` in an `errors` object at the key
* defined by `innerError.path`.
* @param {Object} errors The object to set the error in.
@maradondt
maradondt / retry.ts
Last active April 29, 2025 13:17
helper fn to retry requests follow different strategies
import { sleep } from './utils';
export type RetryOptions = {
/**
* Maximum Retry Count (Following Circuit Breaker Strategy)
* @default 3
*/
retryCount?: number;
/** Prevent retry after link has expired */
linkTtlSec?: number | null;