mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-03-30 08:54:30 +08:00
Type definitions for 'deep-diff', a library that supports diffing JavaScript objects
This commit is contained in:
62
deep-diff/deep-diff-tests.ts
Normal file
62
deep-diff/deep-diff-tests.ts
Normal 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
42
deep-diff/deep-diff.d.ts
vendored
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user