UNPKG

@wordpress/core-data

Version:
8 lines (7 loc) 1.94 kB
{ "version": 3, "sources": ["../../src/utils/conservative-map-item.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * Given the current and next item entity record, returns the minimally \"modified\"\n * result of the next item, preferring value references from the original item\n * if equal. If all values match, the original item is returned.\n *\n * @param {Object} item Original item.\n * @param {Object} nextItem Next item.\n *\n * @return {Object} Minimally modified merged item.\n */\nexport default function conservativeMapItem( item, nextItem ) {\n\t// Return next item in its entirety if there is no original item.\n\tif ( ! item ) {\n\t\treturn nextItem;\n\t}\n\n\tlet hasChanges = false;\n\tconst result = {};\n\tfor ( const key in nextItem ) {\n\t\tif ( fastDeepEqual( item[ key ], nextItem[ key ] ) ) {\n\t\t\tresult[ key ] = item[ key ];\n\t\t} else {\n\t\t\thasChanges = true;\n\t\t\tresult[ key ] = nextItem[ key ];\n\t\t}\n\t}\n\n\tif ( ! hasChanges ) {\n\t\treturn item;\n\t}\n\n\t// Only at this point, backfill properties from the original item which\n\t// weren't explicitly set into the result above. This is an optimization\n\t// to allow `hasChanges` to return early.\n\tfor ( const key in item ) {\n\t\tif ( ! result.hasOwnProperty( key ) ) {\n\t\t\tresult[ key ] = item[ key ];\n\t\t}\n\t}\n\n\treturn result;\n}\n"], "mappings": ";AAGA,OAAO,mBAAmB;AAYX,SAAR,oBAAsC,MAAM,UAAW;AAE7D,MAAK,CAAE,MAAO;AACb,WAAO;AAAA,EACR;AAEA,MAAI,aAAa;AACjB,QAAM,SAAS,CAAC;AAChB,aAAY,OAAO,UAAW;AAC7B,QAAK,cAAe,KAAM,GAAI,GAAG,SAAU,GAAI,CAAE,GAAI;AACpD,aAAQ,GAAI,IAAI,KAAM,GAAI;AAAA,IAC3B,OAAO;AACN,mBAAa;AACb,aAAQ,GAAI,IAAI,SAAU,GAAI;AAAA,IAC/B;AAAA,EACD;AAEA,MAAK,CAAE,YAAa;AACnB,WAAO;AAAA,EACR;AAKA,aAAY,OAAO,MAAO;AACzB,QAAK,CAAE,OAAO,eAAgB,GAAI,GAAI;AACrC,aAAQ,GAAI,IAAI,KAAM,GAAI;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO;AACR;", "names": [] }