Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use filter and map instead of reduce #948

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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