Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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
44 changes: 44 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export type Reviver = (key: string, value: any) => any;
export type BeforeParse = (data: string) => string;

export interface Options {
/**
* Applies a function to the JSON string before parsing.
*/
beforeParse?: BeforeParse;
/**
* Prescribes how the value originally produced by parsing is transformed, before being returned.
* 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.
*/
reviver?: Reviver;
}

/**
* Read and parse a JSON file.
*
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
*
* @example
*
* import * as loadJsonFile from 'load-json-file';
*
* const json = loadJsonFile.sync('foo.json');
* //=> {foo: true}
*/
export function sync<T = any>(filePath: string, options?: Options): T;

/**
* Read and parse a JSON file.
*
* Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
*
* @example
*
* import loadJsonFile from 'load-json-file';
*
* (async () => {
* const json = await loadJsonFile('foo.json');
* //=> {foo: true}
* })();
*/
export default function loadJsonFile<T = any>(filePath: string, options?: Options): Promise<T>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use unknown instead of any so users are forced to explicitly coerce it. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 unknown works as the default

Very cool advanced type from Typescript 3.x

5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ const parse = (data, filePath, options = {}) => {
return parseJson(data, options.reviver, path.relative(process.cwd(), filePath));
};

module.exports = (filePath, options) => pify(fs.readFile)(filePath, 'utf8').then(data => parse(data, filePath, options));
const loadJsonFile = (filePath, options) => pify(fs.readFile)(filePath, 'utf8').then(data => parse(data, filePath, options));

module.exports = loadJsonFile;
module.exports.default = loadJsonFile;
module.exports.sync = (filePath, options) => parse(fs.readFileSync(filePath, 'utf8'), filePath, options);
16 changes: 16 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {expectType} from 'tsd-check';
import loadJsonFile, {sync, Reviver, BeforeParse} from '.';

(async () => {
// To-Do => Expand Tests
// https://github.com/sindresorhus/write-json-file/blob/master/index.test-d.ts
expectType<Reviver>(() => 1);
expectType<Reviver>((a: string) => a.length);
expectType<Reviver>((a: string, b: string) => a.length - b.length);

expectType<BeforeParse>((data) => data);

expectType<void>(await loadJsonFile('unicorn.json'));

expectType<void>(sync('unicorn.json'));
})();
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"read",
Expand All @@ -35,6 +36,12 @@
},
"devDependencies": {
"ava": "*",
"tsd-check": "^0.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just released a new version (0.2.1) which supports top-level await.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 awesome! Changed it to "*"

"xo": "*"
},
"xo": {
"ignores": [
"index.d.ts"
]
}
}