Type definitions for 'deep-diff', a library that supports diffing JavaScript objects

This commit is contained in:
ZauberNerd
2014-09-10 17:30:29 +01:00
parent 50dd84eb5d
commit 3d49b76ec9
2 changed files with 104 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
/// <reference path="./deep-diff.d.ts" />
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);
}
});

42
deep-diff/deep-diff.d.ts vendored Normal file
View File

@@ -0,0 +1,42 @@
// Type definitions for deep-diff
// Project: https://github.com/flitbit/diff/
// Definitions by: ZauberNerd <https://github.com/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;
}