dot-path-value
dot-path-value copied to clipboard
Added relative path get and set
I'm coming from a specific use case on my side where I have an initial path and want to manipulate the object with a relative path from the initial. After getting it working I thought it could be useful to include it on your package.
Explanation
We are going to start with this object:
const obj = {
a: {
b: 'hello',
d: [
{
e: 'world',
},
],
},
};
This PR is composed into two parts:
getByRelativePath(obj, path, relativePath)
- I know the initial path
'a.d.0' - I want to get the value that's relative to the initial path
../../b - I call
getByRelativePath(obj, 'a.d.0', '../../b') - It should be able to step back twice (
'../../'takes the context toa) and step forward once ('b'takes the context toa.b) - I'm expected to get
'hello'
setByRelativePath(obj, path, relativePath, value)
- I know the initial path
'a.d.0' - I want to set the value that's relative to the initial path
../../b - I call
setByRelativePath(obj, 'a.d.0', '../../b', 'good morning') - It should be able to step back twice (
'../../'takes the context toa) and step forward once ('b'takes the context toa.b) - I'm expected
obj.a.b === 'good morning'
Note: Take into account that I'm not well knowledgeable in typescript and learned a good deal with your code. Still I know there is some work to be done on this PR related with that.
Can I get some help on that maybe? :)