ramda-adjunct
ramda-adjunct copied to clipboard
upsert
Is your feature request related to a problem? Please describe.
Polymorphic updating or inserting in one operation. We're using ad hoc polymorphism here.
Describe the solution you'd like
// working on objects
upsert('key', () => 'new value', () => 'initial value', {}); // {key: 'initial value'}
upsert('key', () => 'new value', () => 'initial value', {key: 'value'}); // {key: 'new value'}
// working on arrays
upsert(1, () => 'new value', () => 'initial value', []); // sparse array of [, 'initial value']
upsert(1, () => 'new value', () => 'initial value', ['first value', 'second vaue']); // ['first value', 'new value']
// working on maps
upsert('key', () => 'new value', () => 'initial value', new Map()); // Map({key: 'initial value'})
upsert('key', () => 'new value', () => 'initial value', new Map([['key', 'new value']])); // Map({key: 'new value'})
Describe alternatives you've considered
--
Additional context
TC39 Map.upsert proposal: https://github.com/tc39/proposal-upsert (stage 2 proposal). Our version of polymorhic upsert can work on multiple JavaScript types.
We discussed the viability of this function with TC39 proposal author and he agrees the function make sense if the library (R, RA) already support polymorphic functions.