mariocasciaro / object-path

A tiny JavaScript utility to access deep properties using a path (for Node and the Browser)
MIT License
1.06k stars 84 forks source link

Would be nice if "has" could tell me where it fails #104

Open PAEz opened 4 years ago

PAEz commented 4 years ago

I figure there's two main ways you could do this...

Add a third argument to has. This argument accepts a reference to an array. If there is an error then this array is populated with a path that shows where it failed. The last item in the path is what property was undefined. So like.... object-path.has = function (object,path,error){ if (there was some error) error.length=0; error=[...invalid path]; } var lastError=[]; var has=oe.has(object, 'some,where', lastError); if (!has) have a look at lastError to see where it failed

The other way is to introduce a new property to object-path called something like lastError that will contain details about the last error it encounters. Every time a command on o-p is executed the prop is cleared and repopulated if an error occurs. This way is nice coz you can apply it to every function and put as much info as you want into the prop. I used this way in my EmitterObject (still working on it, dont judge me ;P ).... https://gist.github.com/PAEz/b6fc1687e4e963796f189d539d8d9d0c

vitaly-t commented 3 years ago

If there is any interest, following #107, I implemented my own value resolver, which has the last error index, among other things.