4 ms·
http://agave.js http://agave.js includes (prefix)getPath by default on any object. var mockObject = { foo: 'bar', baz: { bam:'boo',
by nailer 13y ago
http://agave.js http://agave.js includes (prefix)getPath by default on any object.
var mockObject = {
foo: 'bar',
baz: {
bam:'boo',
zar:{
zog:'something useful'
}
}
}
So:
mockObject.getPath('/baz/zar/zog')
or, alternatively:
mockObject.getPath(['baz','zar','zog'])
will return:
'something useful'
It's also got a bunch of other useful stuff like 'kind' (closest prototype of an object, that works consistently everywhere), number methods like (2).weeks().ago(), and reads more cleanly than underscore as it uses actual methods.
- randallsquared 13y agoThe main problem with string-based paths is that the keys are just a string, so an object with slashes embedded in the keys is perfectly valid. So, ambiguity.
- nailer 13y agoYep, that's one reason for the array version.
- blktiger 13y agoWhy would you use mockObject.getPath('/baz/zar/zog') over mockObject.baz.zar.zog? Unless getPath is null safe and you expect that sometimes part of the chain doesn't exist, but the docs for getPath don't indicate that as a feature.
- nailer 13y ago> Unless getPath is null safe and you expect that sometimes part of the chain doesn't exist Yes, that's exactly why. > the docs for getPath don't indicate that as a feature. The docs currently state 'If any of the keys are missing, return undefined. ' Perhaps they should be more explicit? If so I'm happy to take suggestions / pull requests.
- blktiger 13y agoI must have missed that in the docs.