{"version":3,"file":"mergeWithStableProps.mjs","sources":["../../../../../../js-utils/src/mergeWithStableProps.ts"],"sourcesContent":["import { createDeepEqual } from './createDeepEqual';\n\nconst deepEqual = createDeepEqual();\n\n/**\n * Returns a version of `newValue` whose properties that are deeply equal to\n * those in `oldValue` are replaced with those from `oldValue`. This provides a\n * limited form of \"structural sharing\" that provides a stable reference for\n * unchanged slices of the object.\n *\n * If `oldValue` and `newValue` are referentially equal, the same value is\n * returned.\n *\n * @param oldValue The old value\n * @param newValue The new value\n */\nexport const mergeWithStableProps = <T extends Record<string, unknown>, U extends Record<string, unknown> = T>(\n  oldValue: U,\n  newValue: T,\n): T => {\n  // If the values are already referentially the same, just return the new value\n  if ((oldValue as unknown) === newValue) {\n    return newValue;\n  }\n\n  return Object.keys(oldValue).reduce(\n    (acc, key) => {\n      if (key in newValue && deepEqual(oldValue[key], newValue[key])) {\n        acc[key as keyof T] = oldValue[key] as unknown as T[keyof T];\n      }\n      return acc;\n    },\n    { ...newValue },\n  );\n};\n"],"names":["deepEqual","createDeepEqual","mergeWithStableProps","oldValue","newValue","Object","keys","reduce","acc","key"],"mappings":";;AAEA,MAAMA,SAAAA,GAAYC,eAAAA,EAAAA;AAElB;;;;;;;;;;;AAWC,IACM,MAAMC,oBAAAA,GAAuB,CAClCC,QAAAA,EACAC,QAAAA,GAAAA;;IAGA,IAAKD,aAAyBC,QAAAA,EAAU;QACtC,OAAOA,QAAAA;AACT,IAAA;AAEA,IAAA,OAAOC,OAAOC,IAAI,CAACH,UAAUI,MAAM,CACjC,CAACC,GAAAA,EAAKC,GAAAA,GAAAA;QACJ,IAAIA,GAAAA,IAAOL,QAAAA,IAAYJ,SAAAA,CAAUG,QAAQ,CAACM,IAAI,EAAEL,QAAQ,CAACK,GAAAA,CAAI,CAAA,EAAG;AAC9DD,YAAAA,GAAG,CAACC,GAAAA,CAAe,GAAGN,QAAQ,CAACM,GAAAA,CAAI;AACrC,QAAA;QACA,OAAOD,GAAAA;IACT,CAAA,EACA;AAAE,QAAA,GAAGJ;AAAS,KAAA,CAAA;AAElB;;;;"}