From 9a1631a614ef1fb6f36b037722f722e07be438a9 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Tue, 21 Feb 2017 14:17:39 +0000 Subject: [PATCH] fix: handle null as the root object --- lib/undefsafe.js | 5 +++++ test/undefsafe.test.js | 6 ++++++ 2 files changed, 11 insertions(+) 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;