Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ node_js:
- 10
- 12
- 14
script: >
node_version="$(node -v)";
if [ "${node_version:1:1}" -eq 8 ]; then
# tsd test runner doesn't work on node 8
npm run test:no-types;
else
npm test;
fi
after_script:
- coveralls < coverage/lcov.info
68 changes: 65 additions & 3 deletions index.d.ts
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;
}
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
"json-stable-stringify": "latest",
"nyc": "^14.1.0",
"pre-commit": "^1.2.2",
"tape": "^4.11.0"
"tape": "^4.11.0",
"tsd": "^0.16.0"
},
"scripts": {
"eslint": "eslint index.js test",
"test-spec": "tape test/*.js",
"test": "npm run eslint && nyc npm run test-spec"
"test-types": "tsd",
"test": "npm run eslint && nyc npm run test-spec && npm run test-types",
"test:no-types": "npm run eslint && nyc npm run test-spec"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -49,6 +52,9 @@
"text-summary"
]
},
"tsd": {
"directory": "test"
},
"engines": {
"node": ">=8"
}
Expand Down
28 changes: 28 additions & 0 deletions test/index.test-d.ts
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 }));