diff --git a/deep-diff/deep-diff-tests.ts b/deep-diff/deep-diff-tests.ts new file mode 100644 index 000000000..1392effb1 --- /dev/null +++ b/deep-diff/deep-diff-tests.ts @@ -0,0 +1,61 @@ +/// + +import _deepDiff = require('deep-diff'); +var diff = _deepDiff.diff; + +var lhs = { + name: 'my object', + description: 'it\'s an object!', + details: { + it: 'has', + an: 'array', + with: ['a', 'few', 'elements'] + } +}; + +var rhs = { + name: 'updated object', + description: 'it\'s an object!', + details: { + it: 'has', + an: 'array', + with: ['a', 'few', 'more', 'elements', { than: 'before' }] + } +}; + +var differences: deepDiff.IDiff[] = diff(lhs, rhs); + +console.log(differences); + + +// -------------------------- + +var observableDiff = _deepDiff.observableDiff; +var applyChange = _deepDiff.applyChange; + +var lhs = { + name: 'my object', + description: 'it\'s an object!', + details: { + it: 'has', + an: 'array', + with: ['a', 'few', 'elements'] + } +}; + +var rhs = { + name: 'updated object', + description: 'it\'s an object!', + details: { + it: 'has', + an: 'array', + with: ['a', 'few', 'more', 'elements', { than: 'before' }] + } +}; + +observableDiff(lhs, rhs, function (d: deepDiff.IDiff) { + // 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/deep-diff/deep-diff.d.ts b/deep-diff/deep-diff.d.ts new file mode 100644 index 000000000..1b96960a3 --- /dev/null +++ b/deep-diff/deep-diff.d.ts @@ -0,0 +1,40 @@ +// Type definitions for deep-diff +// Project: https://github.com/flitbit/diff/ +// Definitions by: ZauberNerd +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module deepDiff { + interface IDiff { + kind: string; + path: string[]; + lhs: any; + rhs: any; + index?: number; + item?: IDiff; + } + + interface IAccumulator { + push(diff: IDiff): void; + length: number; + } + + interface IPrefilter { + (path: string[], key: string): boolean; + } + + interface IDeepDiff { + diff(): IDiff; + diff(lhs: Object, rhs: Object, prefilter?: IPrefilter, acc?: IAccumulator): 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; + isConflict(): boolean; + noConflict(): IDeepDiff; + } +} + +declare module "deep-diff" { + var diff: deepDiff.IDeepDiff; + export = diff; +}