UNPKG

1.33 kBTypeScriptView Raw
1// From https://github.com/sindresorhus/type-fest
2export type JsonValue = string | number | boolean | null | {[Key in string]?: JsonValue} | JsonValue[];
3
4export type Reviver = (this: unknown, key: string, value: unknown) => unknown;
5export type BeforeParse = (data: string) => string;
6
7export 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/**
21Read and parse a JSON file.
22
23It also strips UTF-8 BOM.
24
25@example
26```
27import {loadJsonFile} from 'load-json-file';
28
29const json = await loadJsonFile('foo.json');
30//=> {foo: true}
31```
32*/
33export function loadJsonFile<ReturnValueType = JsonValue>(filePath: string, options?: Options): Promise<ReturnValueType>;
34
35/**
36Read and parse a JSON file.
37
38It also strips UTF-8 BOM.
39
40@example
41```
42import {loadJsonFileSync} from 'load-json-file';
43
44const json = loadJsonFileSync('foo.json');
45//=> {foo: true}
46```
47*/
48export function loadJsonFileSync<ReturnValueType = JsonValue>(filePath: string, options?: Options): ReturnValueType;