From d400c8de45709125fb2ca7f1408ba69d780cbcc9 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Wed, 8 Sep 2021 17:01:14 +1200 Subject: [PATCH] Return default value if path is invalid (#86) Co-authored-by: Sindre Sorhus --- index.js | 2 +- test.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e1f316f..3fe9b42 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ module.exports = { const pathArray = getPathSegments(path); if (pathArray.length === 0) { - return; + return value; } for (let i = 0; i < pathArray.length; i++) { diff --git a/test.js b/test.js index 0354205..a43c89e 100644 --- a/test.js +++ b/test.js @@ -213,3 +213,7 @@ test('prevent setting/getting `__proto__`', t => { t.is(dotProp.get({}, '__proto__'), undefined); }); + +test('return default value if path is invalid', t => { + t.is(dotProp.get({}, 'constructor', '🦄'), '🦄'); +});