From 095ae0132fcad55dc7ca628102dc04c532164ce2 Mon Sep 17 00:00:00 2001 From: nixx quality Date: Sun, 28 Oct 2018 22:59:40 +0100 Subject: [PATCH] [@types/deep-diff] improve type safety and be up to date with 1.0.2 --- types/deep-diff/deep-diff-tests.ts | 4 +- types/deep-diff/index.d.ts | 81 ++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/types/deep-diff/deep-diff-tests.ts b/types/deep-diff/deep-diff-tests.ts index 42abf98ee1..5e71badae6 100644 --- a/types/deep-diff/deep-diff-tests.ts +++ b/types/deep-diff/deep-diff-tests.ts @@ -23,7 +23,7 @@ var rhs = { } }; -var differences: deepDiff.IDiff[] = diff(lhs, rhs); +var differences: deepDiff.Diff[] = diff(lhs, rhs); console.log(differences); @@ -53,7 +53,7 @@ var rhs = { } }; -observableDiff(lhs, rhs, function (d: deepDiff.IDiff) { +observableDiff(lhs, rhs, function (d) { // Apply all changes except those to the 'name' property... if (d.path.length !== 1 || d.path.join('.') !== 'name') { applyChange(lhs, rhs, d); diff --git a/types/deep-diff/index.d.ts b/types/deep-diff/index.d.ts index de17a03bc8..bdb278b8b0 100644 --- a/types/deep-diff/index.d.ts +++ b/types/deep-diff/index.d.ts @@ -1,42 +1,79 @@ -// Type definitions for deep-diff +// TypeScript Version: 2.3 +// Type definitions for deep-diff 1.0.2 // Project: https://github.com/flitbit/diff/ // Definitions by: ZauberNerd // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace deepDiff { - interface IDiff { - kind: string; - path: string[]; - lhs: any; - rhs: any; - index?: number; - item?: IDiff; + + interface DiffNew { + kind: Kind; + path?: any[]; + rhs: RHS; } - interface IAccumulator { - push(diff: IDiff): void; + interface DiffDeleted { + kind: Kind; + path?: any[]; + lhs: LHS; + } + + interface DiffEdit { + kind: Kind; + path?: any[]; + lhs: LHS; + rhs: RHS; + } + + interface DiffArray { + kind: Kind; + path?: any[]; + index: number; + item: Diff; + } + + type Diff = DiffNew | DiffDeleted | DiffEdit | DiffArray; + + interface PreFilterFunction { + (path: any[], key: any): boolean; + } + interface PreFilterObject { + prefilter?(path: any[], key: any): boolean; + normalize?(currentPath: any, key: any, lhs: LHS, rhs: RHS): [ LHS, RHS ] | undefined; + } + type PreFilter = PreFilterFunction | PreFilterObject; + + interface Accumulator { + push(diff: Diff): void; length: number; } - interface IPrefilter { - (path: string[], key: string): boolean; + interface Observer { + (diff: Diff): void; } - interface IDeepDiff { - diff(lhs: Object, rhs: Object, prefilter?: IPrefilter, acc?: IAccumulator): IDiff[]; - diff(): IDiff; - observableDiff(lhs: Object, rhs: Object, changes: Function, prefilter?: IPrefilter, path?: string[], key?: string, stack?: Object[]): void; - applyDiff(target: Object, source: Object, filter: Function): void; - applyChange(target: Object, source: Object, change: IDiff): void; - revertChange(target: Object, source: Object, change: IDiff): void; + interface Filter { + (target: LHS, source: RHS, change: Diff): boolean; + } + + interface DeepDiff { + diff(lhs: LHS, rhs: RHS, prefilter?: PreFilter): Diff[] | undefined; + diff(lhs: LHS, rhs: RHS, prefilter?: PreFilter, acc?: Accumulator): Accumulator; + orderIndependentDiff: typeof DeepDiff.diff; + observableDiff(lhs: LHS, rhs: RHS, observer?: Observer, prefilter?: PreFilter, orderIndependent?: boolean): Diff[]; + orderIndependentDeepDiff(lhs: LHS, rhs: RHS, changes: Diff[], prefilter: PreFilter, path: any[], key: any, stack: any[]): void; + orderIndepHash(object: any): number; + applyDiff(target: LHS, source: RHS, filter?: Filter): void; + applyChange(target: LHS, source: any, change: Diff): void; + revertChange(target: LHS, source: any, change: Diff): void; isConflict(): boolean; - noConflict(): IDeepDiff; + noConflict(): DeepDiff; } } -declare var DeepDiff: deepDiff.IDeepDiff; +declare var DeepDiff: deepDiff.DeepDiff; declare module "deep-diff" { - var diff: deepDiff.IDeepDiff; + var diff: deepDiff.DeepDiff; export = diff; }