From e576ea16e4fae61e6f8577c9c02a94bff4f8ebcb Mon Sep 17 00:00:00 2001 From: Peter Kooijmans Date: Wed, 8 Jan 2020 00:35:08 +0100 Subject: [PATCH] Add types for json8-patch (#41449) --- types/json8-patch/index.d.ts | 81 ++++++++++++++++++++++++++ types/json8-patch/json8-patch-tests.ts | 10 ++++ types/json8-patch/tsconfig.json | 23 ++++++++ types/json8-patch/tslint.json | 1 + 4 files changed, 115 insertions(+) create mode 100644 types/json8-patch/index.d.ts create mode 100644 types/json8-patch/json8-patch-tests.ts create mode 100644 types/json8-patch/tsconfig.json create mode 100644 types/json8-patch/tslint.json diff --git a/types/json8-patch/index.d.ts b/types/json8-patch/index.d.ts new file mode 100644 index 0000000000..c70194e701 --- /dev/null +++ b/types/json8-patch/index.d.ts @@ -0,0 +1,81 @@ +// Type definitions for json8-patch 1.0 +// Project: https://github.com/sonnyp/JSON8/tree/master/packages/patch +// Definitions by: Peter Kooijmans +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Typescript Version: 2.9 + +export interface BaseOperation { + path: string; +} + +export interface AddOperation extends BaseOperation { + op: "add"; + value: any; +} + +export interface RemoveOperation extends BaseOperation { + op: "remove"; +} + +/** + * The JSON patch replace operation + */ +export interface ReplaceOperation extends BaseOperation { + op: "replace"; + value: any; +} + +/** + * The JSON patch move operation + */ +export interface MoveOperation extends BaseOperation { + op: "move"; + from: any; +} + +/** + * The JSON patch copy operation. + */ +export interface CopyOperation extends BaseOperation { + op: "copy"; + from: any; +} + +/** + * The JSON patch test operation + */ +export interface TestOperation extends BaseOperation { + op: "test"; + value: any; +} + +/** + * All possible JSON patch operations + */ +export type JsonPatchOperation = AddOperation | RemoveOperation | ReplaceOperation | MoveOperation | CopyOperation | TestOperation; + +/** + * A JSON patch as specified in RFC 6902 + */ +export type JsonPatch = JsonPatchOperation[]; + +export interface ApplyResult { + doc: any; +} + +export interface ApplyResultWithRevert extends ApplyResult { + revert: JsonPatch; +} + +export type PatchResult = ApplyResult; +export type PatchResultWithRevert = ApplyResultWithRevert; + +export function apply(doc: any, patch: JsonPatch, options?: { reversible: false }): ApplyResult; +export function apply(doc: any, patch: JsonPatch, options?: { reversible: true }): ApplyResultWithRevert; + +export function patch(doc: any, patch: JsonPatch, options?: { reversible: false }): PatchResult; +export function patch(doc: any, patch: JsonPatch, options?: { reversible: true }): PatchResultWithRevert; + +export function diff(doc1: any, doc2: any): JsonPatch; + +export function revert(doc: any, revertPatch: JsonPatch): ApplyResult; diff --git a/types/json8-patch/json8-patch-tests.ts b/types/json8-patch/json8-patch-tests.ts new file mode 100644 index 0000000000..e9b5f65849 --- /dev/null +++ b/types/json8-patch/json8-patch-tests.ts @@ -0,0 +1,10 @@ +import * as jsonPatch from "json8-patch"; + +const doc1 = { foo: "bar" }; +const doc2 = { bar: 42 }; + +const patch = jsonPatch.diff(doc1, doc2); +const { doc, revert } = jsonPatch.apply(doc1, patch, { reversible: true }); + +// $ExpectType ApplyResult +const reverted = jsonPatch.revert(doc, revert); diff --git a/types/json8-patch/tsconfig.json b/types/json8-patch/tsconfig.json new file mode 100644 index 0000000000..8a146b55e5 --- /dev/null +++ b/types/json8-patch/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", + "json8-patch-tests.ts" + ] +} diff --git a/types/json8-patch/tslint.json b/types/json8-patch/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json8-patch/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }