From cd1bbd6f2ecd8fa2be9fb0022226edec2d88d7b7 Mon Sep 17 00:00:00 2001 From: itsFrank Date: Wed, 3 Aug 2016 21:34:35 +0200 Subject: [PATCH] Added typings for the JSON-Patch library, called 'fast-json-patch' on npm --- fast-json-patch/fast-json-patch-tests.ts | 43 +++++++++++++++ fast-json-patch/fast-json-patch.d.ts | 67 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 fast-json-patch/fast-json-patch-tests.ts create mode 100644 fast-json-patch/fast-json-patch.d.ts diff --git a/fast-json-patch/fast-json-patch-tests.ts b/fast-json-patch/fast-json-patch-tests.ts new file mode 100644 index 0000000000..e7ecc249ac --- /dev/null +++ b/fast-json-patch/fast-json-patch-tests.ts @@ -0,0 +1,43 @@ +/// +import * as jsonpatch from 'fast-json-patch' + +var myobj:{ + firstName:string, + contactDetails: { + phoneNumbers:string[] + } +} = { firstName:"Albert", contactDetails: { phoneNumbers: [ ] } }; +var patches = [ + {op:"replace", path:"/firstName", value:"Joachim" }, + {op:"add", path:"/lastName", value:"Wester" }, + {op:"add", path:"/contactDetails/phoneNumbers/0", value:{ number:"555-123" } } + ]; +jsonpatch.apply( myobj, patches ); + +var myobj2 = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } }; +var observer = jsonpatch.observe( myobj2 ); +myobj2.firstName = "Albert"; +myobj2.contactDetails.phoneNumbers[0].number = "123"; +myobj2.contactDetails.phoneNumbers.push({number:"456"}); +var patches2 = jsonpatch.generate(observer); + +var objA = {user: {firstName: "Albert", lastName: "Einstein"}}; +var objB = {user: {firstName: "Albert", lastName: "Collins"}}; +var diff = jsonpatch.compare(objA, objB); + +var obj = {user: {firstName: "Albert"}}; +var patches3 = [{op: "replace", path: "/user/firstName", value: "Albert"}, {op: "replace", path: "/user/lastName", value: "Einstein"}]; +var errors = jsonpatch.validate(patches, obj); +if (errors.length == 0) { + //there are no errors! +} +else { + for (var i=0; i < errors.length; i++) { + if (!errors[i]) { + console.log("Valid patch at index", i, patches[i]); + } + else { + console.error("Invalid patch at index", i, errors[i], patches[i]); + } + } +} diff --git a/fast-json-patch/fast-json-patch.d.ts b/fast-json-patch/fast-json-patch.d.ts new file mode 100644 index 0000000000..a855b35d36 --- /dev/null +++ b/fast-json-patch/fast-json-patch.d.ts @@ -0,0 +1,67 @@ +// Type definitions for JSON-Patch v1.0.0 +// Project: https://github.com/Starcounter-Jack/JSON-Patch/releases +// Definitions by: Francis O'Brien +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace fastjsonpatch { + + + interface JsonPatch { + /** + * Applies an array of patch instructions to an object + */ + apply(object:any, patches:Patch[], validate?:boolean):boolean + + /** + * Observes changes made to an object, which can then be retieved using generate + */ + observe(object:T, callback?:()=>void):Observer + + /** + * Detach an observer from an object + */ + unobserve(object:T, observer:Observer):void + + /** + * Generate an array of patches from an observer + */ + generate(observer:Observer):Patch[] + + /** + * Create an array of patches from the differences in two objects + */ + compare(object1:any, object2:any):Patch[] + + /** + * Ensure a set of patch instructions is valid + */ + validate(patches:Patch[], tree?:any):JsonPatchError[] + } + + interface Observer { + object:T + patches:Patch[] + unobserve():void + } + + interface Patch { + op:string + path:string + value?:any + from?:string + } + + interface JsonPatchError { + name:string + message:string + index:number + operation:any + tree:any + } +} + +declare var jsonpatch: fastjsonpatch.JsonPatch; + +declare module "fast-json-patch" { + export = jsonpatch +} \ No newline at end of file