Skip to content

Commit 52a012a

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#17)
1 parent ebe5060 commit 52a012a

File tree

4 files changed

+69
-52
lines changed

4 files changed

+69
-52
lines changed

index.d.ts

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
1-
export type Reviver = (this: unknown, key: string, value: unknown) => unknown;
2-
export type BeforeParse = (data: string) => string;
1+
import {JsonValue} from 'type-fest';
32

4-
export interface Options {
3+
declare namespace loadJsonFile {
4+
type Reviver = (this: unknown, key: string, value: any) => unknown;
5+
type BeforeParse = (data: string) => string;
6+
7+
interface Options {
8+
/**
9+
Applies a function to the JSON string before parsing.
10+
*/
11+
readonly beforeParse?: BeforeParse;
12+
13+
/**
14+
Prescribes how the value originally produced by parsing is transformed, before being returned.
15+
See the [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more.
16+
*/
17+
readonly reviver?: Reviver;
18+
}
19+
}
20+
21+
declare const loadJsonFile: {
522
/**
6-
* Applies a function to the JSON string before parsing.
7-
*/
8-
readonly beforeParse?: BeforeParse;
23+
Read and parse a JSON file.
24+
25+
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
26+
27+
@example
28+
```
29+
import loadJsonFile = require('load-json-file');
30+
31+
(async () => {
32+
const json = await loadJsonFile('foo.json');
33+
//=> {foo: true}
34+
})();
35+
```
36+
*/
37+
<T = JsonValue>(filePath: string, options?: loadJsonFile.Options): Promise<T>;
938

1039
/**
11-
* Prescribes how the value originally produced by parsing is transformed, before being returned.
12-
* See the [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more.
13-
*/
14-
readonly reviver?: Reviver;
15-
}
40+
Read and parse a JSON file.
41+
42+
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
43+
44+
@example
45+
```
46+
import loadJsonFile = require('load-json-file');
47+
48+
const json = loadJsonFile.sync('foo.json');
49+
//=> {foo: true}
50+
```
51+
*/
52+
sync<T = JsonValue>(filePath: string, options?: loadJsonFile.Options): T;
53+
54+
// TODO: Remove this for the next major release
55+
default: typeof loadJsonFile;
56+
};
1657

17-
/**
18-
* Read and parse a JSON file.
19-
*
20-
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
21-
*
22-
* @example
23-
*
24-
* import loadJsonFile from 'load-json-file';
25-
*
26-
* (async () => {
27-
* const json = await loadJsonFile('foo.json');
28-
* //=> {foo: true}
29-
* })();
30-
*/
31-
export default function loadJsonFile<T = unknown>(filePath: string, options?: Options): Promise<T>;
32-
33-
/**
34-
* Read and parse a JSON file.
35-
*
36-
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
37-
*
38-
* @example
39-
*
40-
* import * as loadJsonFile from 'load-json-file';
41-
*
42-
* const json = loadJsonFile.sync('foo.json');
43-
* //=> {foo: true}
44-
*/
45-
export function sync<T = unknown>(filePath: string, options?: Options): T;
58+
export = loadJsonFile;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ const parse = (data, filePath, options = {}) => {
1818
const loadJsonFile = (filePath, options) => pify(fs.readFile)(filePath, 'utf8').then(data => parse(data, filePath, options));
1919

2020
module.exports = loadJsonFile;
21+
// TODO: Remove this for the next major release
2122
module.exports.default = loadJsonFile;
2223
module.exports.sync = (filePath, options) => parse(fs.readFileSync(filePath, 'utf8'), filePath, options);

index.test-d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import {expectType} from 'tsd-check';
2-
import loadJsonFile, {sync, Reviver, BeforeParse} from '.';
1+
import {expectType} from 'tsd';
2+
import {JsonValue} from 'type-fest';
3+
import loadJsonFile = require('.');
4+
import {Reviver, BeforeParse} from '.';
35

46
expectType<Reviver>(() => 1);
57
expectType<Reviver>((a: string) => a.length);
68
expectType<Reviver>((a: string, b: string) => a.length - b.length);
79

8-
expectType<BeforeParse>((data) => data);
10+
expectType<BeforeParse>(data => data);
911

10-
expectType<unknown>(await loadJsonFile('unicorn.json'));
12+
expectType<Promise<JsonValue>>(loadJsonFile('unicorn.json'));
1113

12-
expectType<unknown>(sync('unicorn.json'));
14+
expectType<JsonValue>(loadJsonFile.sync('unicorn.json'));

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -29,14 +29,15 @@
2929
"load"
3030
],
3131
"dependencies": {
32-
"graceful-fs": "^4.1.2",
32+
"graceful-fs": "^4.1.15",
3333
"parse-json": "^4.0.0",
34-
"pify": "^3.0.0",
35-
"strip-bom": "^3.0.0"
34+
"pify": "^4.0.1",
35+
"strip-bom": "^3.0.0",
36+
"type-fest": "^0.3.0"
3637
},
3738
"devDependencies": {
38-
"ava": "*",
39-
"tsd-check": "^0.2.1",
40-
"xo": "*"
39+
"ava": "^1.4.1",
40+
"tsd": "^0.7.2",
41+
"xo": "^0.24.0"
4142
}
4243
}

0 commit comments

Comments
 (0)