import { parallel, parallelMap } from 'libs/utils/promise' import { AlwaysTrueTypeGuard } from 'libs/utils/typescript' import { Asserter, AsserterP } from '../Asserter' export const assertMany = (isError: (e: unknown) => e is Err) => (values: Val[], assertOne: Asserter, assertAll: Asserter) => { const errors = values.reduce(function (errors, value) { try { assertOne(value) } catch (e) { if (isError(e)) { return errors.concat([e]) } else { throw e } } return errors }, []) try { assertAll(values) } catch (e) { if (isError(e)) { errors.push(e) } else { throw e } } if (errors.length) throw errors return values } export const enforceManyP = (isError: (e: unknown) => e is Err) => async (values: Val[], enforceOne: AsserterP, enforceAll: AsserterP) => { const one = async () => { await parallelMap(values, enforceOne) } const all = async () => { await parallelMap([values], enforceAll) } await parallel([ one(), all(), ]) return values } export const enforceManyAnyP = enforceManyP(AlwaysTrueTypeGuard)