From 523b733dbe1b4195af59936a8d74ca27dbf59cdb Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 23 Sep 2020 15:05:06 -0400 Subject: [PATCH] [simple-diff] Add simple-diff types (#47956) * [simple-diff] Add simple-diff types * Fix * More lint * Rename * Fix --- types/simple-diff/index.d.ts | 101 +++++++++++++++++++++++++ types/simple-diff/simple-diff-tests.ts | 17 +++++ types/simple-diff/tsconfig.json | 23 ++++++ types/simple-diff/tslint.json | 1 + 4 files changed, 142 insertions(+) create mode 100644 types/simple-diff/index.d.ts create mode 100644 types/simple-diff/simple-diff-tests.ts create mode 100644 types/simple-diff/tsconfig.json create mode 100644 types/simple-diff/tslint.json diff --git a/types/simple-diff/index.d.ts b/types/simple-diff/index.d.ts new file mode 100644 index 0000000000..112c41b6d3 --- /dev/null +++ b/types/simple-diff/index.d.ts @@ -0,0 +1,101 @@ +// Type definitions for simple-diff 1.6 +// Project: https://github.com/redexp/simple-diff#readme +// Definitions by: Nathan Bierema +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace simpleDiff { + type Path = Array; + + 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; diff --git a/types/simple-diff/simple-diff-tests.ts b/types/simple-diff/simple-diff-tests.ts new file mode 100644 index 0000000000..d95095766b --- /dev/null +++ b/types/simple-diff/simple-diff-tests.ts @@ -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'; + } +}); diff --git a/types/simple-diff/tsconfig.json b/types/simple-diff/tsconfig.json new file mode 100644 index 0000000000..73c005731c --- /dev/null +++ b/types/simple-diff/tsconfig.json @@ -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" + ] +} diff --git a/types/simple-diff/tslint.json b/types/simple-diff/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/simple-diff/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }