From 2bc91ab93dca9dfb2d010d5bcb1e8ee9807eb07a Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 4 Nov 2020 12:16:37 +0100 Subject: [PATCH] Exit early --- .../src/navigation/use-create-navigation-tree.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/components/src/navigation/use-create-navigation-tree.js b/packages/components/src/navigation/use-create-navigation-tree.js index 1db7ba9c94938..63d5d4a6e2bbb 100644 --- a/packages/components/src/navigation/use-create-navigation-tree.js +++ b/packages/components/src/navigation/use-create-navigation-tree.js @@ -48,20 +48,23 @@ export const useCreateNavigationTree = () => { visited.push( current.menu ); queue = [ ...queue, ...getChildMenu( current.menu ) ]; - callback( current ); + if ( callback( current ) === false ) { + break; + } } }; const isMenuEmpty = ( menuToCheck ) => { - let count = 0; + let isEmpty = true; traverseMenu( menuToCheck, ( current ) => { if ( ! current.isEmpty ) { - count++; + isEmpty = false; + return false; } } ); - return count === 0; + return isEmpty; }; return {