diff --git a/json-stable-stringify/json-stable-stringify-tests.ts b/json-stable-stringify/json-stable-stringify-tests.ts new file mode 100644 index 0000000000..f28fb88e66 --- /dev/null +++ b/json-stable-stringify/json-stable-stringify-tests.ts @@ -0,0 +1,49 @@ +/// + +import stringify = require('json-stable-stringify'); + +var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; + +{ + console.log(stringify(obj)); +} + +{ + // Second arg can be a stringify.Comparator function. + var s: string = stringify(obj, (a: stringify.Element, b: stringify.Element): number => a.key < b.key ? 1 : -1); + console.log(s); +} + +{ + // Can specify Comparator in an Options object. + function reverse(a: stringify.Element, b: stringify.Element): number { + return a.value < b.value ? 1 : -1; + } + var opts: stringify.Options = { cmp: reverse }; + var s: string = stringify(obj, opts); + console.log(s); +} + +{ + // Space can be a string. + var s: string = stringify(obj, { space: ' ' }); + console.log(s); +} + +{ + // Space can be an integer. + var s: string = stringify(obj, { space: 2 }); + console.log(s); +} + +{ + // The replacer option can remove or modify values. + function removeStrings(key: string, value: any): any { + if (typeof value === "string") { + return undefined; + } + return value; + } + var s: string = stringify(obj, { replacer: removeStrings }); + console.log(s); +} diff --git a/json-stable-stringify/json-stable-stringify.d.ts b/json-stable-stringify/json-stable-stringify.d.ts new file mode 100644 index 0000000000..faeb7c074d --- /dev/null +++ b/json-stable-stringify/json-stable-stringify.d.ts @@ -0,0 +1,33 @@ +// Type definitions for json-stable-stringify 1.0.0 +// Project: https://github.com/substack/json-stable-stringify +// Definitions by: Matt Frantz +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'json-stable-stringify' { + + function stringify(obj: any, opts?: stringify.Comparator | stringify.Options): string; + + module stringify { + + interface Element { + key: string; + value: any; + } + + interface Comparator { + (a: Element, b: Element): number; + } + + interface Replacer { + (key: string, value: any): any; + } + + interface Options { + cmp?: Comparator; + space?: number | string; + replacer?: Replacer; + } + } + + export = stringify; +}