|
1 | | -declare module 'fast-json-stable-stringify' { |
2 | | - function stringify(obj: any): string; |
3 | | - export = stringify; |
| 1 | +export = stringify; |
| 2 | + |
| 3 | +/** |
| 4 | + * Deterministic `JSON.stringify()`. |
| 5 | + * |
| 6 | + * @returns A deterministic stringified string from the object `obj`. |
| 7 | + * |
| 8 | + * @example |
| 9 | + * import stringify = require('fast-json-stable-stringify'); |
| 10 | + * |
| 11 | + * const obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; |
| 12 | + * console.log(stringify(obj)); |
| 13 | + * // -> {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8} |
| 14 | + */ |
| 15 | +declare function stringify( |
| 16 | + obj: any, |
| 17 | + options?: stringify.Options | stringify.Comparator |
| 18 | +): string; |
| 19 | + |
| 20 | +declare namespace stringify { |
| 21 | + interface Options { |
| 22 | + /** |
| 23 | + * You can supply a custom comparison function for object keys. |
| 24 | + * |
| 25 | + * @example |
| 26 | + * // For example, to sort on the object key names in reverse order you could write: |
| 27 | + * |
| 28 | + * import stringify = require('fast-json-stable-stringify'); |
| 29 | + * |
| 30 | + * const obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; |
| 31 | + * const s = stringify(obj, (a, b) => { |
| 32 | + * return a.key < b.key ? 1 : -1; |
| 33 | + * }); |
| 34 | + * console.log(s); |
| 35 | + * // -> {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3} |
| 36 | + * |
| 37 | + * @example |
| 38 | + * // Or if you wanted to sort on the object values in reverse order, you could write: |
| 39 | + * |
| 40 | + * import stringify = require('fast-json-stable-stringify'); |
| 41 | + * |
| 42 | + * const obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 }; |
| 43 | + * const s = stringify(obj, (a, b) => { |
| 44 | + * return a.value < b.value ? 1 : -1; |
| 45 | + * }); |
| 46 | + * console.log(s); |
| 47 | + * // -> {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10} |
| 48 | + */ |
| 49 | + cmp?: Comparator; |
| 50 | + |
| 51 | + /** |
| 52 | + * Pass `true` to stringify circular property as `__cycle__` - the result will not be |
| 53 | + * a valid JSON string in this case. |
| 54 | + * |
| 55 | + * TypeError will be thrown in case of circular object without this option. |
| 56 | + */ |
| 57 | + cycles?: boolean; |
| 58 | + } |
| 59 | + |
| 60 | + type Comparator = (a: CompareDescriptor, b: CompareDescriptor) => number; |
| 61 | + |
| 62 | + interface CompareDescriptor { |
| 63 | + key: string; |
| 64 | + value: any; |
| 65 | + } |
4 | 66 | } |
0 commit comments