update
update
(mapperFns: MapperFns) => (collection: EntryCollection) => EntryCollection
Update matching entries via mapper functions
const obj = { a: 1, b: 2, c: 3 } const inc = n => n + 1 const dec = n => n - 1 const updateAandC = update({ a: inc, c: dec }) const updatedObj = updateAandC(obj) console.log(updatedObj) // is { // a: 2 // b: 2 // c: 2 // }
const obj = { a: 1, b: 2, c: 3 } const inc = (n: number) => n + 1 const dec = (n: number) => n - 1 const updateAandC = update({ a: inc, c: dec }) const updatedObj = updateAandC(obj) console.log(updatedObj) // is { // a: 2 // b: 2 // c: 2 // }
const obj = { a: 1, b: 2, c: 3 } const inc = n => n + 1 const dec = n => n - 1 const updateAandC = update({ a: inc, c: dec }) const updatedObj = updateAandC(obj) console.log(updatedObj) // is { // a: 2 // b: 2 // c: 2 // }
const obj = { a: 1, b: 2, c: 3 } const inc = (n: number) => n + 1 const dec = (n: number) => n - 1 const updateAandC = update({ a: inc, c: dec }) const updatedObj = updateAandC(obj) console.log(updatedObj) // is { // a: 2 // b: 2 // c: 2 // }
Sometimes we want to update specific entries via functions. Below, we are coding a gifting feature on our audiobook platform. Let's test it by gifting Cat's Cradle to Luke.
const friend = { name: 'luke', books: ['Player Piano'], } const addCatsCradle = appendOne("Cat's Cradle") const giftCatsCradleTo = update({ books: addCatsCradle }) const updatedFriend = giftCatsCradleTo(friend) console.log(updatedFriend) // is { // name: luke // books: [Player Piano, Cat's Cradle] // }
type Account = { name: string books: string[] } const friend: Account = { name: 'luke', books: ['Player Piano'], } const addCatsCradle = appendOne("Cat's Cradle") const giftCatsCradleTo = update({ books: addCatsCradle })<Account> const updatedFriend = giftCatsCradleTo(friend) console.log(updatedFriend) // is { // name: luke // cart: [Player Piano, Cat's Cradle] // }
const friend = { name: 'luke', books: ['Player Piano'], } const addCatsCradle = appendOne("Cat's Cradle") const giftCatsCradleTo = update({ books: addCatsCradle, }) const updatedFriend = giftCatsCradleTo(friend) console.log(updatedFriend) // is { // name: luke // books: [Player Piano, Cat's Cradle] // }
type Account = { name: string books: string[] } const friend: Account = { name: 'luke', books: ['Player Piano'], } const addCatsCradle = appendOne("Cat's Cradle") const giftCatsCradleTo = update({ books: addCatsCradle, })<Account> const updatedFriend = giftCatsCradleTo(friend) console.log(updatedFriend) // is { // name: luke // cart: [Player Piano, Cat's Cradle] // }
An array or map of
mapperFns
can only update a respective array or map. An object, on the other hand, can update any EntryCollection.Below shows the array validation.
const inc = n => n + 1 const dec = n => n - 1 const updateFirstTwo = update([inc, dec]) const arr = [1, 5, 3] const updatedArr = updateFirstTwo(arr) console.log(updatedArr) // is [2, 4, 3] const obj = { a: 1 } try { updateFirstTwo(obj) } catch (err) { console.log(err) // prints // update requires argument 'collection' to be an array since you passed an // array for 'mapperFns' }
const inc = (n: number) => n + 1 const dec = (n: number) => n - 1 const updateFirstTwo = update([inc, dec]) const arr = [1, 5, 3] const updatedArr = updateFirstTwo(arr) console.log(updatedArr) // is [2, 4, 3] const obj = { a: 1 } updateFirstTwo(obj) // ts requires an array
const inc = n => n + 1 const dec = n => n - 1 const updateFirstTwo = update([inc, dec]) const arr = [1, 5, 3] const updatedArr = updateFirstTwo(arr) console.log(updatedArr) // is [2, 4, 3] const obj = { a: 1 } try { updateFirstTwo(obj) } catch (err) { console.log(err) // prints // update requires argument 'collection' to be // an array since you passed an array for // 'mapperFns' }
const inc = (n: number) => n + 1 const dec = (n: number) => n - 1 const updateFirstTwo = update([inc, dec]) const arr = [1, 5, 3] const updatedArr = updateFirstTwo(arr) console.log(updatedArr) // is [2, 4, 3] const obj = { a: 1 } updateFirstTwo(obj) // ts requires an array