mMapValues

mMapValues

  • (mapperFn: MapperFn) => (collection: EntryCollection) => collection
  1. Change each value of a collection

    Consider mapValues instead.
    This is for the uncommon case where mutating collection is required.

    const arr = [1, 2, 3]
    const inc = n => n + 1
    const incrementAll = mMapValues(inc)
    const mutatedArr = incrementAll(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3, 4]
    const arr = [1, 2, 3]
    const inc = (n: number) => n + 1
    const incrementAll = mMapValues(inc)
    const mutatedArr = incrementAll(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3, 4]
    
    const arr = [1, 2, 3]
    const inc = n => n + 1
    const incrementAll = mMapValues(inc)
    const mutatedArr = incrementAll(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3, 4]
    const arr = [1, 2, 3]
    const inc = (n: number) => n + 1
    const incrementAll = mMapValues(inc)
    const mutatedArr = incrementAll(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3, 4]
    
  2. Sometimes we want to change each value based on a function. Below, we have a 20% sale across all soccer products in our store. Let's map the old prices to the new ones.

    const soccerProducts = {
      soccerBall: 12.98,
      shinGuards: 9.94,
      cleats: 49.99,
    }
    
    const getSalePrice = compose([multiplyBy(0.8), roundToNearest('0.01')])
    const applySale = mMapValues(getSalePrice)
    applySale(soccerProducts)
    console.log(soccerProducts)
    // is {
    //   soccerBall: 10.38
    //   shinGuards: 7.95
    //   cleats: 39.99
    // }
    type Products = Record<string, number>
    const soccerProducts: Products = {
      soccerBall: 12.98,
      shinGuards: 9.94,
      cleats: 49.99,
    }
    
    const getSalePrice = compose([multiplyBy(0.8), roundToNearest('0.01')])
    const applySale = mMapValues(getSalePrice)<Products>
    applySale(soccerProducts)
    console.log(soccerProducts)
    // is {
    //   soccerBall: 10.38
    //   shinGuards: 7.95
    //   cleats: 39.99
    // }
    
    const soccerProducts = {
      soccerBall: 12.98,
      shinGuards: 9.94,
      cleats: 49.99,
    }
    
    const getSalePrice = compose([
      multiplyBy(0.8),
      roundToNearest('0.01'),
    ])
    const applySale = mMapValues(getSalePrice)
    applySale(soccerProducts)
    console.log(soccerProducts)
    // is {
    //   soccerBall: 10.38
    //   shinGuards: 7.95
    //   cleats: 39.99
    // }
    type Products = Record<string, number>
    const soccerProducts: Products = {
      soccerBall: 12.98,
      shinGuards: 9.94,
      cleats: 49.99,
    }
    
    const getSalePrice = compose([
      multiplyBy(0.8),
      roundToNearest('0.01'),
    ])
    const applySale = mMapValues(
      getSalePrice
    )<Products>
    applySale(soccerProducts)
    console.log(soccerProducts)
    // is {
    //   soccerBall: 10.38
    //   shinGuards: 7.95
    //   cleats: 39.99
    // }