YuriGor / deepdash

eachDeep, filterDeep, findDeep, someDeep, omitDeep, pickDeep, keysDeep etc.. Tree traversal library written in Underscore/Lodash fashion
https://deepdash.io/
MIT License
275 stars 12 forks source link

deepMove item of array into another path from same obj/array #23

Closed crapthings closed 3 years ago

crapthings commented 5 years ago

any possible to provide such feature?

crapthings commented 5 years ago

better combine 'obj path' with _.index

deepMove(obj, '[0].a.[3]', '[1].b.[6]')

crapthings commented 5 years ago

similar to deepRemove / deepCopy but all can action by path?

deepRemove(obj, '[0].a.[3]')

deepCopy(obj, '[0].a.[3]', '[1].b.[6]')

YuriGor commented 5 years ago

Let me please clarify, do you want this?

.set(obj, targetPath, .get(obj, sourcePath));

YuriGor commented 5 years ago

Or this?

.condenseDeep(.set(obj, targetPath, _.get(obj, sourcePath)));

Note, you should decide what will you do if target path already exists.

crapthings commented 5 years ago

https://github.com/rhalff/dot-object#move-a-property-within-one-object-to-another-location

something like this, but it lacks array support i think.

YuriGor commented 5 years ago

Could you please describe your use cases?

Also how do you see this method should behave in case of:

  1. target is object and property exists already by the given path
    var res = _.moveDeep({a:1,b:2}, 'a', 'b');
  2. target is array and item exists already by the given path
    var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[0]');
  3. target is an array and target path points to the not allocated area of array
    var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[3]');
  4. target path go through the not allocated area of some parent array
    var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[3][2].c');
  5. target path points to the child
    var res = _.moveDeep({a: {b: {c: 1}}}, 'a', 'a.b.c');
crapthings commented 5 years ago

maybe move a card from kanban?

the data structure is like

[
  {
    randomid,
    type: 'deck',
    children: [
      {
        randomid,
        type: 'group',
        children: [
          {
            randomid,
            type: 'card',
            name: 'card 1',
          },

          {
            randomid,
            type: 'card',
            name: 'card 2',
          },

          {
            randomid,
            type: 'card',
            name: 'card 3',
          },
        ]
      },

      {
        randomid,
        type: 'card',
        name: 'card 7'
      }
    ]
  },

  {
    randomid,
    type: 'deck',
    children: [
      {
        randomid,
        type: 'card',
        name: 'card 4',
      },

      {
        randomid,
        type: 'card',
        name: 'card 5',
      },

      {
        randomid,
        type: 'card',
        name: 'card 6',
      },
    ]
  },
]

like move 'card 5' into 0.children.1.children.2

crapthings commented 5 years ago

these deep method is powerful

https://github.com/YuriGor/deepdash#mapdeep https://github.com/YuriGor/deepdash#index

when you use these with react, mobx

you render the tree deeply and mutate mobx state every thing change,

but sometimes we need something like swap, move, remove kv pair from nested obj or remove element from nested array etc...

YuriGor commented 5 years ago

I think you requested too complicated method with unclear specs. Please think how would you split it Into separate simpler methods. As for me I'd prefer to do such things manually using lodash get, set and native array methods. If you have some repeating usage patterns - it's better to create helper module, or even a separate package related to specific mobx tasks. Thank you for interest, please feel free to open new requests.

YuriGor commented 4 years ago

Just faced with a similar use case and implemented like this:

So I am going to implement moveDeep as described above. @crapthings, @FunkySamuel37 could you please review the suggested algorithm, if it fits your practice and covers possible edge cases?