From 0d1397aba631f8c4b66f702e0172a1d615c68e2e Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Tue, 1 Apr 2014 00:42:13 +0200 Subject: [PATCH] added definitions for json-pointer --- README.md | 1 + json-pointer/json-pointer-tests.ts | 41 +++++++++++++++ json-pointer/json-pointer.d.ts | 83 ++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 json-pointer/json-pointer-tests.ts create mode 100644 json-pointer/json-pointer.d.ts diff --git a/README.md b/README.md index b2149c5cb..e41116fa1 100755 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ List of Definitions * [jScrollPane](http://jscrollpane.kelvinluck.com) (by [Dániel Tar](https://github.com/qcz)) * [JSDeferred](http://cho45.stfuawsc.com/jsdeferred/) (by [Daisuke Mino](https://github.com/minodisk)) * [JSONEditorOnline](https://github.com/josdejong/jsoneditoronline) (by [Vincent Bortone](https://github.com/vbortone/)) +* [JSON-Pointer](https://www.npmjs.org/package/json-pointer) (by [Bart van der Schoor](https://github.com/Bartvds)) * [jStorage](http://www.jstorage.info/) (by [Danil Flores](https://github.com/dflor003/)) * [JWPlayer](http://developer.longtailvideo.com/trac/) (by [Martin Duparc](https://github.com/martinduparc/)) * [KeyboardJS](https://github.com/RobertWHurst/KeyboardJS) (by [Vincent Bortone](https://github.com/vbortone/)) diff --git a/json-pointer/json-pointer-tests.ts b/json-pointer/json-pointer-tests.ts new file mode 100644 index 000000000..79297c0f0 --- /dev/null +++ b/json-pointer/json-pointer-tests.ts @@ -0,0 +1,41 @@ +/// + +import JsonPointer = require('json-pointer'); + +var value: any; +var str: string; +var strArr: string[]; +var pointer: string; +var bool: any; +var object:Object; + +bool = JsonPointer.has(object, pointer); +value = JsonPointer.get(object, pointer); +JsonPointer.set(object, pointer, value); +JsonPointer.remove(object, pointer); + +object = JsonPointer.dict(object); + +JsonPointer.walk(object, (elem) => { + +}); + +str = JsonPointer.escape(str); +str = JsonPointer.unescape(str); + +strArr = JsonPointer.parse(str); +str = JsonPointer.compile(strArr); + +var wrap = JsonPointer(object); + +bool = wrap.has(pointer); +value = wrap.get(pointer); +wrap.set(pointer, value); +wrap.remove(pointer); + +object = wrap.dict(); + +wrap.walk((elem, key) => { + value = elem; + str = key; +}); \ No newline at end of file diff --git a/json-pointer/json-pointer.d.ts b/json-pointer/json-pointer.d.ts new file mode 100644 index 000000000..fa8b57f4c --- /dev/null +++ b/json-pointer/json-pointer.d.ts @@ -0,0 +1,83 @@ +// Type definitions for json-pointer 1.0 l +// Project: https://www.npmjs.org/package/json-pointer +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "json-pointer" { + function JSON_Pointer(object: Object): JSON_Pointer.JSON_PointerWrap; + + module JSON_Pointer { + /** + * Wrap an object with accessors + */ + /** + * Looks up a JSON pointer in an object. + */ + function get(object: Object, pointer: string): any; + /** + * Set a value for a JSON pointer on object. + */ + function set(object: Object, pointer: string, value: any): void; + /** + * Removes an attribute of object referenced by pointer + */ + function remove(object: Object, pointer: string): void; + /** + * Creates a dictionary object (pointer -> value). + */ + function dict(object: Object): Object; + /** + * Just like: each(pointer.dict(obj), iterator); + */ + function walk(object: Object, iterator: (value: any, key: string) => void): void; + /** + * Tests if an object has a value for a JSON pointer. + */ + function has(object: Object, pointer: string): boolean; + /** + * Escapes a reference token. + */ + function escape(str: string): string; + /** + * Unescape a reference token. + */ + function unescape(str: string): string; + /** + * Converts a JSON pointer into an array of reference tokens. + */ + function parse(str: string): string[]; + /** + * Builds a json pointer from an array of reference tokens. + */ + function compile(str: string[]): string; + + interface JSON_PointerWrap { + /** + * Looks up a JSON pointer in an object. + */ + get(pointer: string): any; + /** + * Set a value for a JSON pointer on object. + */ + set(pointer: string, value: any): void; + /** + * Removes an attribute of object referenced by pointer + */ + remove(pointer: string): void; + /** + * Creates a dictionary object (pointer -> value). + */ + dict(): Object; + /** + * Just like: each(pointer.dict(obj), iterator); + */ + walk(iterator: (value: any, key: string) => void): void; + /** + * Tests if an object has a value for a JSON pointer. + */ + has(pointer: string): boolean; + } + } + + export = JSON_Pointer; +}