Skip to content

Commit b793ade

Browse files
author
mfrantz
committed
Add json-stable-stringify v1.0.0
1 parent 5f48028 commit b793ade

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
///<reference path="json-stable-stringify.d.ts"/>
2+
3+
import stringify = require('json-stable-stringify');
4+
5+
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
6+
7+
{
8+
console.log(stringify(obj));
9+
}
10+
11+
{
12+
// Second arg can be a stringify.Comparator function.
13+
var s: string = stringify(obj, (a: stringify.Element, b: stringify.Element): number => a.key < b.key ? 1 : -1);
14+
console.log(s);
15+
}
16+
17+
{
18+
// Can specify Comparator in an Options object.
19+
function reverse(a: stringify.Element, b: stringify.Element): number {
20+
return a.value < b.value ? 1 : -1;
21+
}
22+
var opts: stringify.Options = { cmp: reverse };
23+
var s: string = stringify(obj, opts);
24+
console.log(s);
25+
}
26+
27+
{
28+
// Space can be a string.
29+
var s: string = stringify(obj, { space: ' ' });
30+
console.log(s);
31+
}
32+
33+
{
34+
// Space can be an integer.
35+
var s: string = stringify(obj, { space: 2 });
36+
console.log(s);
37+
}
38+
39+
{
40+
// The replacer option can remove or modify values.
41+
function removeStrings(key: string, value: any): any {
42+
if (typeof value === "string") {
43+
return undefined;
44+
}
45+
return value;
46+
}
47+
var s: string = stringify(obj, { replacer: removeStrings });
48+
console.log(s);
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Type definitions for json-stable-stringify 1.0.0
2+
// Project: https://github.com/substack/json-stable-stringify
3+
// Definitions by: Matt Frantz <https://github.com/mhfrantz/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module 'json-stable-stringify' {
7+
8+
function stringify(obj: any, opts?: stringify.Comparator | stringify.Options): string;
9+
10+
module stringify {
11+
12+
interface Element {
13+
key: string;
14+
value: any;
15+
}
16+
17+
interface Comparator {
18+
(a: Element, b: Element): number;
19+
}
20+
21+
interface Replacer {
22+
(key: string, value: any): any;
23+
}
24+
25+
interface Options {
26+
cmp?: Comparator;
27+
space?: number | string;
28+
replacer?: Replacer;
29+
}
30+
}
31+
32+
export = stringify;
33+
}

0 commit comments

Comments
 (0)