gcanti / monocle-ts

Functional optics: a (partial) porting of Scala monocle
https://gcanti.github.io/monocle-ts/
MIT License
1.05k stars 52 forks source link

insertAt & renameAt #183

Open anthonyjoeseph opened 2 years ago

anthonyjoeseph commented 2 years ago

🚀 Feature request

As discussed in https://github.com/gcanti/fp-ts/issues/1460#issuecomment-824204261, adding insertAt and renameAt functions to all optics

Current Behavior

const user = { boss: { name: 'Anthony' } }
const newUser = { boss: { name: user.name, title: "manager" } }

const neighborhood = { house: { dog: 'Rex' } }
const newDog = { house: { cat: 'Rex' } }

Desired Behavior

const newUser = pipe(L.id<typeof user>(), L.prop('boss'), L.insertAt('title', "manager"))
// newUser: { boss: { name: string; title: string } }

const newDog = pipe(L.id<typeof neighborhood>(), L.prop('house'), L.renameAt('dog', "cat"))
// newDog: { house: { cat: string; } }

Suggested Solution

PLenses (as proposed here https://github.com/gcanti/monocle-ts/issues/59)

Who does this impact? Who is this for?

Users who want to modify the shape of their data

Describe alternatives you've considered

spectacles-ts as similar functionality, but I wanted to post here as a follow up on this comment from the fp-ts issue tracker https://github.com/gcanti/fp-ts/issues/1460#issuecomment-824204261

Additional context

the names 'insertAt' and 'renameAt' are analogs to existing functions in the ReadonlyRecord module of fp-ts