mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2025-10-20 02:34:16 +03:00
* 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>
46 lines
1.4 KiB
TypeScript
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,
|
|
});
|
|
}
|