All files / src/common util.ts

0% Statements 0/23
0% Branches 0/2
0% Functions 0/8
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                                                                     
import { Maybe } from "./types"
 
export const removeKeyFromObj = <T> (
    obj: {[key: string]: T},
    key: string
  ): {[key: string]: T} => {
  const { [key]: omit, ...rest } = obj // eslint-disable-line
  return rest
}
 
export const updateOrRemoveKeyFromObj = <T> (
    obj: {[key: string]: T},
    key: string,
    val: Maybe<T>
  ): {[key: string]: T} => (
  val === null
    ? removeKeyFromObj(obj, key)
    : {
      ...obj,
      [key]: val
    }
)
 
export const mapObj = <T, S> (
    obj: {[key: string]: T},
    fn: (val: T, key: string) => S
  ): {[key: string]: S}  => {
  const newObj = {} as {[key: string]: S}
  Object.entries(obj).forEach(([key, value]) => {
    newObj[key] = fn(value, key)
  })
  return newObj
}
 
export const mapObjAsync = async <T, S> (
    obj: {[key: string]: T},
    fn: (val: T, key: string) => Promise<S>
  ): Promise<{[key: string]: S}> => {
  const newObj = {} as {[key: string]: S}
  await Promise.all(
    Object.entries(obj).map(async ([key, value]) => {
      newObj[key] = await fn(value, key)
    })
  )
  return newObj
}
 
export const arrContains = <T>(arr: T[], val: T): boolean => {
  return arr.indexOf(val) > -1
}
 
export const asyncWaterfall = async <T>(val: T, operations: ((val: T) => Promise<T>)[]): Promise<T> => {
  let acc = val
  for(let i=0; i<operations.length; i++){
    acc = await operations[i](acc)
  }
  return acc
}