pKeepWhen

pKeepWhen

  • (predicate: AsyncPredicate) => async (collection: Collection) => Collection
  1. Keep entries that pass an asynchronous condition.

    The predicate is called for all entries at once. See Notes for an example.

    const gt2 = async n => n > 2
    const keepWhenGt2 = pKeepWhen(gt2)
    
    const arr = [4, 1, 3, 2]
    await keepWhenGt2(arr) // is [4, 3]
    
    const obj = { d: 4, a: 1, c: 3, b: 2 }
    await keepWhenGt2(obj) // is { d: 4, c: 3 }
    const gt2 = async (n: number) => n > 2
    const keepWhenGt2 = pKeepWhen(gt2)
    
    const arr = [4, 1, 3, 2]
    await keepWhenGt2(arr) // is [4, 3]
    
    const obj = { d: 4, a: 1, c: 3, b: 2 }
    await keepWhenGt2(obj) // is { d: 4, c: 3 }
    
    const gt2 = async n => n > 2
    const keepWhenGt2 = pKeepWhen(gt2)
    
    const arr = [4, 1, 3, 2]
    await keepWhenGt2(arr) // is [4, 3]
    
    const obj = { d: 4, a: 1, c: 3, b: 2 }
    await keepWhenGt2(obj) // is { d: 4, c: 3 }
    const gt2 = async (n: number) => n > 2
    const keepWhenGt2 = pKeepWhen(gt2)
    
    const arr = [4, 1, 3, 2]
    await keepWhenGt2(arr) // is [4, 3]
    
    const obj = { d: 4, a: 1, c: 3, b: 2 }
    await keepWhenGt2(obj) // is { d: 4, c: 3 }
    
  2. Sometimes we want to keep entries that pass an asynchronous condition. Below, we call a mock store API to see which items in our shopping cart give us rewards.

    const mockStoreApi = {
      includedInRewards: async item => item.name === 'detergent',
    }
    
    const shoppingCart = [
      { name: 'broom', cost: 3 },
      { name: 'mop', cost: 10 },
      { name: 'detergent', cost: 13 },
    ]
    const keepRewardItems = pKeepWhen(mockStoreApi.includedInRewards)
    
    const rewardItems = await keepRewardItems(shoppingCart)
    
    console.log('items gaining you rewards: ', rewardItems)
    // prints
    // items gaining you rewards:
    // [{
    //   name: detergent
    //   cost: 13
    // }]
    type Item = {
      name: string
      cost: number
    }
    
    const mockStoreApi = {
      includedInRewards: async (item: Item) => item.name === 'detergent',
    }
    
    const shoppingCart: Item[] = [
      { name: 'broom', cost: 3 },
      { name: 'mop', cost: 10 },
      { name: 'detergent', cost: 13 },
    ]
    const keepRewardItems = pKeepWhen(mockStoreApi.includedInRewards)<Item[]>
    
    const rewardItems = await keepRewardItems(shoppingCart)
    
    console.log('items gaining you rewards: ', rewardItems)
    // prints
    // items gaining you rewards:
    // [{
    //   name: detergent
    //   cost: 13
    // }]
    
    const mockStoreApi = {
      includedInRewards: async item =>
        item.name === 'detergent',
    }
    
    const shoppingCart = [
      { name: 'broom', cost: 3 },
      { name: 'mop', cost: 10 },
      { name: 'detergent', cost: 13 },
    ]
    const keepRewardItems = pKeepWhen(
      mockStoreApi.includedInRewards
    )
    
    const rewardItems =
      await keepRewardItems(shoppingCart)
    
    console.log(
      'items gaining you rewards: ',
      rewardItems
    )
    // prints
    // items gaining you rewards:
    // [{
    //   name: detergent
    //   cost: 13
    // }]
    type Item = {
      name: string
      cost: number
    }
    
    const mockStoreApi = {
      includedInRewards: async (item: Item) =>
        item.name === 'detergent',
    }
    
    const shoppingCart: Item[] = [
      { name: 'broom', cost: 3 },
      { name: 'mop', cost: 10 },
      { name: 'detergent', cost: 13 },
    ]
    const keepRewardItems = pKeepWhen(
      mockStoreApi.includedInRewards
    )<Item[]>
    
    const rewardItems =
      await keepRewardItems(shoppingCart)
    
    console.log(
      'items gaining you rewards: ',
      rewardItems
    )
    // prints
    // items gaining you rewards:
    // [{
    //   name: detergent
    //   cost: 13
    // }]
    
  3. pKeepWhen calls the predicate for all entries at once. See async.filterLimit if you want to limit the concurrency.

    const start = new Date().getTime()
    
    const gt2 = async n => {
      const msPassed = Date.now() - start
      console.log(`ms passed: ${msPassed}, for n = ${n}`)
    
      await pWaitMs(20)
      return n > 2
    }
    const keepWhenGt2 = pKeepWhen(gt2)
    
    await keepWhenGt2([2, 3, 4])
    const totalMsPassed = Date.now() - start
    console.log(`total ms passed: ${totalMsPassed}`)
    const start = new Date().getTime()
    
    const gt2 = async (n: number) => {
      const msPassed = Date.now() - start
      console.log(`ms passed: ${msPassed}, for n = ${n}`)
    
      await pWaitMs(20)
      return n > 2
    }
    const keepWhenGt2 = pKeepWhen(gt2)<number[]>
    
    await keepWhenGt2([2, 3, 4])
    const totalMsPassed = Date.now() - start
    console.log(`total ms passed: ${totalMsPassed}`)
    
    const start = new Date().getTime()
    
    const gt2 = async n => {
      const msPassed = Date.now() - start
      console.log(
        `ms passed: ${msPassed}, for n = ${n}`
      )
    
      await pWaitMs(20)
      return n > 2
    }
    const keepWhenGt2 = pKeepWhen(gt2)
    
    await keepWhenGt2([2, 3, 4])
    const totalMsPassed = Date.now() - start
    console.log(`total ms passed: ${totalMsPassed}`)
    const start = new Date().getTime()
    
    const gt2 = async (n: number) => {
      const msPassed = Date.now() - start
      console.log(
        `ms passed: ${msPassed}, for n = ${n}`
      )
    
      await pWaitMs(20)
      return n > 2
    }
    const keepWhenGt2 = pKeepWhen(gt2)<number[]>
    
    await keepWhenGt2([2, 3, 4])
    const totalMsPassed = Date.now() - start
    console.log(`total ms passed: ${totalMsPassed}`)