redux-devtools/packages/redux-devtools-inspector-monitor/src/createDiffPatcher.ts
renovate[bot] 9e4e054f8b
fix(deps): update dependency jsondiffpatch to ^0.6.0 (#1586)
* fix(deps): update dependency jsondiffpatch to ^0.6.0

* Cleanup renovate.json

* Code updates

* Update test config

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Nathan Bierema <nbierema@gmail.com>
2024-01-23 21:48:11 -05:00

46 lines
1.4 KiB
TypeScript

import { DiffPatcher } from 'jsondiffpatch';
import type { DiffContext } from 'jsondiffpatch';
const defaultObjectHash = (obj: object, idx: number | undefined) => {
const o = obj as Record<string, unknown>;
return (
(o === null && '$$null') ||
(o && (o.id || o.id === 0) && `$$id:${JSON.stringify(o.id)}`) ||
(o && (o._id || o._id === 0) && `$$_id:${JSON.stringify(o._id)}`) ||
`$$index:${idx}`
);
};
const defaultPropertyFilter = (name: string, context: DiffContext) =>
typeof (context.left as Record<string, unknown>)[name] !== 'function' &&
typeof (context.right as Record<string, unknown>)[name] !== 'function';
const defaultDiffPatcher = new DiffPatcher({
arrays: { detectMove: false } as {
detectMove: boolean;
includeValueOnMove: boolean;
},
objectHash: defaultObjectHash,
propertyFilter: defaultPropertyFilter,
});
export default function createDiffPatcher(
objectHash:
| ((item: unknown, index: number | undefined) => string)
| undefined,
propertyFilter: ((name: string, context: DiffContext) => boolean) | undefined,
) {
if (!objectHash && !propertyFilter) {
return defaultDiffPatcher;
}
return new DiffPatcher({
arrays: { detectMove: false } as {
detectMove: boolean;
includeValueOnMove: boolean;
},
objectHash: objectHash || defaultObjectHash,
propertyFilter: propertyFilter || defaultPropertyFilter,
});
}