Skip to content

Commit bbfe42f

Browse files
committed
feat(ts-definitions): Improve TypeScript definitions, add tests for types
1 parent 49e230d commit bbfe42f

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@ node_js:
44
- 10
55
- 12
66
- 14
7+
script: >
8+
node_version="$(node -v)";
9+
if [ "${node_version:1:1}" -eq 8 ]; then
10+
# tsd test runner doesn't work on node 8
11+
npm run test:no-types;
12+
else
13+
npm test;
14+
fi
715
after_script:
816
- coveralls < coverage/lcov.info

index.d.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,66 @@
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+
}
466
}

package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
"json-stable-stringify": "latest",
1515
"nyc": "^14.1.0",
1616
"pre-commit": "^1.2.2",
17-
"tape": "^4.11.0"
17+
"tape": "^4.11.0",
18+
"tsd": "^0.15.1"
1819
},
1920
"scripts": {
2021
"eslint": "eslint index.js test",
2122
"test-spec": "tape test/*.js",
22-
"test": "npm run eslint && nyc npm run test-spec"
23+
"test-types": "tsd",
24+
"test": "npm run eslint && nyc npm run test-spec && npm run test-types",
25+
"test:no-types": "npm run eslint && nyc npm run test-spec"
2326
},
2427
"repository": {
2528
"type": "git",
@@ -49,6 +52,15 @@
4952
"text-summary"
5053
]
5154
},
55+
"tsd": {
56+
"directory": "test",
57+
"compilerOptions": {
58+
"lib": [
59+
"es2015"
60+
],
61+
"skipLibCheck": true
62+
}
63+
},
5264
"engines": {
5365
"node": ">=8"
5466
}

test/index.test-d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expectType } from "tsd";
2+
import stringify = require("../index");
3+
4+
// test type exports
5+
type CompareDescriptor = stringify.CompareDescriptor;
6+
type Comparator = stringify.Comparator;
7+
type Options = stringify.Options;
8+
9+
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
10+
11+
expectType<string>(stringify(obj));
12+
expectType<string>(
13+
stringify(obj, (a, b) => {
14+
expectType<CompareDescriptor>(a);
15+
expectType<CompareDescriptor>(b);
16+
return a.key < b.key ? 1 : -1;
17+
})
18+
);
19+
expectType<string>(
20+
stringify(obj, {
21+
cmp(a, b) {
22+
expectType<CompareDescriptor>(a);
23+
expectType<CompareDescriptor>(b);
24+
return a.key < b.key ? 1 : -1;
25+
},
26+
})
27+
);
28+
expectType<string>(stringify(obj, { cycles: true }));

0 commit comments

Comments
 (0)