Skip to content

Commit

Permalink
Use flatMap where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 17, 2018
1 parent ebac53e commit d8bd451
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
3 changes: 1 addition & 2 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ export function getPossibleBlockTransformations( blocks ) {
* @return {Array} Array of transforms.
*/
export function getPossibleShortcutTransformations( name ) {
const transformsFrom = getBlockTypes()
.reduce( ( acc, blockType ) => [ ...acc, ...get( blockType, 'transforms.from', [] ) ], [] )
const transformsFrom = flatMap( getBlockTypes(), ( blockType ) => get( blockType, 'transforms.from', [] ) )
.filter( isTransformForBlockSource( name, 'shortcut', false ) );
const transformsTo = get( getBlockType( name ), 'transforms.to', [] )
.filter( ( { type } ) => type === 'shortcut' );
Expand Down
12 changes: 6 additions & 6 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, compact, get, initial, last, isEmpty, first, map } from 'lodash';
import { find, compact, get, initial, last, isEmpty, first, map, flatMap } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -150,19 +150,19 @@ registerBlockType( 'core/list', {

// All lists already with tag, set back to paragraphs.
if ( isSame && firstNodeName === tag ) {
return blocks.reduce( ( acc, { attributes: { values } } ) => [
...acc,
...compact( values.map( ( value ) => get( value, 'props.children', null ) ) )
return flatMap( blocks, ( { attributes: { values } } ) =>
compact( values.map( ( value ) => get( value, 'props.children', null ) ) )
.map( ( content ) => createBlock( 'core/paragraph', {
content: [ content ],
} ) ),
], [] );
);
}

// Merge list.
return createBlock( 'core/list', {
nodeName: tag,
values: blocks.reduce( ( acc, { attributes: { values } } ) => [ ...acc, ...values ], [] ),
values: flatMap( blocks, ( { attributes: { values } } ) => values )
.map( ( value, i ) => <li key={ i }>{ get( value, 'props.children', null ) }</li> ),
} );
},
} ) ),
Expand Down

0 comments on commit d8bd451

Please sign in to comment.