From 222fbfffb5187569bb671e0682aa813b02f9ab1b Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 24 Mar 2017 10:19:35 -0400 Subject: [PATCH] Collapse parser logic into single reduce loop --- modules/blocks/parser/index.js | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/modules/blocks/parser/index.js b/modules/blocks/parser/index.js index e0acdd84fe4807..4576de19ce58e6 100644 --- a/modules/blocks/parser/index.js +++ b/modules/blocks/parser/index.js @@ -33,26 +33,20 @@ export function getBlockAttributes( blockNode, blockSettings ) { /** * Returns a list of blocks extracted from the Post Content * - * @param {String} postContent The post content - * @return {Array} Block list + * @param {String} content The post content + * @return {Array} Block list */ -const parse = ( postContent ) => { - const nodeBlocks = grammarParse( postContent ); +export default function parse( content ) { + return grammarParse( content ).reduce( ( memo, blockNode ) => { + const settings = getBlockSettings( blockNode.blockType ); - return nodeBlocks - .map( ( blockNode ) => { - return { - blockSettings: getBlockSettings( blockNode.blockType ), - blockNode - }; - } ) - .filter( ( { blockSettings } ) => !! blockSettings ) - .map( ( { blockNode, blockSettings } ) => { - return { + if ( settings ) { + memo.push( { blockType: blockNode.blockType, - attributes: getBlockAttributes( blockNode, blockSettings ) - }; - } ); -}; + attributes: getBlockAttributes( blockNode, settings ) + } ); + } -export default parse; + return memo; + }, [] ); +}