@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
8 lines (7 loc) • 1.51 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/get-nested-value.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 undefined.\n * @return {*} Value of the object property at the specified path.\n */\nexport default function getNestedValue( object, path, defaultValue ) {\n\tif (\n\t\t! object ||\n\t\ttypeof object !== 'object' ||\n\t\t( typeof path !== 'string' && ! Array.isArray( path ) )\n\t) {\n\t\treturn object;\n\t}\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 !== undefined ? value : defaultValue;\n}\n"],
"mappings": ";AAYe,SAAR,eAAiC,QAAQ,MAAM,cAAe;AACpE,MACC,CAAE,UACF,OAAO,WAAW,YAChB,OAAO,SAAS,YAAY,CAAE,MAAM,QAAS,IAAK,GACnD;AACD,WAAO;AAAA,EACR;AACA,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,UAAU,SAAY,QAAQ;AACtC;",
"names": []
}