Skip to content

Commit

Permalink
Lodash: Replace _.mergeWith() with deepmerge in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jun 12, 2023
1 parent 1ae8864 commit fe8a518
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@wordpress/shortcode": "file:../shortcode",
"change-case": "^4.1.2",
"colord": "^2.7.0",
"deepmerge": "^4.3.0",
"fast-deep-equal": "^3.1.3",
"hpq": "^1.3.0",
"is-plain-object": "^5.0.0",
Expand Down
66 changes: 39 additions & 27 deletions packages/blocks/src/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { mergeWith } from 'lodash';
import deepmerge from 'deepmerge';

/**
* WordPress dependencies
Expand All @@ -14,6 +14,41 @@ import { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom';
import { hasBlockSupport } from '..';
import { getRawTransforms } from './get-raw-transforms';

const customMerge = ( key ) => {
return ( srcValue, objValue ) => {
switch ( key ) {
case 'children': {
if ( objValue === '*' || srcValue === '*' ) {
return '*';
}

return { ...objValue, ...srcValue };
}
case 'attributes':
case 'require': {
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
}
case 'isMatch': {
// If one of the values being merge is undefined (matches everything),
// the result of the merge will be undefined.
if ( ! objValue || ! srcValue ) {
return undefined;
}
// When merging two isMatch functions, the result is a new function
// that returns if one of the source functions returns true.
return ( ...args ) => {
return objValue( ...args ) || srcValue( ...args );
};
}
}

return deepmerge( objValue, srcValue, {
customMerge,
clone: false,
} );
};
};

export function getBlockContentSchemaFromTransforms( transforms, context ) {
const phrasingContentSchema = getPhrasingContentSchema( context );
const schemaArgs = { phrasingContentSchema, isPaste: context === 'paste' };
Expand Down Expand Up @@ -51,32 +86,9 @@ export function getBlockContentSchemaFromTransforms( transforms, context ) {
);
} );

return mergeWith( {}, ...schemas, ( objValue, srcValue, key ) => {
switch ( key ) {
case 'children': {
if ( objValue === '*' || srcValue === '*' ) {
return '*';
}

return { ...objValue, ...srcValue };
}
case 'attributes':
case 'require': {
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
}
case 'isMatch': {
// If one of the values being merge is undefined (matches everything),
// the result of the merge will be undefined.
if ( ! objValue || ! srcValue ) {
return undefined;
}
// When merging two isMatch functions, the result is a new function
// that returns if one of the source functions returns true.
return ( ...args ) => {
return objValue( ...args ) || srcValue( ...args );
};
}
}
return deepmerge.all( schemas, {
customMerge,
clone: false,
} );
}

Expand Down

0 comments on commit fe8a518

Please sign in to comment.