[simple-diff] Add simple-diff types (#47956)

* [simple-diff] Add simple-diff types

* Fix

* More lint

* Rename

* Fix
This commit is contained in:
Nathan Bierema 2020-09-23 15:05:06 -04:00 committed by GitHub
parent 4fd4a572b9
commit 523b733dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 0 deletions

101
types/simple-diff/index.d.ts vendored Normal file
View File

@ -0,0 +1,101 @@
// Type definitions for simple-diff 1.6
// Project: https://github.com/redexp/simple-diff#readme
// Definitions by: Nathan Bierema <https://github.com/Methuselah96>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace simpleDiff {
type Path = Array<string | number>;
interface AddEvent {
oldPath: Path;
newPath: Path;
type: 'add';
oldValue: undefined;
newValue: unknown;
}
interface RemoveEvent {
oldPath: Path;
newPath: Path;
type: 'remove';
oldValue: unknown;
newValue: undefined;
}
interface ChangeEvent {
oldPath: Path;
newPath: Path;
type: 'change';
oldValue: unknown;
newValue: unknown;
}
interface AddItemEvent {
oldPath: Path;
newPath: Path;
type: 'add-item';
oldIndex: -1;
curIndex: -1;
newIndex: number;
newValue: unknown;
}
interface RemoveItemEvent {
oldPath: Path;
newPath: Path;
type: 'remove-item';
oldIndex: number;
curIndex: number;
newIndex: -1;
oldValue: unknown;
}
interface MoveItemEvent {
oldPath: Path;
newPath: Path;
type: 'move-item';
oldIndex: number;
curIndex: number;
newIndex: number;
}
type Event =
| AddEvent
| RemoveEvent
| ChangeEvent
| AddItemEvent
| RemoveItemEvent
| MoveItemEvent;
interface Options {
idProp?: string;
idProps?: {
[path: string]: string;
};
comparators?: Array<[
unknown,
(
oldValue: unknown,
newValue: unknown,
options: { oldPath: Path; newPath: Path }
) => boolean
]>;
ignore?: (
oldValue: unknown,
newValue: unknown,
options: { oldPath: Path; newPath: Path }
) => boolean;
callback?: (event: Event) => void;
addEvent?: string;
changeEvent?: string;
removeEvent?: string;
addItemEvent?: string;
removeItemEvent?: string;
moveItemEvent?: string;
}
}
declare function simpleDiff(oldObj: unknown, newObj: unknown, options?: simpleDiff.Options): simpleDiff.Event[];
export as namespace simpleDiff;
export = simpleDiff;

View File

@ -0,0 +1,17 @@
import diff = require('simple-diff');
declare const oldObject: unknown;
declare const newObject: unknown;
const changes = diff(oldObject, newObject, {
idProp: '_id',
idProps: {
"prop1.prop2.*.prop3": "cid"
},
comparators: [
[Date, (a, b, ops) => true]
],
ignore: (a, b, { oldPath }) => {
return oldPath.join('.') === 'prop.test1';
}
});

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"simple-diff-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }