Skip to content

Add beforeParse and reviver options #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2018
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
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const stripBom = require('strip-bom');
const parseJson = require('parse-json');
const pify = require('pify');

const parse = (data, fp) => parseJson(stripBom(data), path.relative('.', fp));
const parse = (data, fp, options) => {
options = options || {};
data = stripBom(data);
if (typeof options.beforeParse === 'function') {
data = options.beforeParse(data);
}
return parseJson(data, options.reviver, path.relative('.', fp));
};

module.exports = fp => pify(fs.readFile)(fp, 'utf8').then(data => parse(data, fp));
module.exports.sync = fp => parse(fs.readFileSync(fp, 'utf8'), fp);
module.exports = (fp, options) => pify(fs.readFile)(fp, 'utf8').then(data => parse(data, fp, options));
module.exports.sync = (fp, options) => parse(fs.readFileSync(fp, 'utf8'), fp, options);
20 changes: 18 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,30 @@ loadJsonFile('foo.json').then(json => {

## API

### loadJsonFile(filepath)
### loadJsonFile(filepath, [options])

Returns a promise for the parsed JSON.

### loadJsonFile.sync(filepath)
### loadJsonFile.sync(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 [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more.


## Related

Expand Down
12 changes: 11 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import test from 'ava';
import m from './';
import m from '.';

const fixture = path.join(__dirname, 'package.json');

Expand All @@ -12,3 +12,13 @@ test('async', async t => {
test('sync', t => {
t.is(m.sync(fixture).name, 'load-json-file');
});

test('beforeParse', async t => {
const data = await m(fixture, {beforeParse: s => s.replace('"name": "load-json-file"', '"name": "foo"')});
t.is(data.name, 'foo');
});

test('reviver', async t => {
const data = await m(fixture, {reviver: (k, v) => k === 'name' ? 'foo' : v});
t.is(data.name, 'foo');
});