101 icon indicating copy to clipboard operation
101 copied to clipboard

Deep `put`

Open srph opened this issue 7 years ago • 11 comments

Currently put [immutably] only sets the value to the object path. I was wondering if it would be possible to merge the new object to the current.

Sample Data

var user = {
  name: 'Brian Mae S. Bodollo',
  mother: {
    name: 'Marian Rivera',

    mother: {
      name: 'Claire Borromeo',

      daughters: [{
        name: 'Marian Rivera' 
      }]
    }
  },
};

Current

var updated = put(user, {
  'mother.mother.daughters.0.age': 5,
  'mother.mother.daughters.0.height': 6,
  'mother.mother.daughters.0.width': 6
});

Suggestion:

var updated = put(user, 'mother.mother.daughters.0', {
  age: 5,
  height: 6,
  width: 6
});

Maybe we could have a new method (named update) instead of updating the functionality of put?

Let me know if I missed out anything. Thanks!

srph avatar Aug 25 '16 21:08 srph

Quick reply for now. I recently fixed a bug with put. I think the example you provided should work as expected with the latest version of 101.

Try it out and see if the problem remains:

'npm i --save 101@latest'

Lmk, Typed using my thumbs..

On Aug 25, 2016, at 2:24 PM, Kier Borromeo [email protected] wrote:

Currently put [immutably] only sets the value to the object path. I was wondering if it would be possible to merge the new object to the current.

Sample Data

var user = { name: 'Brian Mae S. Bodollo', mother: { name: 'Marian Rivera',

mother: {
  name: 'Claire Borromeo',

  daughters: [{
    name: 'Marian Rivera' 
  }]
}

}, }; Current

var updated = put(user, { 'mother.mother.daughters.0.age': 5, 'mother.mother.daughters.0.height': 6, 'mother.mother.daughters.0.width': 6 }); Suggestion:

var updated = put(user, 'mother.mother.daughters.0', { age: 5, height: 6, width: 6 }); Maybe we could have a new method (named update) instead of updating the functionality of put?

Let me know if I missed out anything. Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

tjmehta avatar Aug 25 '16 22:08 tjmehta

Thanks for the quick reply! <3

Yea, I'm using the latest version. It doesn't seem to be fixed 😕 (Demo).

For clarification, the end result (to the object mentioned previously) should be:

console.log(user.mother.mother.daughters[0], updated.mother.mother.daughters[0]);
// { name: 'Immaculate Borromeo' }
// { name: 'Immaculate Borromeo', age: 5, height: 6, width: 6 }

Moreover, I didn't find any test case about this scenario.

Let me know where I can help :)

srph avatar Aug 26 '16 03:08 srph

I see what you are saying now. Sorry, looked at that too quick on my phone. Let me think about this a bit. Would keypather.flatten be sufficient for your needs?:

var keypather = require('keypather')()
var put = require('101/put')

var update = keypather.flatten({
  age: 5,
  height: 6,
  width: 6
}, '.', 'mother.mother.daughters.0')
put(user, update)

tjmehta avatar Aug 26 '16 03:08 tjmehta

Yea. But to simplify even more, I was wondering if we could achieve the ff:

var update = require('101/update');

var updated = update(user, 'mother.mother.daughters.0', {
  age: 5,
  height: 6,
  width: 6
});

Similar to put but merges the object :D

srph avatar Aug 26 '16 19:08 srph

Could we extend '101/assign'?

tjmehta avatar Aug 26 '16 21:08 tjmehta

Assign current only supports a depth of 1

tjmehta avatar Aug 26 '16 21:08 tjmehta

And doesn't support keypaths

tjmehta avatar Aug 26 '16 21:08 tjmehta

I think we could do that, but I think it could complicate assign in both functionality and API. IMO, I would prefer a new function like update :D.

srph avatar Aug 27 '16 05:08 srph

The plan for assign is to possibly deprecate it in v2.0.0.

Actually to get this in before then how about '101/extend'. I am traveling this week, but happy to take a PR.

Typed using my thumbs..

On Aug 26, 2016, at 10:26 PM, Kier Borromeo [email protected] wrote:

I think we could do that, but I think it could complicate assign in both functionality and API. IMO, I would prefer a new function like update :D.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

tjmehta avatar Aug 27 '16 05:08 tjmehta

Oh, how about we name it merge? _.merge is like _.assign but recursively merges the object. However, _.merge mutates the original object.

srph avatar Aug 27 '16 06:08 srph

👍

Typed using my thumbs..

On Aug 27, 2016, at 1:25 AM, Kier Borromeo [email protected] wrote:

Oh, how about we name it merge? _.merge is like _.assign but recursively merges the object.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

tjmehta avatar Aug 27 '16 10:08 tjmehta