-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block Variations: Compare objects based on given properties #62272
Changes from 5 commits
dfc4e02
8806d80
75d61fc
d81e44d
7d655b2
1e68d01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -18,3 +18,33 @@ export const getValueFromObjectPath = ( object, path, defaultValue ) => { | |||
} ); | ||||
return value ?? defaultValue; | ||||
}; | ||||
|
||||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
function isObject( candidate ) { | ||||
return ( | ||||
typeof candidate === 'object' && | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We called it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need more context. What exactly are we referring to as "item" in the Interactivity API? What is being referred to here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In Interactivity API, we check that several "items" are a JSON object in the same file (sources, state, config, proxy handlers, etc). So we call the variable to check "item". "Candidate" seems also a good general definition, and would be ok to use the same name in all functions that are just checking that an item is an object (there are a few of them across the workspace). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, ok. You can change that if you want 🙂 |
||||
candidate.constructor === Object && | ||||
candidate !== null | ||||
); | ||||
} | ||||
|
||||
/** | ||||
* Determine whether a set of object properties matches a given object. | ||||
* | ||||
* Given an object of block attributes and an object of variation attributes, | ||||
* this function checks recursively whether all the variation attributes are | ||||
* present in the block attributes object. | ||||
* | ||||
* @param {Object} blockAttributes The object to inspect. | ||||
* @param {Object} variationAttributes The object of property values to match. | ||||
* @return {boolean} Whether the block attributes match the variation attributes. | ||||
*/ | ||||
export function matchesAttributes( blockAttributes, variationAttributes ) { | ||||
if ( isObject( blockAttributes ) && isObject( variationAttributes ) ) { | ||||
return Object.entries( variationAttributes ).every( | ||||
( [ key, value ] ) => | ||||
matchesAttributes( blockAttributes?.[ key ], value ) | ||||
); | ||||
} | ||||
|
||||
return blockAttributes === variationAttributes; | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How far is it from
isShallowEqual
from@wordpress/is-shallow-equal
at this point?gutenberg/packages/is-shallow-equal/src/index.js
Lines 23 to 33 in 06470bb