@wordpress/blocks
Version:
Block API for WordPress.
8 lines (7 loc) • 2.62 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/store/utils.js"],
"sourcesContent": ["/**\n * Helper util to return a value from a certain path of the object.\n * Path is specified as either:\n * - a string of properties, separated by dots, for example: \"x.y\".\n * - an array of properties, for example `[ 'x', 'y' ]`.\n * You can also specify a default value in case the result is nullish.\n *\n * @param {Object} object Input object.\n * @param {string|Array} path Path to the object property.\n * @param {*} defaultValue Default value if the value at the specified path is nullish.\n * @return {*} Value of the object property at the specified path.\n */\nexport const getValueFromObjectPath = ( object, path, defaultValue ) => {\n\tconst normalizedPath = Array.isArray( path ) ? path : path.split( '.' );\n\tlet value = object;\n\tnormalizedPath.forEach( ( fieldName ) => {\n\t\tvalue = value?.[ fieldName ];\n\t} );\n\treturn value ?? defaultValue;\n};\n\nfunction isObject( candidate ) {\n\treturn (\n\t\ttypeof candidate === 'object' &&\n\t\tcandidate.constructor === Object &&\n\t\tcandidate !== null\n\t);\n}\n\n/**\n * Determine whether a set of object properties matches a given object.\n *\n * Given an object of block attributes and an object of variation attributes,\n * this function checks recursively whether all the variation attributes are\n * present in the block attributes object.\n *\n * @param {Object} blockAttributes The object to inspect.\n * @param {Object} variationAttributes The object of property values to match.\n * @return {boolean} Whether the block attributes match the variation attributes.\n */\nexport function matchesAttributes( blockAttributes, variationAttributes ) {\n\tif ( isObject( blockAttributes ) && isObject( variationAttributes ) ) {\n\t\treturn Object.entries( variationAttributes ).every(\n\t\t\t( [ key, value ] ) =>\n\t\t\t\tmatchesAttributes( blockAttributes?.[ key ], value )\n\t\t);\n\t}\n\n\treturn blockAttributes === variationAttributes;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYO,IAAM,yBAAyB,CAAE,QAAQ,MAAM,iBAAkB;AACvE,QAAM,iBAAiB,MAAM,QAAS,IAAK,IAAI,OAAO,KAAK,MAAO,GAAI;AACtE,MAAI,QAAQ;AACZ,iBAAe,QAAS,CAAE,cAAe;AACxC,YAAQ,QAAS,SAAU;AAAA,EAC5B,CAAE;AACF,SAAO,SAAS;AACjB;AAEA,SAAS,SAAU,WAAY;AAC9B,SACC,OAAO,cAAc,YACrB,UAAU,gBAAgB,UAC1B,cAAc;AAEhB;AAaO,SAAS,kBAAmB,iBAAiB,qBAAsB;AACzE,MAAK,SAAU,eAAgB,KAAK,SAAU,mBAAoB,GAAI;AACrE,WAAO,OAAO,QAAS,mBAAoB,EAAE;AAAA,MAC5C,CAAE,CAAE,KAAK,KAAM,MACd,kBAAmB,kBAAmB,GAAI,GAAG,KAAM;AAAA,IACrD;AAAA,EACD;AAEA,SAAO,oBAAoB;AAC5B;",
"names": []
}