diff --git a/json-patch/json-patch-tests.ts b/json-patch/json-patch-tests.ts new file mode 100644 index 0000000000..c2736c403f --- /dev/null +++ b/json-patch/json-patch-tests.ts @@ -0,0 +1,35 @@ +/// + +import jp = require("json-patch"); + +// Add property, result: {foo: 'bar'} +jsonpatch.apply({}, [{ op: 'add', path: '/foo', value: 'bar' }]); +// Add array element, result: {foo: [1, 2, 3]} +jsonpatch.apply({ foo: [1, 3] }, [{ op: 'add', path: '/foo/1', value: 2 }]); +// Complex, result: {foo: [{bar: 'baz'}]} +jsonpatch.apply({ foo: [{}] }, [{ op: 'add', path: '/foo/0/bar', value: 'baz' }]); + +// Remove property, result: {} +jsonpatch.apply({ foo: 'bar' }, [{ op: 'remove', path: '/foo' }]); +// Remove array element, result: {foo: [1, 3]} +jsonpatch.apply({ foo: [1, 2, 3] }, [{ op: 'remove', path: '/foo/1' }]); +// Complex, result: {foo: [{}]} +jsonpatch.apply({ foo: [{ bar: 'baz' }] }, [{ op: 'remove', path: '/foo/0/bar' }]); + +// Replace property, result: {foo: 1} +jsonpatch.apply({ foo: 'bar' }, [{ op: 'replace', path: '/foo', value: 1 }]); +// Repalce array element, result: {foo: [1, 4, 3]} +jsonpatch.apply({ foo: [1, 2, 3] }, [{ op: 'replace', path: '/foo/1', value: 4 }]); +// Complex, result: {foo: [{bar: 1}]} +jsonpatch.apply({ foo: [{ bar: 'baz' }] }, [{ op: 'replace', path: '/foo/0/bar', value: 1 }]); + +// Move property, result {bar: [1, 2, 3]} +jsonpatch.apply({ foo: [1, 2, 3] }, [{ op: 'move', from: '/foo', path: '/bar' }]); +// Copy property, result {foo: [1, 2, 3], bar: 2} +jsonpatch.apply({ foo: [1, 2, 3] }, [{ op: 'copy', from: '/foo/1', path: '/bar' }]); +// Test equality of property to value, result: true +jsonpatch.apply({ foo: 'bar' }, [{ op: 'test', path: '/foo', value: 'bar' }]); + +jsonpatch.compile([{ op: 'test', path: '/foo', value: 'bar' }])({ foo: 'bar' }); + +jp.apply({}, [{ op: 'add', path: '/foo', value: 'bar' }]); diff --git a/json-patch/json-patch.d.ts b/json-patch/json-patch.d.ts new file mode 100644 index 0000000000..308fe35c69 --- /dev/null +++ b/json-patch/json-patch.d.ts @@ -0,0 +1,41 @@ +// Type definitions for json-patch +// Project: https://github.com/bruth/jsonpatch-js +// Definitions by: vvakame +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module jsonpatch { + type OpPatch = AddPath | RemovePath | ReplacePath | MovePath | CopyPath | TestPath; + interface Patch { + op: string; + } + interface AddPath extends Patch { + path: string; + value: any; + } + interface RemovePath extends Patch { + path: string; + } + interface ReplacePath extends Patch { + path: string; + value: any; + } + interface MovePath extends Patch { + from: string; + path: string; + } + interface CopyPath extends Patch { + from: string; + path: string; + } + interface TestPath extends Patch { + path: string; + value: any; + } + + function apply(document: any, patches: OpPatch[]): any; + function compile(patches: OpPatch[]): (document: any) => any; +} + +declare module "json-patch" { + export = jsonpatch; +}