fn-update
Functional updates for immutable objects
Why?
Because:
const makeYourPoint = ...extending stuff: 'can' be: ...stuffbe really: ...stuffbereally tedious: ...stuffbereallytedious especially: ...stuffbereallytediousespecially when: deeply: 'nested!' 'point was already made?': extendingstuffbereallytediousespeciallywhen && extendingstuffbereallytediousespecaiallywhendeeply === 'nested!'
Instead, how about this?
const makeYourPoint =
Sweet. What else?
fn-update
is cool (useful?) because:
- updates preserve identity(!), recursively - especially useful for libraries like
Redux
. No unnecessary re-renders. - the interface is flexible - update functions are decoupled from the structure they are updating (i.e. you do not need to supply updates in the same nested structure as the object you are updating). This is especially useful for manipulating normalized json structures
- all updates are curried functions... so you can pass them around, filter them, compose them, reuse them..
- special treatment of
delete
andrename
operations
Idiomatic use cases
By far the most valuable use-case for fn-update is transforming data.
For example:
- transforming state in a Redux reducer
- tweaking an API response in a promise chain
- reusing or combining transformations
// some illustrative examples: // 1. Transforming an api response with nested valuesconst toUpperCase = value // 2. Updating redux state in a reducer:// Instead of creating new objects every time, the updaters// only modify what's necessary. This likely means fewer renders!const reducer = actiontypeactionpayloadstate
Usage
const updateAt = const person = name:'Ben' preferences: whisky: region:'speyside' // Basic usage: Update a deeply nested valueconst shoutyBen = person // Constant values are applied as if they were liftedconst ambivalentBen = person // Missing keys along a path are created when setconst alcoholicBen = person // Each value in the path is passed to the updater function:const descriptiveBen =
const updateAtupdates = // Multiple `updateAt` calls can be composed with `updates`const oldAndBoringBen = person
const updateAtupdates = // `updates` can also take a pattern of updaters instead of a listconst oldAndBoringBen = person
// You can generate update functions any way you like. // All the functions here do the same thing: // This function: // is equivalent to: // is equivalent to: // is (almost) equivalent to:// ( this one always creates a new `prefs` object, whereas the others won't if prefs isn't materially changed ) // is equivalent to:
// Keys can be renamed or deletedconst updateAtops = const namelessBen = person const americanBen = person
// Array values can be updated too:const confusedSuperman = aliases: 'Clark Kent''Kal something' const superman = confusedSuperman // and deletedconst clarkKent = superman // although you could of course do the same thing with a filter...const alsoClarkKent = superman
// You can ignore null/undefined values by using 'nonNil'const makePersonOlder = updates const stillUndefined = const sillNull =