diff --git a/deep-diff/deep-diff-tests.ts b/deep-diff/deep-diff-tests.ts new file mode 100644 index 0000000000..4377bddd1e --- /dev/null +++ b/deep-diff/deep-diff-tests.ts @@ -0,0 +1,62 @@ +/// + +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: 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: 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 0000000000..07fb0eb232 --- /dev/null +++ b/deep-diff/deep-diff.d.ts @@ -0,0 +1,42 @@ +// Type definitions for deep-diff +// Project: https://github.com/flitbit/diff/ +// Definitions by: ZauberNerd +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +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; +} + +declare module deepDiff { + export 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 var diff: deepDiff.IDeepDiff; + +declare module "deep-diff" { + export = diff; +} \ No newline at end of file