ramda-adjunct icon indicating copy to clipboard operation
ramda-adjunct copied to clipboard

upsert

Open char0n opened this issue 5 years ago • 1 comments

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.

char0n avatar Oct 14 '19 07:10 char0n

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.

char0n avatar Dec 28 '19 10:12 char0n