Skip to content

Commit

Permalink
Refactor: Use filter and map instead of reduce
Browse files Browse the repository at this point in the history
Just curious on the choice of the reduce. Seems like a filter and map
are more appropriate when joined with a space.
  • Loading branch information
dmsnell committed May 30, 2017
1 parent 62552a9 commit 7b43ce8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
54 changes: 24 additions & 30 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ export function getCommentAttributes( realAttributes, expectedAttributes ) {
);

// Serialize the comment attributes
return keys.reduce( ( memo, key ) => {
const value = realAttributes[ key ];
if ( undefined === value ) {
return memo;
}

return memo + `${ key }="${ value }" `;
}, '' );
return keys
.filter( key => undefined !== realAttributes[ key ] )
.map( key => `${ key }="${ realAttributes[ key ] }"` )
.join( ' ' );
}

/**
Expand All @@ -70,28 +66,26 @@ export function getCommentAttributes( realAttributes, expectedAttributes ) {
* @return {String} The post content
*/
export default function serialize( blocks ) {
return blocks.reduce( ( memo, block ) => {
const blockType = block.blockType;
const settings = getBlockSettings( blockType );
const saveContent = getSaveContent( settings.save, block.attributes );
const beautifyOptions = {
indent_inner_html: true,
wrap_line_length: 0,
};

return memo + (
'<!-- wp:' +
blockType +
' ' +
getCommentAttributes(
return blocks
.map( block => {
const blockType = block.blockType;
const settings = getBlockSettings( blockType );
const saveContent = getSaveContent( settings.save, block.attributes );
const beautifyOptions = {
indent_inner_html: true,
wrap_line_length: 0,
};
const attributes = getCommentAttributes(
block.attributes,
parseBlockAttributes( saveContent, settings )
) +
'-->' +
( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) +
'<!-- /wp:' +
blockType +
' -->'
) + '\n\n';
}, '' );
);

return [
`<!-- wp:${ blockType }${ attributes ? ` ${ attributes } ` : ' ' }-->`,
saveContent ? beautifyHtml( saveContent, beautifyOptions ) : undefined,
`<!-- /wp:${ blockType } -->`,
].join( '\n' );
} )
.join( '\n\n' )
.concat( '\n\n' ); // since we no longer have off-by-one-errors with spacing, we may not need this
}
4 changes: 2 additions & 2 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe( 'block serializer', () => {
fruit: 'bananas',
} );

expect( attributes ).to.equal( 'category="food" ripeness="ripe" ' );
expect( attributes ).to.equal( 'category="food" ripeness="ripe"' );
} );

it( 'should not append an undefined attribute value', () => {
Expand All @@ -83,7 +83,7 @@ describe( 'block serializer', () => {
fruit: 'bananas',
} );

expect( attributes ).to.equal( 'category="food" ' );
expect( attributes ).to.equal( 'category="food"' );
} );
} );

Expand Down

0 comments on commit 7b43ce8

Please sign in to comment.