Add json-stable-stringify v1.0.0

This commit is contained in:
mfrantz 2015-04-02 11:15:55 -07:00
parent 5f48028783
commit b793adec99
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,49 @@
///<reference path="json-stable-stringify.d.ts"/>
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);
}

View File

@ -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 <https://github.com/mhfrantz/>
// 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;
}