keep

keep

  • (values: ValueCollection) => (collection: Collection) => Collection
  1. Keep all values in the collection.

    const keepOnesAndTwos = keep([1, 2])
    
    keepOnesAndTwos([1, 2, 3, 4, 1]) // is [1, 2, 1]
    const keepOnesAndTwos = keep([1, 2])
    
    keepOnesAndTwos([1, 2, 3, 4, 1]) // is [1, 2, 1]
    
    const keepOnesAndTwos = keep([1, 2])
    
    keepOnesAndTwos([1, 2, 3, 4, 1]) // is [1, 2, 1]
    const keepOnesAndTwos = keep([1, 2])
    
    keepOnesAndTwos([1, 2, 3, 4, 1]) // is [1, 2, 1]
    
  2. Sometimes we only want certain values. Below, we have some weekly volunteers at the cat shelter. The weekend helpers' jobs are especially difficult, so let's list their names for "thank you" cards.

    const volunteers = {
      jason: 'saturday',
      amy: 'wednesday',
      kim: 'saturday',
      grace: 'tuesday',
      sam: 'sunday',
    }
    
    const keepWeekendHelpers = keep(['saturday', 'sunday'])
    const getWeekendHelpers = compose([keepWeekendHelpers, Object.keys])
    
    const weekendHelpers = getWeekendHelpers(volunteers)
    console.log(weekendHelpers)
    // is [
    //  jason
    //  kim
    //  sam
    // ]
    type Volunteers = Record<string, string>
    const volunteers = {
      jason: 'saturday',
      amy: 'wednesday',
      kim: 'saturday',
      grace: 'tuesday',
      sam: 'sunday',
    }
    
    const keepWeekendHelpers = keep(['saturday', 'sunday'])<Volunteers>
    const getWeekendHelpers = compose([keepWeekendHelpers, Object.keys])
    
    const weekendHelpers = getWeekendHelpers(volunteers)
    console.log(weekendHelpers)
    // is [
    //  jason
    //  kim
    //  sam
    // ]
    
    const volunteers = {
      jason: 'saturday',
      amy: 'wednesday',
      kim: 'saturday',
      grace: 'tuesday',
      sam: 'sunday',
    }
    
    const keepWeekendHelpers = keep([
      'saturday',
      'sunday',
    ])
    const getWeekendHelpers = compose([
      keepWeekendHelpers,
      Object.keys,
    ])
    
    const weekendHelpers =
      getWeekendHelpers(volunteers)
    console.log(weekendHelpers)
    // is [
    //  jason
    //  kim
    //  sam
    // ]
    type Volunteers = Record<string, string>
    const volunteers = {
      jason: 'saturday',
      amy: 'wednesday',
      kim: 'saturday',
      grace: 'tuesday',
      sam: 'sunday',
    }
    
    const keepWeekendHelpers = keep([
      'saturday',
      'sunday',
    ])<Volunteers>
    const getWeekendHelpers = compose([
      keepWeekendHelpers,
      Object.keys,
    ])
    
    const weekendHelpers =
      getWeekendHelpers(volunteers)
    console.log(weekendHelpers)
    // is [
    //  jason
    //  kim
    //  sam
    // ]