From 3d7f5582c7295b9b5a365e5c485bd709550f96bb Mon Sep 17 00:00:00 2001 From: Brysen LaBute Date: Mon, 24 Oct 2022 15:13:19 +0000 Subject: [PATCH] fix: add null check before accessing props This corrects null reference error when a child is null/undefined (which is valid). Closes 18. --- README.md | 124 +++++++++--------- dist/README.md | 124 +++++++++--------- src/getChild/index.deep.test.js | 2 +- src/getChild/index.ts | 2 +- src/getChildByType/index.deep.test.js | 2 +- src/getChildByType/index.test.js | 2 +- src/getChildren/index.deep.test.js | 4 +- src/getChildren/index.ts | 2 +- src/getChildrenByType/index.deep.test.js | 4 +- src/getChildrenByType/index.ts | 2 +- src/getChildrenWithDescendant/index.test.js | 4 +- .../index.test.js | 2 +- src/getDescendantDepth/index.test.js | 4 +- src/getDescendantDepth/index.ts | 2 +- src/getDescendantDepthByType/index.test.js | 4 +- src/getDescendantDepthByType/index.ts | 2 +- src/overrideProps/index.test.js | 4 +- src/removeChildren/index.deep.test.js | 6 +- src/removeChildren/index.ts | 2 +- src/removeChildrenByType/index.deep.test.js | 12 +- src/removeChildrenByType/index.ts | 2 +- 21 files changed, 168 insertions(+), 144 deletions(-) diff --git a/README.md b/README.md index 92cdb9b..9abeddd 100644 --- a/README.md +++ b/README.md @@ -163,97 +163,97 @@ CHANGELOG.md -- history of changes to the module README.md -- this file /lib └───/es5 + └───/_private + └───utils.d.ts - 60 Bytes + └───utils.js - 890 Bytes └───/getChild - └───index.d.ts - 1.23 KB - └───index.js - 1.81 KB + └───index.d.ts - 1.2 KB + └───index.js - 1.82 KB └───/getChildByType - └───index.d.ts - 4.16 KB - └───index.js - 6.08 KB + └───index.d.ts - 4.09 KB + └───index.js - 5.97 KB └───/getChildren - └───index.d.ts - 1.21 KB + └───index.d.ts - 1.19 KB └───index.js - 2.07 KB └───/getChildrenByType - └───index.d.ts - 3.86 KB - └───index.js - 5.41 KB + └───index.d.ts - 3.8 KB + └───index.js - 5.36 KB └───/getChildrenWithDescendant - └───index.d.ts - 638 Bytes - └───index.js - 1.31 KB + └───index.d.ts - 626 Bytes + └───index.js - 1.28 KB └───/getChildrenWithDescendantByType - └───index.d.ts - 2.26 KB - └───index.js - 3.02 KB + └───index.d.ts - 2.22 KB + └───index.js - 2.96 KB └───/getDescendantDepth - └───index.d.ts - 1.14 KB - └───index.js - 2.46 KB + └───index.d.ts - 1.12 KB + └───index.js - 2.45 KB └───/getDescendantDepthByType - └───index.d.ts - 2.39 KB - └───index.js - 3.93 KB - └───index.d.ts - 1.1 KB - └───index.js - 4.23 KB + └───index.d.ts - 2.35 KB + └───index.js - 3.91 KB + └───index.d.ts - 1.08 KB + └───index.js - 4.19 KB └───/noEmptyChildren - └───index.d.ts - 1.75 KB - └───index.js - 3.43 KB + └───index.d.ts - 1.72 KB + └───index.js - 3.37 KB └───/overrideProps - └───index.d.ts - 2.72 KB - └───index.js - 4.66 KB + └───index.d.ts - 2.68 KB + └───index.js - 4.57 KB └───/removeChildren - └───index.d.ts - 1.22 KB - └───index.js - 2.57 KB + └───index.d.ts - 1.2 KB + └───index.js - 2.56 KB └───/removeChildrenByType - └───index.d.ts - 3.71 KB - └───index.js - 5.63 KB + └───index.d.ts - 3.65 KB + └───index.js - 5.58 KB └───/typeOfComponent - └───index.d.ts - 614 Bytes - └───index.js - 1.75 KB - └───types.d.ts - 249 Bytes - └───types.js - 79 Bytes - └───/_private - └───utils.d.ts - 61 Bytes - └───utils.js - 913 Bytes + └───index.d.ts - 603 Bytes + └───index.js - 1.72 KB + └───types.d.ts - 240 Bytes + └───types.js - 77 Bytes └───/es6 + └───/_private + └───utils.d.ts - 60 Bytes + └───utils.js - 681 Bytes └───/getChild - └───index.d.ts - 1.23 KB + └───index.d.ts - 1.2 KB └───index.js - 1.63 KB └───/getChildByType - └───index.d.ts - 4.16 KB - └───index.js - 5.78 KB + └───index.d.ts - 4.09 KB + └───index.js - 5.68 KB └───/getChildren - └───index.d.ts - 1.21 KB + └───index.d.ts - 1.19 KB └───index.js - 1.87 KB └───/getChildrenByType - └───index.d.ts - 3.86 KB - └───index.js - 5.12 KB + └───index.d.ts - 3.8 KB + └───index.js - 5.08 KB └───/getChildrenWithDescendant - └───index.d.ts - 638 Bytes - └───index.js - 1.13 KB + └───index.d.ts - 626 Bytes + └───index.js - 1.1 KB └───/getChildrenWithDescendantByType - └───index.d.ts - 2.26 KB - └───index.js - 2.81 KB + └───index.d.ts - 2.22 KB + └───index.js - 2.76 KB └───/getDescendantDepth - └───index.d.ts - 1.14 KB + └───index.d.ts - 1.12 KB └───index.js - 2.3 KB └───/getDescendantDepthByType - └───index.d.ts - 2.39 KB - └───index.js - 3.74 KB - └───index.d.ts - 1.1 KB - └───index.js - 905 Bytes + └───index.d.ts - 2.35 KB + └───index.js - 3.72 KB + └───index.d.ts - 1.08 KB + └───index.js - 892 Bytes └───/noEmptyChildren - └───index.d.ts - 1.75 KB - └───index.js - 3.2 KB + └───index.d.ts - 1.72 KB + └───index.js - 3.15 KB └───/overrideProps - └───index.d.ts - 2.72 KB - └───index.js - 4.44 KB + └───index.d.ts - 2.68 KB + └───index.js - 4.36 KB └───/removeChildren - └───index.d.ts - 1.22 KB - └───index.js - 2.34 KB + └───index.d.ts - 1.2 KB + └───index.js - 2.33 KB └───/removeChildrenByType - └───index.d.ts - 3.71 KB - └───index.js - 5.32 KB + └───index.d.ts - 3.65 KB + └───index.js - 5.28 KB └───/typeOfComponent - └───index.d.ts - 614 Bytes - └───index.js - 1.6 KB - └───types.d.ts - 249 Bytes - └───types.js - 12 Bytes - └───/_private - └───utils.d.ts - 61 Bytes - └───utils.js - 700 Bytes + └───index.d.ts - 603 Bytes + └───index.js - 1.58 KB + └───types.d.ts - 240 Bytes + └───types.js - 11 Bytes ```` \ No newline at end of file diff --git a/dist/README.md b/dist/README.md index 92cdb9b..9abeddd 100644 --- a/dist/README.md +++ b/dist/README.md @@ -163,97 +163,97 @@ CHANGELOG.md -- history of changes to the module README.md -- this file /lib └───/es5 + └───/_private + └───utils.d.ts - 60 Bytes + └───utils.js - 890 Bytes └───/getChild - └───index.d.ts - 1.23 KB - └───index.js - 1.81 KB + └───index.d.ts - 1.2 KB + └───index.js - 1.82 KB └───/getChildByType - └───index.d.ts - 4.16 KB - └───index.js - 6.08 KB + └───index.d.ts - 4.09 KB + └───index.js - 5.97 KB └───/getChildren - └───index.d.ts - 1.21 KB + └───index.d.ts - 1.19 KB └───index.js - 2.07 KB └───/getChildrenByType - └───index.d.ts - 3.86 KB - └───index.js - 5.41 KB + └───index.d.ts - 3.8 KB + └───index.js - 5.36 KB └───/getChildrenWithDescendant - └───index.d.ts - 638 Bytes - └───index.js - 1.31 KB + └───index.d.ts - 626 Bytes + └───index.js - 1.28 KB └───/getChildrenWithDescendantByType - └───index.d.ts - 2.26 KB - └───index.js - 3.02 KB + └───index.d.ts - 2.22 KB + └───index.js - 2.96 KB └───/getDescendantDepth - └───index.d.ts - 1.14 KB - └───index.js - 2.46 KB + └───index.d.ts - 1.12 KB + └───index.js - 2.45 KB └───/getDescendantDepthByType - └───index.d.ts - 2.39 KB - └───index.js - 3.93 KB - └───index.d.ts - 1.1 KB - └───index.js - 4.23 KB + └───index.d.ts - 2.35 KB + └───index.js - 3.91 KB + └───index.d.ts - 1.08 KB + └───index.js - 4.19 KB └───/noEmptyChildren - └───index.d.ts - 1.75 KB - └───index.js - 3.43 KB + └───index.d.ts - 1.72 KB + └───index.js - 3.37 KB └───/overrideProps - └───index.d.ts - 2.72 KB - └───index.js - 4.66 KB + └───index.d.ts - 2.68 KB + └───index.js - 4.57 KB └───/removeChildren - └───index.d.ts - 1.22 KB - └───index.js - 2.57 KB + └───index.d.ts - 1.2 KB + └───index.js - 2.56 KB └───/removeChildrenByType - └───index.d.ts - 3.71 KB - └───index.js - 5.63 KB + └───index.d.ts - 3.65 KB + └───index.js - 5.58 KB └───/typeOfComponent - └───index.d.ts - 614 Bytes - └───index.js - 1.75 KB - └───types.d.ts - 249 Bytes - └───types.js - 79 Bytes - └───/_private - └───utils.d.ts - 61 Bytes - └───utils.js - 913 Bytes + └───index.d.ts - 603 Bytes + └───index.js - 1.72 KB + └───types.d.ts - 240 Bytes + └───types.js - 77 Bytes └───/es6 + └───/_private + └───utils.d.ts - 60 Bytes + └───utils.js - 681 Bytes └───/getChild - └───index.d.ts - 1.23 KB + └───index.d.ts - 1.2 KB └───index.js - 1.63 KB └───/getChildByType - └───index.d.ts - 4.16 KB - └───index.js - 5.78 KB + └───index.d.ts - 4.09 KB + └───index.js - 5.68 KB └───/getChildren - └───index.d.ts - 1.21 KB + └───index.d.ts - 1.19 KB └───index.js - 1.87 KB └───/getChildrenByType - └───index.d.ts - 3.86 KB - └───index.js - 5.12 KB + └───index.d.ts - 3.8 KB + └───index.js - 5.08 KB └───/getChildrenWithDescendant - └───index.d.ts - 638 Bytes - └───index.js - 1.13 KB + └───index.d.ts - 626 Bytes + └───index.js - 1.1 KB └───/getChildrenWithDescendantByType - └───index.d.ts - 2.26 KB - └───index.js - 2.81 KB + └───index.d.ts - 2.22 KB + └───index.js - 2.76 KB └───/getDescendantDepth - └───index.d.ts - 1.14 KB + └───index.d.ts - 1.12 KB └───index.js - 2.3 KB └───/getDescendantDepthByType - └───index.d.ts - 2.39 KB - └───index.js - 3.74 KB - └───index.d.ts - 1.1 KB - └───index.js - 905 Bytes + └───index.d.ts - 2.35 KB + └───index.js - 3.72 KB + └───index.d.ts - 1.08 KB + └───index.js - 892 Bytes └───/noEmptyChildren - └───index.d.ts - 1.75 KB - └───index.js - 3.2 KB + └───index.d.ts - 1.72 KB + └───index.js - 3.15 KB └───/overrideProps - └───index.d.ts - 2.72 KB - └───index.js - 4.44 KB + └───index.d.ts - 2.68 KB + └───index.js - 4.36 KB └───/removeChildren - └───index.d.ts - 1.22 KB - └───index.js - 2.34 KB + └───index.d.ts - 1.2 KB + └───index.js - 2.33 KB └───/removeChildrenByType - └───index.d.ts - 3.71 KB - └───index.js - 5.32 KB + └───index.d.ts - 3.65 KB + └───index.js - 5.28 KB └───/typeOfComponent - └───index.d.ts - 614 Bytes - └───index.js - 1.6 KB - └───types.d.ts - 249 Bytes - └───types.js - 12 Bytes - └───/_private - └───utils.d.ts - 61 Bytes - └───utils.js - 700 Bytes + └───index.d.ts - 603 Bytes + └───index.js - 1.58 KB + └───types.d.ts - 240 Bytes + └───types.js - 11 Bytes ```` \ No newline at end of file diff --git a/src/getChild/index.deep.test.js b/src/getChild/index.deep.test.js index 4d2142c..99b269b 100644 --- a/src/getChild/index.deep.test.js +++ b/src/getChild/index.deep.test.js @@ -27,7 +27,7 @@ let children = [ { type: 'span' }, { type: 'div' }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildDeep', () => { test('Deep find', () => { diff --git a/src/getChild/index.ts b/src/getChild/index.ts index ae0ef00..1d0c2eb 100644 --- a/src/getChild/index.ts +++ b/src/getChild/index.ts @@ -36,7 +36,7 @@ export const getChildDeep = (children: T, for (const child of _children) { if (predicate(child as TC)) return child as TC; - if ((child as any).props?.children) { + if ((child as any)?.props?.children) { const result = getChildDeep((child as NannyNode).props.children, predicate); if (result) return result; diff --git a/src/getChildByType/index.deep.test.js b/src/getChildByType/index.deep.test.js index f429073..a7991b3 100644 --- a/src/getChildByType/index.deep.test.js +++ b/src/getChildByType/index.deep.test.js @@ -28,7 +28,7 @@ const children = [ { type: 'span' }, { type: 'div' }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildByTypeDeep', () => { test('Deep find => single', () => { diff --git a/src/getChildByType/index.test.js b/src/getChildByType/index.test.js index e2f704e..5cbdb03 100644 --- a/src/getChildByType/index.test.js +++ b/src/getChildByType/index.test.js @@ -4,7 +4,7 @@ const React = require('react'); const { getChildByType } = require('../../dist/lib/es5/index'); describe('getChildByType', () => { - React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); + React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); test('Single', () => { let children = { props: { __TYPE: 'CustomComponent' }}; diff --git a/src/getChildren/index.deep.test.js b/src/getChildren/index.deep.test.js index 104cfb7..165e9b5 100644 --- a/src/getChildren/index.deep.test.js +++ b/src/getChildren/index.deep.test.js @@ -26,8 +26,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildrenDeep', () => { test('Deep find', () => { diff --git a/src/getChildren/index.ts b/src/getChildren/index.ts index 4134896..b3c9d21 100644 --- a/src/getChildren/index.ts +++ b/src/getChildren/index.ts @@ -40,7 +40,7 @@ export const getChildrenDeep = (children: output = [...output, child as TC]; } - if ((child as NannyNode).props?.children) { + if ((child as NannyNode)?.props?.children) { output = [...output, ...getChildrenDeep((child as NannyNode).props.children, predicate)]; } } diff --git a/src/getChildrenByType/index.deep.test.js b/src/getChildrenByType/index.deep.test.js index 710f43b..d4b8395 100644 --- a/src/getChildrenByType/index.deep.test.js +++ b/src/getChildrenByType/index.deep.test.js @@ -33,8 +33,10 @@ const children = [ }, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildrenByTypeDeep', () => { test('Deep Single', () => { diff --git a/src/getChildrenByType/index.ts b/src/getChildrenByType/index.ts index 451be22..f5bd241 100644 --- a/src/getChildrenByType/index.ts +++ b/src/getChildrenByType/index.ts @@ -83,7 +83,7 @@ export const getChildrenByTypeDeep = (children: T output = [...output, child as T]; } - if ((child as NannyNode).props?.children && !(skipWhenFound && found)) { + if ((child as NannyNode)?.props?.children && !(skipWhenFound && found)) { output = [...output, ...getChildrenByTypeDeep((child as NannyNode).props.children, _types, { customTypeKey, skipWhenFound })]; } } diff --git a/src/getChildrenWithDescendant/index.test.js b/src/getChildrenWithDescendant/index.test.js index 9522db5..05477bd 100644 --- a/src/getChildrenWithDescendant/index.test.js +++ b/src/getChildrenWithDescendant/index.test.js @@ -26,8 +26,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildrenWithDescendant', () => { test('Basic', () => { diff --git a/src/getChildrenWithDescendantByType/index.test.js b/src/getChildrenWithDescendantByType/index.test.js index 40488ee..c36a17e 100644 --- a/src/getChildrenWithDescendantByType/index.test.js +++ b/src/getChildrenWithDescendantByType/index.test.js @@ -28,7 +28,7 @@ const children = [ { type: 'span' }, { type: 'div' }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getChildrenWithDescendantByType', () => { test('Single', () => { diff --git a/src/getDescendantDepth/index.test.js b/src/getDescendantDepth/index.test.js index b55d299..d6fb06c 100644 --- a/src/getDescendantDepth/index.test.js +++ b/src/getDescendantDepth/index.test.js @@ -26,8 +26,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getDescendantDepth', () => { test('Basic', () => { diff --git a/src/getDescendantDepth/index.ts b/src/getDescendantDepth/index.ts index d0bdfa8..e4659af 100644 --- a/src/getDescendantDepth/index.ts +++ b/src/getDescendantDepth/index.ts @@ -29,7 +29,7 @@ export const getDescendantDepth = (childr for (const child of _children) { if (predicate(child as TC)) return level + 1; - if ((child as any).props?.children) { + if ((child as any)?.props?.children) { const result = getDepth((child as NannyNode).props.children, predicate, level + 1); if (result > 0) return result; diff --git a/src/getDescendantDepthByType/index.test.js b/src/getDescendantDepthByType/index.test.js index 95c17be..61f4ee3 100644 --- a/src/getDescendantDepthByType/index.test.js +++ b/src/getDescendantDepthByType/index.test.js @@ -27,8 +27,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('getDescendantDepthByType', () => { test('Single', () => { diff --git a/src/getDescendantDepthByType/index.ts b/src/getDescendantDepthByType/index.ts index a3f31a2..aad2e08 100644 --- a/src/getDescendantDepthByType/index.ts +++ b/src/getDescendantDepthByType/index.ts @@ -52,7 +52,7 @@ export const getDescendantDepthByType = (children return level + 1; } - if ((child as NannyNode).props?.children) { + if ((child as NannyNode)?.props?.children) { const result = getDepth((child as NannyNode).props.children, level + 1); if (result > 0) return result; diff --git a/src/overrideProps/index.test.js b/src/overrideProps/index.test.js index 0de6bbf..0b82bb2 100644 --- a/src/overrideProps/index.test.js +++ b/src/overrideProps/index.test.js @@ -4,7 +4,7 @@ const React = require('react'); const { overrideProps, overridePropsDeep } = require('../../dist/lib/es5/index'); describe('overrideProps', () => { - React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); + React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); React.cloneElement = (x, props) => ({ ...x, props: { ...x.props, ...props }}); const component = { @@ -100,7 +100,7 @@ describe('overrideProps', () => { }); describe('overridePropsDeep', () => { - React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); + React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); React.cloneElement = (x, props) => ({ ...x, props: { ...x.props, ...props }}); const children = { diff --git a/src/removeChildren/index.deep.test.js b/src/removeChildren/index.deep.test.js index 25d2513..a7b1f2f 100644 --- a/src/removeChildren/index.deep.test.js +++ b/src/removeChildren/index.deep.test.js @@ -26,8 +26,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('removeChildrenDeep', () => { test('Deep remove', () => { @@ -52,6 +54,8 @@ describe('removeChildrenDeep', () => { { props: { __TYPE: 'CustomComponent', active: false, children: 'Outer child' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]); }); }); \ No newline at end of file diff --git a/src/removeChildren/index.ts b/src/removeChildren/index.ts index d8fda91..aa6f097 100644 --- a/src/removeChildren/index.ts +++ b/src/removeChildren/index.ts @@ -37,7 +37,7 @@ export const removeChildrenDeep = (childr for (const child of _children) { if (!predicate(child as TC)) { - if ((child as NannyNode).props?.children) { + if ((child as NannyNode)?.props?.children) { output = [ ...output, Object.assign((child as NannyNode), { diff --git a/src/removeChildrenByType/index.deep.test.js b/src/removeChildrenByType/index.deep.test.js index 056db3d..75a8ae4 100644 --- a/src/removeChildrenByType/index.deep.test.js +++ b/src/removeChildrenByType/index.deep.test.js @@ -26,8 +26,10 @@ const children = [ { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]; -React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]).filter(z => z != undefined); +React.Children.toArray = x => (x && Array.isArray(x) ? x : [x]); describe('removeChildrenByTypeDeep', () => { test('Single', () => { @@ -51,6 +53,8 @@ describe('removeChildrenByTypeDeep', () => { }, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]); }); @@ -75,6 +79,8 @@ describe('removeChildrenByTypeDeep', () => { }, { type: 'span' }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]); }); @@ -99,6 +105,8 @@ describe('removeChildrenByTypeDeep', () => { { props: { __TYPE: 'CustomComponent', active: false, children: 'Outer child' }}, { props: { __TYPE: 'CustomComponent', active: true, children: 'Outer child active' }}, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]); }); @@ -120,6 +128,8 @@ describe('removeChildrenByTypeDeep', () => { }, }, { type: 'div' }, + undefined, + { type: 'div', props: { children: [null, {type: 'div'}] } }, ]); }); }); \ No newline at end of file diff --git a/src/removeChildrenByType/index.ts b/src/removeChildrenByType/index.ts index 7c0242e..6338f8d 100644 --- a/src/removeChildrenByType/index.ts +++ b/src/removeChildrenByType/index.ts @@ -75,7 +75,7 @@ export const removeChildrenByTypeDeep = (children for (const child of _children) { if (_types.indexOf(typeOfComponent(child, customTypeKey)) === -1) { - if ((child as NannyNode).props?.children) { + if ((child as NannyNode)?.props?.children) { output = [ ...output, Object.assign({}, (child as NannyNode), {