Skip to content

Commit 9138c28

Browse files
committed
Require Node.js 12.20 and move to ESM
Fixes #23
1 parent e99664d commit 9138c28

File tree

8 files changed

+83
-101
lines changed

8 files changed

+83
-101
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

index.d.ts

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
1-
import {JsonValue} from 'type-fest';
2-
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-
}
1+
// From https://github.com/sindresorhus/type-fest
2+
export type JsonValue = string | number | boolean | null | {[Key in string]?: JsonValue} | JsonValue[];
3+
4+
export type Reviver = (this: unknown, key: string, value: unknown) => unknown;
5+
export type BeforeParse = (data: string) => string;
6+
7+
export interface Options {
8+
/**
9+
Applies a function to the JSON string before parsing.
10+
*/
11+
readonly beforeParse?: BeforeParse;
2012

21-
declare const loadJsonFile: {
2213
/**
23-
Read and parse a JSON file.
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+
}
2419

25-
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
20+
/**
21+
Read and parse a JSON file.
2622
27-
@example
28-
```
29-
import loadJsonFile = require('load-json-file');
23+
It also strips UTF-8 BOM.
3024
31-
(async () => {
32-
const json = await loadJsonFile('foo.json');
33-
//=> {foo: true}
34-
})();
35-
```
36-
*/
37-
<ReturnValueType = JsonValue>(filePath: string, options?: loadJsonFile.Options): Promise<ReturnValueType>;
25+
@example
26+
```
27+
import {loadJsonFile} from 'load-json-file';
3828
39-
/**
40-
Read and parse a JSON file.
29+
const json = await loadJsonFile('foo.json');
30+
//=> {foo: true}
31+
```
32+
*/
33+
export function loadJsonFile<ReturnValueType = JsonValue>(filePath: string, options?: Options): Promise<ReturnValueType>;
4134

42-
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
35+
/**
36+
Read and parse a JSON file.
4337
44-
@example
45-
```
46-
import loadJsonFile = require('load-json-file');
38+
It also strips UTF-8 BOM.
4739
48-
const json = loadJsonFile.sync('foo.json');
49-
//=> {foo: true}
50-
```
51-
*/
52-
sync<ReturnValueType = JsonValue>(filePath: string, options?: loadJsonFile.Options): ReturnValueType;
53-
};
40+
@example
41+
```
42+
import {loadJsonFileSync} from 'load-json-file';
5443
55-
export = loadJsonFile;
44+
const json = loadJsonFileSync('foo.json');
45+
//=> {foo: true}
46+
```
47+
*/
48+
export function loadJsonFileSync<ReturnValueType = JsonValue>(filePath: string, options?: Options): ReturnValueType;

index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
'use strict';
2-
const path = require('path');
3-
const {promisify} = require('util');
4-
const fs = require('graceful-fs');
5-
const stripBom = require('strip-bom');
6-
const parseJson = require('parse-json');
1+
import {readFileSync} from 'node:fs';
2+
import {readFile} from 'node:fs/promises';
73

8-
const parse = (data, filePath, options = {}) => {
9-
data = stripBom(data);
4+
const parse = (buffer, {beforeParse, reviver} = {}) => {
5+
// Unlike `buffer.toString()` and `fs.readFile(path, 'utf8')`, `TextDecoder`` will remove BOM.
6+
let data = new TextDecoder().decode(buffer);
107

11-
if (typeof options.beforeParse === 'function') {
12-
data = options.beforeParse(data);
8+
if (typeof beforeParse === 'function') {
9+
data = beforeParse(data);
1310
}
1411

15-
return parseJson(data, options.reviver, path.relative(process.cwd(), filePath));
12+
return JSON.parse(data, reviver);
1613
};
1714

18-
module.exports = async (filePath, options) => parse(await promisify(fs.readFile)(filePath, 'utf8'), filePath, options);
19-
module.exports.sync = (filePath, options) => parse(fs.readFileSync(filePath, 'utf8'), filePath, options);
15+
export async function loadJsonFile(filePath, options) {
16+
const buffer = await readFile(filePath);
17+
return parse(buffer, options);
18+
}
19+
20+
export function loadJsonFileSync(filePath, options) {
21+
const buffer = readFileSync(filePath);
22+
return parse(buffer, options);
23+
}

index.test-d.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import {expectType} from 'tsd';
1+
import {expectType, expectAssignable} from 'tsd';
22
import {JsonValue} from 'type-fest';
3-
import loadJsonFile = require('.');
4-
import {Reviver, BeforeParse} from '.';
3+
import {loadJsonFile, loadJsonFileSync, Reviver, BeforeParse} from './index.js';
54

6-
expectType<Reviver>(() => 1);
7-
expectType<Reviver>((a: string) => a.length);
8-
expectType<Reviver>((a: string, b: string) => a.length - b.length);
5+
expectAssignable<Reviver>(() => 1);
96

10-
expectType<BeforeParse>(data => data);
7+
expectType<BeforeParse>(data => data); // eslint-disable-line @typescript-eslint/no-unsafe-return
118

129
expectType<Promise<JsonValue>>(loadJsonFile('unicorn.json'));
1310

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

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Read and parse a JSON file",
55
"license": "MIT",
66
"repository": "sindresorhus/load-json-file",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -25,18 +28,11 @@
2528
"parse",
2629
"file",
2730
"fs",
28-
"graceful",
2931
"load"
3032
],
31-
"dependencies": {
32-
"graceful-fs": "^4.1.15",
33-
"parse-json": "^5.0.0",
34-
"strip-bom": "^4.0.0",
35-
"type-fest": "^0.6.0"
36-
},
3733
"devDependencies": {
38-
"ava": "^2.1.0",
39-
"tsd": "^0.7.3",
40-
"xo": "^0.24.0"
34+
"ava": "^3.15.0",
35+
"tsd": "^0.17.0",
36+
"xo": "^0.44.0"
4137
}
4238
}

readme.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,30 @@
22

33
> Read and parse a JSON file
44
5-
[Strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom), uses [`graceful-fs`](https://github.com/isaacs/node-graceful-fs), and throws more [helpful JSON errors](https://github.com/sindresorhus/parse-json).
6-
5+
It also [strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom).
76

87
## Install
98

109
```
1110
$ npm install load-json-file
1211
```
1312

14-
1513
## Usage
1614

1715
```js
18-
const loadJsonFile = require('load-json-file');
16+
import {loadJsonFile} from 'load-json-file';
1917

20-
(async () => {
21-
console.log(await loadJsonFile('foo.json'));
22-
//=> {foo: true}
23-
})();
18+
console.log(await loadJsonFile('foo.json'));
19+
//=> {foo: true}
2420
```
2521

26-
2722
## API
2823

2924
### loadJsonFile(filePath, options?)
3025

3126
Returns a `Promise<unknown>` with the parsed JSON.
3227

33-
### loadJsonFile.sync(filepath, options?)
28+
### loadJsonFileSync(filepath, options?)
3429

3530
Returns the parsed JSON.
3631

@@ -50,14 +45,12 @@ Type: `Function`
5045

5146
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.
5247

53-
5448
## load-json-file for enterprise
5549

5650
Available as part of the Tidelift Subscription.
5751

5852
The maintainers of load-json-file and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-load-json-file?utm_source=npm-load-json-file&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
5953

60-
6154
## Related
6255

6356
- [write-json-file](https://github.com/sindresorhus/write-json-file) - Stringify and write JSON to a file atomically

test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import path from 'path';
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
23
import test from 'ava';
3-
import loadJsonFile from '.';
4+
import {loadJsonFile, loadJsonFileSync} from './index.js';
45

6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
57
const fixture = path.join(__dirname, 'package.json');
68

79
test('async', async t => {
@@ -10,19 +12,19 @@ test('async', async t => {
1012
});
1113

1214
test('sync', t => {
13-
t.is(loadJsonFile.sync(fixture).name, 'load-json-file');
15+
t.is(loadJsonFileSync(fixture).name, 'load-json-file');
1416
});
1517

1618
test('beforeParse option', async t => {
1719
const data = await loadJsonFile(fixture, {
18-
beforeParse: string => string.replace('"name": "load-json-file"', '"name": "foo"')
20+
beforeParse: string => string.replace('"name": "load-json-file"', '"name": "foo"'),
1921
});
2022
t.is(data.name, 'foo');
2123
});
2224

2325
test('reviver option', async t => {
2426
const data = await loadJsonFile(fixture, {
25-
reviver: (key, value) => key === 'name' ? 'foo' : value
27+
reviver: (key, value) => key === 'name' ? 'foo' : value,
2628
});
2729
t.is(data.name, 'foo');
2830
});

0 commit comments

Comments
 (0)