Skip to content

Commit

Permalink
Blocks API: Simplify logic using lodash's reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 17, 2017
1 parent acfd16b commit 18c5e05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { parse as hpqParse } from 'hpq';
import { keys } from 'lodash';
import { keys, reduce } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -124,11 +124,10 @@ export function getBlockAttribute( attributeKey, attributeSchema, rawContent, co
* @return {Object} All block attributes
*/
export function getBlockAttributes( blockType, rawContent, attributes ) {
const blockAttributes = keys( blockType.attributes ).reduce( ( memo, attributeKey ) => {
const attributeSchema = blockType.attributes[ attributeKey ];
const blockAttributes = reduce( blockType.attributes, ( memo, attributeSchema, attributeKey ) => {
memo[ attributeKey ] = getBlockAttribute( attributeKey, attributeSchema, rawContent, attributes );
return memo;
}, {} ) || {};
}, {} );

// If the block supports anchor, parse the id
if ( blockType.supportAnchor ) {
Expand Down
6 changes: 3 additions & 3 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { get, isFunction, some, keys } from 'lodash';
import { get, isFunction, some, reduce } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -121,10 +121,10 @@ export function registerBlockType( name, settings ) {
const block = blocks[ name ] = {
...settings,
name,
attributes: keys( attributes ).reduce( ( memo, attributeKey ) => {
attributes: reduce( attributes, ( memo, attributeSchema, attributeKey ) => {
memo[ attributeKey ] = {
source: 'comment',
...attributes[ attributeKey ],
...attributeSchema,
};
return memo;
}, {} ),
Expand Down

0 comments on commit 18c5e05

Please sign in to comment.