-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e334ecd
commit bb3b325
Showing
4 changed files
with
77 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/block-editor/src/utils/get-transformed-metadata.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Transform the metadata attribute with only the values and bindings specified by each transform. | ||
* Returns `undefined` if the input metadata is falsy. | ||
* | ||
* @param {Object} metadata Original metadata attribute from the block that is being transformed. | ||
* @param {Array} supportedProps Array with the metadata properties to keep after the transform. | ||
* @param {Object} bindingsMappings Maps each bound attribute of the original block to its corresponding attribute in the result. | ||
* @return {Object|undefined} New metadata object only with the relevant properties. | ||
*/ | ||
export function getTransformedMetadata( | ||
metadata, | ||
supportedProps, | ||
bindingsMappings | ||
) { | ||
if ( ! metadata ) { | ||
return; | ||
} | ||
return Object.entries( metadata ).reduce( ( obj, [ prop, value ] ) => { | ||
// If prop is not supported, don't add it to the new metadata object. | ||
if ( ! supportedProps.includes( prop ) ) { | ||
return obj; | ||
} | ||
// If prop is `bindings` and `bindingsMappings` is not defined, don't add it to the new metadata object. | ||
if ( prop === 'bindings' && ! bindingsMappings ) { | ||
return obj; | ||
} | ||
// Adapt bindings object if `bindingsMappings` is defined. | ||
// The rest of properties are added as they are. | ||
obj[ prop ] = | ||
prop === 'bindings' && !! bindingsMappings | ||
? Object.entries( bindingsMappings ).reduce( | ||
( bindingsObj, [ originalKey, resultingKey ] ) => { | ||
bindingsObj[ resultingKey ] = value[ originalKey ]; | ||
return bindingsObj; | ||
}, | ||
{} | ||
) | ||
: value; | ||
|
||
return obj; | ||
}, {} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters