load-json-file
Advanced tools
Comparing version 6.2.0 to 7.0.0
@@ -1,55 +0,48 @@ | ||
import {JsonValue} from 'type-fest'; | ||
// From https://github.com/sindresorhus/type-fest | ||
export type JsonValue = string | number | boolean | null | {[Key in string]?: JsonValue} | JsonValue[]; | ||
declare namespace loadJsonFile { | ||
type Reviver = (this: unknown, key: string, value: any) => unknown; | ||
type BeforeParse = (data: string) => string; | ||
export type Reviver = (this: unknown, key: string, value: unknown) => unknown; | ||
export type BeforeParse = (data: string) => string; | ||
interface Options { | ||
/** | ||
Applies a function to the JSON string before parsing. | ||
*/ | ||
readonly beforeParse?: BeforeParse; | ||
export interface Options { | ||
/** | ||
Applies a function to the JSON string before parsing. | ||
*/ | ||
readonly 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. | ||
*/ | ||
readonly reviver?: Reviver; | ||
} | ||
/** | ||
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. | ||
*/ | ||
readonly reviver?: Reviver; | ||
} | ||
declare const loadJsonFile: { | ||
/** | ||
Read and parse a JSON file. | ||
/** | ||
Read and parse a JSON file. | ||
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors. | ||
It also strips UTF-8 BOM. | ||
@example | ||
``` | ||
import loadJsonFile = require('load-json-file'); | ||
@example | ||
``` | ||
import {loadJsonFile} from 'load-json-file'; | ||
(async () => { | ||
const json = await loadJsonFile('foo.json'); | ||
//=> {foo: true} | ||
})(); | ||
``` | ||
*/ | ||
<T = JsonValue>(filePath: string, options?: loadJsonFile.Options): Promise<T>; | ||
const json = await loadJsonFile('foo.json'); | ||
//=> {foo: true} | ||
``` | ||
*/ | ||
export function loadJsonFile<ReturnValueType = JsonValue>(filePath: string, options?: Options): Promise<ReturnValueType>; | ||
/** | ||
Read and parse a JSON file. | ||
/** | ||
Read and parse a JSON file. | ||
Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors. | ||
It also strips UTF-8 BOM. | ||
@example | ||
``` | ||
import loadJsonFile = require('load-json-file'); | ||
@example | ||
``` | ||
import {loadJsonFileSync} from 'load-json-file'; | ||
const json = loadJsonFile.sync('foo.json'); | ||
//=> {foo: true} | ||
``` | ||
*/ | ||
sync<T = JsonValue>(filePath: string, options?: loadJsonFile.Options): T; | ||
}; | ||
export = loadJsonFile; | ||
const json = loadJsonFileSync('foo.json'); | ||
//=> {foo: true} | ||
``` | ||
*/ | ||
export function loadJsonFileSync<ReturnValueType = JsonValue>(filePath: string, options?: Options): ReturnValueType; |
30
index.js
@@ -1,19 +0,23 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const {promisify} = require('util'); | ||
const fs = require('graceful-fs'); | ||
const stripBom = require('strip-bom'); | ||
const parseJson = require('parse-json'); | ||
import {readFileSync} from 'node:fs'; | ||
import {readFile} from 'node:fs/promises'; | ||
const parse = (data, filePath, options = {}) => { | ||
data = stripBom(data); | ||
const parse = (buffer, {beforeParse, reviver} = {}) => { | ||
// Unlike `buffer.toString()` and `fs.readFile(path, 'utf8')`, `TextDecoder`` will remove BOM. | ||
let data = new TextDecoder().decode(buffer); | ||
if (typeof options.beforeParse === 'function') { | ||
data = options.beforeParse(data); | ||
if (typeof beforeParse === 'function') { | ||
data = beforeParse(data); | ||
} | ||
return parseJson(data, options.reviver, path.relative(process.cwd(), filePath)); | ||
return JSON.parse(data, reviver); | ||
}; | ||
module.exports = async (filePath, options) => parse(await promisify(fs.readFile)(filePath, 'utf8'), filePath, options); | ||
module.exports.sync = (filePath, options) => parse(fs.readFileSync(filePath, 'utf8'), filePath, options); | ||
export async function loadJsonFile(filePath, options) { | ||
const buffer = await readFile(filePath); | ||
return parse(buffer, options); | ||
} | ||
export function loadJsonFileSync(filePath, options) { | ||
const buffer = readFileSync(filePath); | ||
return parse(buffer, options); | ||
} |
{ | ||
"name": "load-json-file", | ||
"version": "6.2.0", | ||
"version": "7.0.0", | ||
"description": "Read and parse a JSON file", | ||
"license": "MIT", | ||
"repository": "sindresorhus/load-json-file", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -28,16 +31,9 @@ "scripts": { | ||
"fs", | ||
"graceful", | ||
"load" | ||
], | ||
"dependencies": { | ||
"graceful-fs": "^4.1.15", | ||
"parse-json": "^5.0.0", | ||
"strip-bom": "^4.0.0", | ||
"type-fest": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.1.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,8 +0,7 @@ | ||
# load-json-file [![Build Status](https://travis-ci.org/sindresorhus/load-json-file.svg?branch=master)](https://travis-ci.org/sindresorhus/load-json-file) | ||
# load-json-file | ||
> Read and parse a JSON file | ||
[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). | ||
It also [strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom). | ||
## Install | ||
@@ -14,15 +13,11 @@ | ||
## Usage | ||
```js | ||
const loadJsonFile = require('load-json-file'); | ||
import {loadJsonFile} from 'load-json-file'; | ||
(async () => { | ||
console.log(await loadJsonFile('foo.json')); | ||
//=> {foo: true} | ||
})(); | ||
console.log(await loadJsonFile('foo.json')); | ||
//=> {foo: true} | ||
``` | ||
## API | ||
@@ -34,3 +29,3 @@ | ||
### loadJsonFile.sync(filepath, options?) | ||
### loadJsonFileSync(filepath, options?) | ||
@@ -55,18 +50,10 @@ Returns the parsed JSON. | ||
## load-json-file for enterprise | ||
Available as part of the Tidelift Subscription. | ||
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) | ||
## Related | ||
- [write-json-file](https://github.com/sindresorhus/write-json-file) - Stringify and write JSON to a file atomically | ||
--- | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-load-json-file?utm_source=npm-load-json-file&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
Yes
5368
55
57
- Removedgraceful-fs@^4.1.15
- Removedparse-json@^5.0.0
- Removedstrip-bom@^4.0.0
- Removedtype-fest@^0.6.0
- Removed@babel/code-frame@7.26.2(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjson-parse-even-better-errors@2.3.1(transitive)
- Removedlines-and-columns@1.2.4(transitive)
- Removedparse-json@5.2.0(transitive)
- Removedpicocolors@1.1.1(transitive)
- Removedstrip-bom@4.0.0(transitive)
- Removedtype-fest@0.6.0(transitive)