forked from carlos8f/json-stable-stringify
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts-definitions): Improve TypeScript definitions, add tests for t…
…ypes
- Loading branch information
1 parent
49e230d
commit 4ea7150
Showing
5 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,66 @@ | ||
declare module 'fast-json-stable-stringify' { | ||
function stringify(obj: any): string; | ||
export = stringify; | ||
export = stringify; | ||
|
||
/** | ||
* Deterministic `JSON.stringify()`. | ||
* | ||
* @returns A deterministic stringified string from the object `obj`. | ||
* | ||
* @example | ||
* import stringify = require('fast-json-stable-stringify'); | ||
* | ||
* const obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; | ||
* console.log(stringify(obj)); | ||
* // -> {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8} | ||
*/ | ||
declare function stringify( | ||
obj: any, | ||
options?: stringify.Options | stringify.Comparator | ||
): string; | ||
|
||
declare namespace stringify { | ||
interface Options { | ||
/** | ||
* You can supply a custom comparison function for object keys. | ||
* | ||
* @example | ||
* // For example, to sort on the object key names in reverse order you could write: | ||
* | ||
* import stringify = require('fast-json-stable-stringify'); | ||
* | ||
* const obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; | ||
* const s = stringify(obj, (a, b) => { | ||
* return a.key < b.key ? 1 : -1; | ||
* }); | ||
* console.log(s); | ||
* // -> {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3} | ||
* | ||
* @example | ||
* // Or if you wanted to sort on the object values in reverse order, you could write: | ||
* | ||
* import stringify = require('fast-json-stable-stringify'); | ||
* | ||
* const obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 }; | ||
* const s = stringify(obj, (a, b) => { | ||
* return a.value < b.value ? 1 : -1; | ||
* }); | ||
* console.log(s); | ||
* // -> {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10} | ||
*/ | ||
cmp?: Comparator; | ||
|
||
/** | ||
* Pass `true` to stringify circular property as `__cycle__` - the result will not be | ||
* a valid JSON string in this case. | ||
* | ||
* TypeError will be thrown in case of circular object without this option. | ||
*/ | ||
cycles?: boolean; | ||
} | ||
|
||
type Comparator = (a: CompareDescriptor, b: CompareDescriptor) => number; | ||
|
||
interface CompareDescriptor { | ||
key: string; | ||
value: any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expectType } from "tsd"; | ||
import stringify = require("../index"); | ||
|
||
// test type exports | ||
type CompareDescriptor = stringify.CompareDescriptor; | ||
type Comparator = stringify.Comparator; | ||
type Options = stringify.Options; | ||
|
||
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; | ||
|
||
expectType<string>(stringify(obj)); | ||
expectType<string>( | ||
stringify(obj, (a, b) => { | ||
expectType<CompareDescriptor>(a); | ||
expectType<CompareDescriptor>(b); | ||
return a.key < b.key ? 1 : -1; | ||
}) | ||
); | ||
expectType<string>( | ||
stringify(obj, { | ||
cmp(a, b) { | ||
expectType<CompareDescriptor>(a); | ||
expectType<CompareDescriptor>(b); | ||
return a.key < b.key ? 1 : -1; | ||
}, | ||
}) | ||
); | ||
expectType<string>(stringify(obj, { cycles: true })); |