import * as Constants from "./constants" export function unreachable( value: never, message = `No such case in exhaustive switch: ${value}` ) { throw new Error(message) } export async function parallelForeach( items: T[], task: (item: T) => Promise, maxConcurrency: number ): Promise { const promises: Promise[] = [] let index = 0 const processItem = async (item: T) => { try { await task(item) } finally { processNext() } } const processNext = () => { if (index >= items.length) { // No more items to process return } const item = items[index] index++ const promise = processItem(item) promises.push(promise) if (promises.length >= maxConcurrency) { Promise.race(promises).then(processNext) } else { processNext() } } processNext() await Promise.all(promises) } export function filterValueToLabel() { return Object.keys(Constants.OperatorOptions).reduce( (acc: { [key: string]: string }, key: string) => { const ops: { [key: string]: any } = Constants.OperatorOptions const op: { [key: string]: string } = ops[key] acc[op["value"]] = op.label return acc }, {} ) } export function hasSchema(test: any) { return ( typeof test === "object" && !Array.isArray(test) && test !== null && !(test instanceof Date) && Object.keys(test).length > 0 ) }