From 1d4fd6e7e3508382a52fcfe75d20ea1bfef46828 Mon Sep 17 00:00:00 2001 From: "Liew,Ching" Date: Tue, 21 Nov 2017 11:54:46 -0800 Subject: [PATCH 1/5] null check in nested key resolution --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5054e35..c3591f2 100644 --- a/src/index.js +++ b/src/index.js @@ -251,7 +251,7 @@ function getItemValues(item, key) { // handle nested keys value = key .split('.') - .reduce((itemObj, nestedKey) => itemObj[nestedKey], item) + .reduce((itemObj, nestedKey) => itemObj && itemObj[nestedKey] ? itemObj[nestedKey] : null, item) } else { value = item[key] } From af913fa624c9a1978c7a093deef0f245b605272e Mon Sep 17 00:00:00 2001 From: "Liew,Ching" Date: Tue, 21 Nov 2017 14:05:29 -0800 Subject: [PATCH 2/5] updating test --- src/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 5c60ee2..495bed2 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -105,7 +105,7 @@ const tests = { }, 'can handle objected with nested keys': { input: [ - [{name: {first: 'baz'}}, {name: {first: 'bat'}}, {name: {first: 'foo'}}], + [{name: {first: 'baz'}}, {name: {first: 'bat'}}, {}], 'ba', {keys: ['name.first']}, ], From 9678b22438822c215bfba0e7ff74dcac735e0b98 Mon Sep 17 00:00:00 2001 From: "Liew,Ching" Date: Tue, 21 Nov 2017 14:09:20 -0800 Subject: [PATCH 3/5] less code for same logic --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c3591f2..33baa30 100644 --- a/src/index.js +++ b/src/index.js @@ -251,7 +251,7 @@ function getItemValues(item, key) { // handle nested keys value = key .split('.') - .reduce((itemObj, nestedKey) => itemObj && itemObj[nestedKey] ? itemObj[nestedKey] : null, item) + .reduce((itemObj, nestedKey) => itemObj ? itemObj[nestedKey] : null, item) } else { value = item[key] } From 2c8a2cb3929d0529357397ab05d15960b2da4172 Mon Sep 17 00:00:00 2001 From: cliewpaypal <33815862+cliewpaypal@users.noreply.github.com> Date: Tue, 21 Nov 2017 14:17:56 -0800 Subject: [PATCH 4/5] more robust testcase --- src/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 495bed2..5719ca5 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -105,7 +105,7 @@ const tests = { }, 'can handle objected with nested keys': { input: [ - [{name: {first: 'baz'}}, {name: {first: 'bat'}}, {}], + [{name: {first: 'baz'}}, {name: {first: 'bat'}}, {name: {}}, {}], 'ba', {keys: ['name.first']}, ], From b02c4b9fddf534f244e229cfd6d3644a4c344f66 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Tue, 21 Nov 2017 15:22:07 -0700 Subject: [PATCH 5/5] improve test cases --- src/__tests__/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 5719ca5..ba549bd 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -105,7 +105,14 @@ const tests = { }, 'can handle objected with nested keys': { input: [ - [{name: {first: 'baz'}}, {name: {first: 'bat'}}, {name: {}}, {}], + [ + {name: {first: 'baz'}}, + {name: {first: 'bat'}}, + {name: {first: 'foo'}}, + {name: null}, + {}, + null, + ], 'ba', {keys: ['name.first']}, ],