Skip to content
Merged
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
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 = unknown>(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 = unknown>(filePath: string, options?: Options): Promise<T>;
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);
12 changes: 12 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {expectType} from 'tsd-check';
import loadJsonFile, {sync, Reviver, BeforeParse} from '.';

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

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

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

expectType<unknown>(sync('unicorn.json'));
6 changes: 4 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,7 @@
},
"devDependencies": {
"ava": "*",
"tsd-check": "^0.2.1",
"xo": "*"
}
}