Closed crapthings closed 3 years ago
better combine 'obj path' with _.index
deepMove(obj, '[0].a.[3]', '[1].b.[6]')
similar to deepRemove / deepCopy but all can action by path?
deepRemove(obj, '[0].a.[3]')
deepCopy(obj, '[0].a.[3]', '[1].b.[6]')
Let me please clarify, do you want this?
.set(obj, targetPath, .get(obj, sourcePath));
Or this?
.condenseDeep(.set(obj, targetPath, _.get(obj, sourcePath)));
Note, you should decide what will you do if target path already exists.
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.
Could you please describe your use cases?
Also how do you see this method should behave in case of:
var res = _.moveDeep({a:1,b:2}, 'a', 'b');
var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[0]');
var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[3]');
var res = _.moveDeep({a:1,b:[2]}, 'a', 'b[3][2].c');
var res = _.moveDeep({a: {b: {c: 1}}}, 'a', 'a.b.c');
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
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...
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.
Just faced with a similar use case and implemented like this:
fromPath
fromPath
toPath
points to object - put the value there. Developer cares about possible overwriting by himself.toPath
points to the array - insert value there, before possible existing array element, so the value will take a position by given index and possible consequent elements will be shifted right. If given index is greater then the length of the array - empty array slots will be preserved.fromPath
points to the array - optionally (by default) condense source array to fill the hole possibly left after deletion.fromPath
and toPath
are both pointing to the same array - then it means that by default target array will be condensed.fromPath
does not exist - undefined
value will be used without error similar to Lodash get
toPath
does not exist - it will be created similarly as it will be created by Lodash set
method.toPath
points to the depths of fromPath
- it is the same case when toPath
does not exist, because fromPath
will be deleted.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?
any possible to provide such feature?