pax_global_header00006660000000000000000000000064141175506340014520gustar00rootroot0000000000000052 comment=eeac7ad786731a6c7e4a50b414ebb43c847bf6f6 load-json-file-7.0.1/000077500000000000000000000000001411755063400143305ustar00rootroot00000000000000load-json-file-7.0.1/.editorconfig000066400000000000000000000002571411755063400170110ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 load-json-file-7.0.1/.gitattributes000066400000000000000000000000231411755063400172160ustar00rootroot00000000000000* text=auto eol=lf load-json-file-7.0.1/.github/000077500000000000000000000000001411755063400156705ustar00rootroot00000000000000load-json-file-7.0.1/.github/funding.yml000066400000000000000000000001701411755063400200430ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/load-json-file custom: https://sindresorhus.com/donate load-json-file-7.0.1/.github/security.md000066400000000000000000000002631411755063400200620ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. load-json-file-7.0.1/.github/workflows/000077500000000000000000000000001411755063400177255ustar00rootroot00000000000000load-json-file-7.0.1/.github/workflows/main.yml000066400000000000000000000006261411755063400214000ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test load-json-file-7.0.1/.gitignore000066400000000000000000000000271411755063400163170ustar00rootroot00000000000000node_modules yarn.lock load-json-file-7.0.1/.npmrc000066400000000000000000000000231411755063400154430ustar00rootroot00000000000000package-lock=false load-json-file-7.0.1/index.d.ts000066400000000000000000000024631411755063400162360ustar00rootroot00000000000000// From https://github.com/sindresorhus/type-fest export type JsonValue = string | number | boolean | null | {[Key in string]?: JsonValue} | JsonValue[]; export type Reviver = (this: unknown, key: string, value: unknown) => unknown; export type BeforeParse = (data: string) => string; 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; } /** Read and parse a JSON file. It also strips UTF-8 BOM. @example ``` import {loadJsonFile} from 'load-json-file'; const json = await loadJsonFile('foo.json'); //=> {foo: true} ``` */ export function loadJsonFile(filePath: string, options?: Options): Promise; /** Read and parse a JSON file. It also strips UTF-8 BOM. @example ``` import {loadJsonFileSync} from 'load-json-file'; const json = loadJsonFileSync('foo.json'); //=> {foo: true} ``` */ export function loadJsonFileSync(filePath: string, options?: Options): ReturnValueType; load-json-file-7.0.1/index.js000066400000000000000000000012151411755063400157740ustar00rootroot00000000000000import {readFileSync, promises as fs} from 'node:fs'; const {readFile} = fs; 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 beforeParse === 'function') { data = beforeParse(data); } return JSON.parse(data, reviver); }; 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); } load-json-file-7.0.1/index.test-d.ts000066400000000000000000000006511411755063400172100ustar00rootroot00000000000000import {expectType, expectAssignable} from 'tsd'; import {JsonValue} from 'type-fest'; import {loadJsonFile, loadJsonFileSync, Reviver, BeforeParse} from './index.js'; expectAssignable(() => 1); expectType(data => data); // eslint-disable-line @typescript-eslint/no-unsafe-return expectType>(loadJsonFile('unicorn.json')); expectType(loadJsonFileSync('unicorn.json')); load-json-file-7.0.1/license000066400000000000000000000021351411755063400156760ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. load-json-file-7.0.1/package.json000066400000000000000000000012701411755063400166160ustar00rootroot00000000000000{ "name": "load-json-file", "version": "7.0.1", "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": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "read", "json", "parse", "file", "fs", "load" ], "devDependencies": { "ava": "^3.15.0", "tsd": "^0.17.0", "xo": "^0.44.0" } } load-json-file-7.0.1/readme.md000066400000000000000000000030401411755063400161040ustar00rootroot00000000000000# load-json-file > Read and parse a JSON file It also [strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom). ## Install ``` $ npm install load-json-file ``` ## Usage ```js import {loadJsonFile} from 'load-json-file'; console.log(await loadJsonFile('foo.json')); //=> {foo: true} ``` ## API ### loadJsonFile(filePath, options?) Returns a `Promise` with the parsed JSON. ### loadJsonFileSync(filepath, options?) Returns the parsed JSON. #### options Type: `object` ##### beforeParse Type: `Function` Applies a function to the JSON string before parsing. ##### reviver Type: `Function` 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. ## 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 load-json-file-7.0.1/test.js000066400000000000000000000015141411755063400156460ustar00rootroot00000000000000import path from 'node:path'; import {fileURLToPath} from 'node:url'; import test from 'ava'; import {loadJsonFile, loadJsonFileSync} from './index.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixture = path.join(__dirname, 'package.json'); test('async', async t => { const data = await loadJsonFile(fixture); t.is(data.name, 'load-json-file'); }); test('sync', t => { t.is(loadJsonFileSync(fixture).name, 'load-json-file'); }); test('beforeParse option', async t => { const data = await loadJsonFile(fixture, { beforeParse: string => string.replace('"name": "load-json-file"', '"name": "foo"'), }); t.is(data.name, 'foo'); }); test('reviver option', async t => { const data = await loadJsonFile(fixture, { reviver: (key, value) => key === 'name' ? 'foo' : value, }); t.is(data.name, 'foo'); });