import {delay, isNil, isUndefined} from 'lodash-es' export interface DelayHandler { (...args: TArgs): TResult | void clear: () => void getId: () => number updateDelayTime: (delayInMS?: number) => void } export function createDelayHandler( func: (...args: TArgs) => TResult, delayInMS?: number ): DelayHandler { let timer: number let delayMS: number function clear() { if (timer) { clearTimeout(timer) timer = 0 } } function delayHandler(...args: typeof func.arguments) { clear() if (delayMS) { timer = delay(func, delayMS, args) } else { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return func(...args) } return undefined } function getId() { return timer } function updateDelayTime(newDelayInMS?: number) { delayMS = !newDelayInMS || newDelayInMS < 0 ? 0 : newDelayInMS } updateDelayTime(delayInMS) delayHandler.clear = clear delayHandler.getId = getId delayHandler.updateDelayTime = updateDelayTime return delayHandler } export function safeInvoke( func: ((...arg: TArgs) => TResult) | undefined | null, ...arg: TArgs ) { if (func && typeof func === 'function') { return func(...arg) } return undefined } export function toggleSelectedList( id: T, list: T[], { multiple = true, onSelect, onDeselect, }: { multiple?: boolean onSelect?: (id: T, nextList: T[], currentList: T[]) => void onDeselect?: (id: T, nextList: T[], currentList: T[]) => void } = {} ) { const index = list.indexOf(id) let nextList = list.slice() let onChange if (index > -1) { nextList.splice(index, 1) onChange = onDeselect } else { onChange = onSelect if (multiple) { nextList.push(id) } else { nextList = [id] } } safeInvoke(onChange, id, nextList, list) return nextList } export function hasOwnProperty(obj: unknown, propName: string) { return Object.prototype.hasOwnProperty.call(obj, propName) } export function addThousandSeparator(num: number | string, separator = ',') { if (isNil(num)) { return '' } const number = Number(num) if (!separator || (typeof num !== 'string' && isNaN(number)) || (typeof num === 'number' && !isFinite(number)) ) { return `${num}` } const [integer, decimals] = `${num}`.split('.') const int = integer.replace(/\B(?=(\d{3})+(?!\d))/g, separator) return isUndefined(decimals) ? '' + int : int + '.' + decimals } export function createRandomId(prefix = '') { const random = (`${Math.random()}`).replace('0.', '') return prefix + parseInt(random, 10).toString(36) } export function isPromiseLike(maybePromise: unknown): maybePromise is Promise { if (!maybePromise) { return false } const promise = maybePromise as Promise return (typeof promise === 'object' || typeof promise === 'function') && typeof promise.then === 'function' && typeof promise.finally === 'function' } export function isPromise(maybePromise: unknown): maybePromise is Promise { try { return maybePromise instanceof Promise } catch (e) { return isPromiseLike(maybePromise) } } export function isWindows() { return /windows/i.test(window.navigator.userAgent) } export function isMac() { return /mac/i.test(window.navigator.userAgent) } export async function waitTimeout(timeout = 200) { return new Promise(resolve => { setTimeout(resolve, timeout) /* istanbul ignore if */ if (jest.isMockFunction(setTimeout)) { jest.advanceTimersByTime(timeout) jest.runOnlyPendingTimers() } }) }