diff --git a/lib/undefsafe.js b/lib/undefsafe.js index 07b91d9..52db639 100644 --- a/lib/undefsafe.js +++ b/lib/undefsafe.js @@ -40,6 +40,11 @@ function undefsafe(obj, path, value) { return res; } + // bail if there's nothing + if (obj === undefined || obj === null) { + return undefined; + } + var parts = split(path); var key = null; var type = typeof obj; diff --git a/test/undefsafe.test.js b/test/undefsafe.test.js index c5b6470..49f29b0 100644 --- a/test/undefsafe.test.js +++ b/test/undefsafe.test.js @@ -8,6 +8,12 @@ test('should handle primatives', function (t) { t.end(); }); +test('should handle null', function (t) { + var r = undefsafe(null, 'foo'); + t.equal(r, undefined, 'undefsafe works with null'); + t.end(); +}); + test('should handle empty objects', function (t) { var value = {}; var r;