This commit is contained in:
Chris Kruining 2024-12-17 08:18:25 +01:00
parent b23db1d5a8
commit 1d88565773
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
17 changed files with 360 additions and 229 deletions

View file

@ -56,7 +56,7 @@ export enum MutarionKind {
}
export type Created = { kind: MutarionKind.Create, key: string, value: any };
export type Updated = { kind: MutarionKind.Update, key: string, value: any, original: any };
export type Deleted = { kind: MutarionKind.Delete, key: string };
export type Deleted = { kind: MutarionKind.Delete, key: string, original: any };
export type Mutation = Created | Updated | Deleted;
export function* deepDiff<T1 extends object, T2 extends object>(a: T1, b: T2, path: string[] = []): Generator<Mutation, void, unknown> {
@ -74,7 +74,7 @@ export function* deepDiff<T1 extends object, T2 extends object>(a: T1, b: T2, pa
}
if (keyA && keyB === undefined) {
yield { key: path.concat(keyA.toString()).join('.'), kind: MutarionKind.Delete };
yield { key: path.concat(keyA.toString()).join('.'), kind: MutarionKind.Delete, original: valueA };
continue;
}
@ -93,7 +93,7 @@ export function* deepDiff<T1 extends object, T2 extends object>(a: T1, b: T2, pa
yield ((): Mutation => {
if (valueA === null || valueA === undefined) return { key, kind: MutarionKind.Create, value: valueB };
if (valueB === null || valueB === undefined) return { key, kind: MutarionKind.Delete };
if (valueB === null || valueB === undefined) return { key, kind: MutarionKind.Delete, original: valueA };
return { key, kind: MutarionKind.Update, value: valueB, original: valueA };
})();