Skip to content

Commit 4d0f303

Browse files
authored
Lodash: Replace _.mergeWith() with deepmerge in blocks (#50637)
1 parent fd7c943 commit 4d0f303

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/blocks/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@wordpress/shortcode": "file:../shortcode",
4646
"change-case": "^4.1.2",
4747
"colord": "^2.7.0",
48+
"deepmerge": "^4.3.0",
4849
"fast-deep-equal": "^3.1.3",
4950
"hpq": "^1.3.0",
5051
"is-plain-object": "^5.0.0",

packages/blocks/src/api/raw-handling/utils.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* External dependencies
33
*/
4-
import { mergeWith } from 'lodash';
4+
import deepmerge from 'deepmerge';
55

66
/**
77
* WordPress dependencies
@@ -14,6 +14,41 @@ import { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom';
1414
import { hasBlockSupport } from '..';
1515
import { getRawTransforms } from './get-raw-transforms';
1616

17+
const customMerge = ( key ) => {
18+
return ( srcValue, objValue ) => {
19+
switch ( key ) {
20+
case 'children': {
21+
if ( objValue === '*' || srcValue === '*' ) {
22+
return '*';
23+
}
24+
25+
return { ...objValue, ...srcValue };
26+
}
27+
case 'attributes':
28+
case 'require': {
29+
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
30+
}
31+
case 'isMatch': {
32+
// If one of the values being merge is undefined (matches everything),
33+
// the result of the merge will be undefined.
34+
if ( ! objValue || ! srcValue ) {
35+
return undefined;
36+
}
37+
// When merging two isMatch functions, the result is a new function
38+
// that returns if one of the source functions returns true.
39+
return ( ...args ) => {
40+
return objValue( ...args ) || srcValue( ...args );
41+
};
42+
}
43+
}
44+
45+
return deepmerge( objValue, srcValue, {
46+
customMerge,
47+
clone: false,
48+
} );
49+
};
50+
};
51+
1752
export function getBlockContentSchemaFromTransforms( transforms, context ) {
1853
const phrasingContentSchema = getPhrasingContentSchema( context );
1954
const schemaArgs = { phrasingContentSchema, isPaste: context === 'paste' };
@@ -51,32 +86,9 @@ export function getBlockContentSchemaFromTransforms( transforms, context ) {
5186
);
5287
} );
5388

54-
return mergeWith( {}, ...schemas, ( objValue, srcValue, key ) => {
55-
switch ( key ) {
56-
case 'children': {
57-
if ( objValue === '*' || srcValue === '*' ) {
58-
return '*';
59-
}
60-
61-
return { ...objValue, ...srcValue };
62-
}
63-
case 'attributes':
64-
case 'require': {
65-
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
66-
}
67-
case 'isMatch': {
68-
// If one of the values being merge is undefined (matches everything),
69-
// the result of the merge will be undefined.
70-
if ( ! objValue || ! srcValue ) {
71-
return undefined;
72-
}
73-
// When merging two isMatch functions, the result is a new function
74-
// that returns if one of the source functions returns true.
75-
return ( ...args ) => {
76-
return objValue( ...args ) || srcValue( ...args );
77-
};
78-
}
79-
}
89+
return deepmerge.all( schemas, {
90+
customMerge,
91+
clone: false,
8092
} );
8193
}
8294

0 commit comments

Comments
 (0)