This file contains 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 CacheEntry<R> = { | |
value: R; | |
timestamp: number; | |
}; | |
const promises = new Map<string, Promise<unknown>>; | |
export const cacheTTL = <Args extends unknown[], R>( | |
fn: (...args: Args) => Promise<R>, | |
ttl: number, | |
keyResolver?: (...args: Args) => string |
This file contains 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 const toDotNotation = (obj: object, prefix = ''): DotNotationObject => { | |
return Object.entries(obj) | |
.reduce<DotNotationObject>((acc, [key, value]) => { | |
const k = prefix ? `${prefix}.${key}` : key; | |
if (value && typeof value === 'object') { | |
if (Array.isArray(value)) { | |
return value.reduce((arrayAcc, item, index) => { | |
const kA = `${k}[${index}]`; |
This file contains 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
{ | |
"env": { | |
"node": true, | |
"es2021": true | |
}, | |
"extends": [ | |
"eslint:recommended", | |
"plugin:@typescript-eslint/recommended", | |
"plugin:import/errors", | |
"plugin:import/warnings", |
This file contains 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 type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; |
This file contains 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
/** | |
* Group array of objects by given keys | |
* | |
* @param keys keys to be grouped by | |
* @param array objects to be grouped | |
* | |
* @returns an object with objects in `array` grouped by `keys` | |
*/ | |
export const groupBy = <T>(keys: (keyof T)[]) => (array: T[]): Record<string, T[]> => { | |
return array.reduce((objectsByKeyValue, obj) => { |
This file contains 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 React, { Fragment, useEffect, useState } from 'react'; | |
import { createPortal } from 'react-dom'; | |
const Portal: React.FC<{ selector: string }> = ({ children, selector }) => { | |
const [mounted, setMounted] = useState<boolean>(false); | |
useEffect(() => { | |
setMounted(true); | |
return () => setMounted(false); | |
}, [selector]); |
This file contains 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 EventEmitter from 'events'; | |
export interface TimerEvents { | |
'tick': (time: number) => void; | |
'complete': () => void; | |
} | |
export interface Timer { | |
addListener<U extends keyof TimerEvents>(event: U, listener: TimerEvents[U]): this; | |
prependListener<U extends keyof TimerEvents>(event: U, listener: TimerEvents[U]): this; |
This file contains 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
declare global { | |
interface Array<T> { | |
/** | |
* Creates an tuple of grouped elements limited by the length of the first array. | |
* @template U | |
* @param {U} list | |
*/ | |
zip<U>(list: U[]): [T, U][] | |
} | |
} |
This file contains 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
/** | |
* Returns the values from array that are present in each of the other arrays | |
* | |
* @param {...unknown[]} arrays | |
* @returns {unknown[]} | |
*/ | |
export const intersection = (...arrays: unknown[]): unknown[] => { | |
return arrays.reduce((a, b) => a.filter((c: any) => b.includes(c))) | |
}; |
This file contains 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
/** | |
* Returns the values from array that are not present in the other arrays | |
* | |
* @param {unknown[]} first | |
* @param {unknown[]} second | |
* @returns {unknown[]} | |
*/ | |
export const difference = (first: unknown[], second: unknown[]): unknown[] => { | |
return first.filter(item => !second.includes(item)); | |
}; |
NewerOlder