mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 19:07:08 +00:00
Add json-stable-stringify v1.0.0
This commit is contained in:
parent
5f48028783
commit
b793adec99
49
json-stable-stringify/json-stable-stringify-tests.ts
Normal file
49
json-stable-stringify/json-stable-stringify-tests.ts
Normal 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);
|
||||
}
|
||||
33
json-stable-stringify/json-stable-stringify.d.ts
vendored
Normal file
33
json-stable-stringify/json-stable-stringify.d.ts
vendored
Normal 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user