Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


The 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.


Yep, that's one reason for the array version.


Why 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.


> 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.


I must have missed that in the docs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: