UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 3.4 kB
{ "version": 3, "sources": ["../../src/utils/object.js"], "sourcesContent": ["/**\n * Immutably sets a value inside an object. Like `lodash#set`, but returning a\n * new object. Treats nullish initial values as empty objects. Clones any\n * nested objects. Supports arrays, too.\n *\n * @param {Object} object Object to set a value in.\n * @param {number|string|Array} path Path in the object to modify.\n * @param {*} value New value to set.\n * @return {Object} Cloned object with the new value set.\n */\nexport function setImmutably( object, path, value ) {\n\t// Normalize path\n\tpath = Array.isArray( path ) ? [ ...path ] : [ path ];\n\n\t// Shallowly clone the base of the object\n\tobject = Array.isArray( object ) ? [ ...object ] : { ...object };\n\n\tconst leaf = path.pop();\n\n\t// Traverse object from root to leaf, shallowly cloning at each level\n\tlet prev = object;\n\tfor ( const key of path ) {\n\t\tconst lvl = prev[ key ];\n\t\tprev = prev[ key ] = Array.isArray( lvl ) ? [ ...lvl ] : { ...lvl };\n\t}\n\n\tprev[ leaf ] = value;\n\n\treturn object;\n}\n\n/**\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 arrayPath = Array.isArray( path ) ? path : path.split( '.' );\n\tlet value = object;\n\tarrayPath.forEach( ( fieldName ) => {\n\t\tvalue = value?.[ fieldName ];\n\t} );\n\treturn value ?? defaultValue;\n};\n\n/**\n * Helper util to filter out objects with duplicate values for a given property.\n *\n * @param {Object[]} array Array of objects to filter.\n * @param {string} property Property to filter unique values by.\n *\n * @return {Object[]} Array of objects with unique values for the specified property.\n */\nexport function uniqByProperty( array, property ) {\n\tconst seen = new Set();\n\treturn array.filter( ( item ) => {\n\t\tconst value = item[ property ];\n\t\treturn seen.has( value ) ? false : seen.add( value );\n\t} );\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,aAAc,QAAQ,MAAM,OAAQ;AAEnD,SAAO,MAAM,QAAS,IAAK,IAAI,CAAE,GAAG,IAAK,IAAI,CAAE,IAAK;AAGpD,WAAS,MAAM,QAAS,MAAO,IAAI,CAAE,GAAG,MAAO,IAAI,EAAE,GAAG,OAAO;AAE/D,QAAM,OAAO,KAAK,IAAI;AAGtB,MAAI,OAAO;AACX,aAAY,OAAO,MAAO;AACzB,UAAM,MAAM,KAAM,GAAI;AACtB,WAAO,KAAM,GAAI,IAAI,MAAM,QAAS,GAAI,IAAI,CAAE,GAAG,GAAI,IAAI,EAAE,GAAG,IAAI;AAAA,EACnE;AAEA,OAAM,IAAK,IAAI;AAEf,SAAO;AACR;AAcO,IAAM,yBAAyB,CAAE,QAAQ,MAAM,iBAAkB;AACvE,QAAM,YAAY,MAAM,QAAS,IAAK,IAAI,OAAO,KAAK,MAAO,GAAI;AACjE,MAAI,QAAQ;AACZ,YAAU,QAAS,CAAE,cAAe;AACnC,YAAQ,QAAS,SAAU;AAAA,EAC5B,CAAE;AACF,SAAO,SAAS;AACjB;AAUO,SAAS,eAAgB,OAAO,UAAW;AACjD,QAAM,OAAO,oBAAI,IAAI;AACrB,SAAO,MAAM,OAAQ,CAAE,SAAU;AAChC,UAAM,QAAQ,KAAM,QAAS;AAC7B,WAAO,KAAK,IAAK,KAAM,IAAI,QAAQ,KAAK,IAAK,KAAM;AAAA,EACpD,CAAE;AACH;", "names": [] }