Push?
Closed this issue · 9 comments
How would one push an item to an array with updeep? Or is this something that would be a good addition?
Hi @peteruithoven,
You could do this:
u({
children: children => [...children, newChild],
}, obj)
or es5:
u({
children: function(children) { return children + [newChild]; },
}, obj)
That said, I have thought about adding u.push
, u.shift
, u.pop
, u.unshift
, etc. I think they'd make good additions.
Clever! But yeah quite verbose.
I really like how you allow custom functions to be called, what do you think about finding a way to give them extra arguments? Something like:
const state = {
items: []
}
function add(items, item) {
return [...arr, item];
}
const updated = u({items: {[add,'foo']}}, state);
// or
const updated = u({items: {add:['foo']}}, state);
// or
const updated = u({items: {add:'foo'}}, state);
That's what currying is for:
const add = item => items => [...items, item];
const updated = u({items: add('foo')}, state);
@aaronjensen In the curried code, I'm assuming ...arr
should be ...items
, correct?
@mindjuice yep, copied code w/ a typo :)
Like!
My questions are more than answered, but if someone wants to implement methods like push it could stay open.
Great, I'll close this out. Thanks!
Btw, if you have further questions, feel free to ping me on https://discord.gg/0ZcbPKXt5bY7zvjh
Just a note to say +1 on u.push
, u.shift
, u.pop
, u.unshift
etc methods. At first glance I expected to see them in the API. (Love updeep btw.)