Skip to content

Instantly share code, notes, and snippets.

View uladkasach's full-sized avatar

Uladzimir Kasacheuski uladkasach

  • Miami, FL
View GitHub Profile
export enum QueueType {
FIRST_IN_FIRST_OUT = "FIRST_IN_FIRST_OUT",
LAST_IN_FIRST_OUT = "LAST_IN_FIRST_OUT",
}
export class Queue<T> {
private type: QueueType;
private data: T[];
constructor({ type = QueueType.LAST_IN_FIRST_OUT }: { type: QueueType }) {
this.type = type;
@uladkasach
uladkasach / getFirstFunctionNameFromCallStack.test.ts
Created December 16, 2022 05:32
getFirstFunctionNameFromCallStack
import { getFirstFunctionNameFromCallStack } from './getFirstFunctionNameFromCallStack';
export const someSuperCoolAnonymousFunctionAssignedToAVariableIWantTheNameOf =
() => {
const methodName = getFirstFunctionNameFromCallStack();
return { methodName };
};
export const someSuperCoolNamedFunctionIWantTheNameOfWhichHappensToAlsoBeAssignedToAVariable =
function someSuperCoolNamedFunctionIWantTheNameOf() {
@uladkasach
uladkasach / command.sh
Created July 27, 2021 10:37
*.test.integration.ts -> *.integration.test.ts
for f in **/*.test.integration.ts; do mv -- "$f" "${f%.test.integration.ts}.integration.test.ts"; done
@uladkasach
uladkasach / isOfEnum.ts
Created March 28, 2021 14:41
isOfEnum.ts
// generic fn to create those type guards: https://stackoverflow.com/a/58278753/3068233
const createIsOfEnum = <T>(e: T) => (token: any): token is T[keyof T] => Object.values(e).includes(token as T[keyof T]);
// then use it to create a type checker for your particular enums
const isPlanet = createIsOfEnum(Planet);
// ...
@uladkasach
uladkasach / isPresent.ts
Created March 28, 2021 14:39
isPresent.ts
// https://github.com/microsoft/TypeScript/issues/16069#issuecomment-566222173
export const isPresent = <T>(t: T | undefined | null | void): t is T => t !== undefined && t !== null;
import { withCastingOutput } from './withCastingOutput';
describe('withCastingOutput', () => {
it('should be able to wrap a function and add output casting to it', async () => {
const startFromTheBottom = ({ bottom }: { bottom: boolean }) => ({
started: `started from the ${bottom ? 'bottom' : 'top'}`,
bottom,
});
const castToHere = ({ started, bottom }: { started: string; bottom?: boolean }) =>
`${started}${bottom ? ", now we're here" : " and we're still here"}`;
/**
* function which calls the wrapped function and runs it again one time if an error is caught
*/
export const withRetry = <R extends ReturnType<T>, T extends () => any>(logic: T) => {
return async (): Promise<R> => {
try {
return await logic();
} catch (error) {
return await logic();
}
export const withDbTransaction = <
P extends { dbConnection: DatabaseConnection },
R,
T extends (args: P) => Promise<R>
>(
logic: T,
): T => {
return (async (args: Parameters<T>[0]) => {
await args.dbConnection.beginTransaction(); // begin transaction
try {
@uladkasach
uladkasach / withTimeout.ts
Last active June 24, 2020 16:15
withTimeout.ts
/**
* returns a new function which calls the input function and "races" the result against a promise that throws an error on timeout.
*
* the result is:
* - if your async fn takes longer than timeout ms, then an error will be thrown
* - if your async fn executes faster than timeout ms, you'll get the normal response of the fn
*
* ### usage
* ```ts
* const result = await withTimeout(() => doSomethingAsync(...args), 3000);
@uladkasach
uladkasach / vims.md
Last active March 24, 2020 12:51
favorite commands

searching

  • /${__regex__}, enter, n/shift-n => forward search
  • ?${__regex__}, enter, n/shift-n => backwards search

search and replace

  • :${__from__},${__to__}s/${__search_regex__}/${__replace_regex__}/gc
    • drop c for no confirmation
    • replace ${__from__},${__to__} w/ % for all lines

move screen w/o cursor