safe-obj

Safe-obj

Underscore helpers to make object accessors safe

I came (partly) from a Perl background where you could say $obj->{thing}->{another_thing}->{more_things}, and it would not blow up even if $obj was totally empty. Similarly, you could say $obj->{thing}->{foo} = 'bar', even if $obj had no property called 'thing'. I don't miss everything about Perl, but I do miss that. In javascript, you'd have to say:

Install

Usage

Server

As of v1.0.0, this module works with both lodash and underscore, though it requires using _.safe(obj, path) with lodash since _(obj).safe(path) triggers lodash's chaining.

safe-obj exports an object that can be mixed into underscore/lodash like this:

_.mixin(require('safe-obj'));

that let's you access object properties with wild abandon.

Client

Additionally, as of v1.0.0, it works on the client side. If window._ is defined (i.e. lodash or underscore has been loaded), _.safe will add an _safe property to it, which you can mix in with _.mixin(_._safe).