jsonfile
Advanced tools
Comparing version 2.2.3 to 2.3.0
@@ -0,1 +1,6 @@ | ||
2.3.0 / 2016-04-16 | ||
------------------ | ||
- add `throws` to `readFile()`. See [#39][#39] | ||
- add support for any arbitrary `fs` module. Useful with [mock-fs](https://www.npmjs.com/package/mock-fs) | ||
2.2.3 / 2015-10-14 | ||
@@ -45,3 +50,3 @@ ------------------ | ||
* bugfix: passed `options` to `fs.readFile` and `fs.readFileSync`. This technically changes behavior, but | ||
changes it according to docs. #12 | ||
changes it according to docs. [#12][#12] | ||
@@ -68,1 +73,46 @@ 1.1.1 / 2013-11-11 | ||
* Initial release. | ||
[#44]: https://github.com/jprichardson/node-jsonfile/issues/44 "Extra characters in written file" | ||
[#43]: https://github.com/jprichardson/node-jsonfile/issues/43 "Prettyfy json when written to file" | ||
[#42]: https://github.com/jprichardson/node-jsonfile/pull/42 "Moved fs.readFileSync within the try/catch" | ||
[#41]: https://github.com/jprichardson/node-jsonfile/issues/41 "Linux: Hidden file not working" | ||
[#40]: https://github.com/jprichardson/node-jsonfile/issues/40 "autocreate folder doesnt work from Path-value" | ||
[#39]: https://github.com/jprichardson/node-jsonfile/pull/39 "Add `throws` option for readFile (async)" | ||
[#38]: https://github.com/jprichardson/node-jsonfile/pull/38 "Update README.md writeFile[Sync] signature" | ||
[#37]: https://github.com/jprichardson/node-jsonfile/pull/37 "support append file" | ||
[#36]: https://github.com/jprichardson/node-jsonfile/pull/36 "Add typescript definition file." | ||
[#35]: https://github.com/jprichardson/node-jsonfile/pull/35 "Add typescript definition file." | ||
[#34]: https://github.com/jprichardson/node-jsonfile/pull/34 "readFile JSON parse error includes filename" | ||
[#33]: https://github.com/jprichardson/node-jsonfile/pull/33 "fix throw->throws typo in readFileSync()" | ||
[#32]: https://github.com/jprichardson/node-jsonfile/issues/32 "readFile & readFileSync can possible have strip-comments as an option?" | ||
[#31]: https://github.com/jprichardson/node-jsonfile/pull/31 "[Modify] Support string include is unicode escape string" | ||
[#30]: https://github.com/jprichardson/node-jsonfile/issues/30 "How to use Jsonfile package in Meteor.js App?" | ||
[#29]: https://github.com/jprichardson/node-jsonfile/issues/29 "writefile callback if no error?" | ||
[#28]: https://github.com/jprichardson/node-jsonfile/issues/28 "writeFile options argument broken " | ||
[#27]: https://github.com/jprichardson/node-jsonfile/pull/27 "Use svg instead of png to get better image quality" | ||
[#26]: https://github.com/jprichardson/node-jsonfile/issues/26 "Breaking change to fs-extra" | ||
[#25]: https://github.com/jprichardson/node-jsonfile/issues/25 "support string encoding param for read methods" | ||
[#24]: https://github.com/jprichardson/node-jsonfile/issues/24 "readFile: Passing in null options with a callback throws an error" | ||
[#23]: https://github.com/jprichardson/node-jsonfile/pull/23 "Add appendFile and appendFileSync" | ||
[#22]: https://github.com/jprichardson/node-jsonfile/issues/22 "Default value for spaces in readme.md is outdated" | ||
[#21]: https://github.com/jprichardson/node-jsonfile/pull/21 "Update license attribute" | ||
[#20]: https://github.com/jprichardson/node-jsonfile/issues/20 "Add simple caching functionallity" | ||
[#19]: https://github.com/jprichardson/node-jsonfile/pull/19 "Add appendFileSync method" | ||
[#18]: https://github.com/jprichardson/node-jsonfile/issues/18 "Add updateFile and updateFileSync methods" | ||
[#17]: https://github.com/jprichardson/node-jsonfile/issues/17 "seem read & write sync has sequentially problem" | ||
[#16]: https://github.com/jprichardson/node-jsonfile/pull/16 "export spaces defaulted to null" | ||
[#15]: https://github.com/jprichardson/node-jsonfile/issues/15 "`jsonfile.spaces` should default to `null`" | ||
[#14]: https://github.com/jprichardson/node-jsonfile/pull/14 "Add EOL at EOF" | ||
[#13]: https://github.com/jprichardson/node-jsonfile/issues/13 "Add a final newline" | ||
[#12]: https://github.com/jprichardson/node-jsonfile/issues/12 "readFile doesn't accept options" | ||
[#11]: https://github.com/jprichardson/node-jsonfile/pull/11 "Added try,catch to readFileSync" | ||
[#10]: https://github.com/jprichardson/node-jsonfile/issues/10 "No output or error from writeFile" | ||
[#9]: https://github.com/jprichardson/node-jsonfile/pull/9 "Change 'js' to 'jf' in example." | ||
[#8]: https://github.com/jprichardson/node-jsonfile/pull/8 "Updated forgotten module.exports to me." | ||
[#7]: https://github.com/jprichardson/node-jsonfile/pull/7 "Add file name in error message" | ||
[#6]: https://github.com/jprichardson/node-jsonfile/pull/6 "Use graceful-fs when possible" | ||
[#5]: https://github.com/jprichardson/node-jsonfile/pull/5 "Jsonfile doesn't behave nicely when used inside a test suite." | ||
[#4]: https://github.com/jprichardson/node-jsonfile/pull/4 "Added options parameter to writeFile and writeFileSync" | ||
[#3]: https://github.com/jprichardson/node-jsonfile/issues/3 "test2" | ||
[#2]: https://github.com/jprichardson/node-jsonfile/issues/2 "homepage field must be a string url. Deleted." | ||
[#1]: https://github.com/jprichardson/node-jsonfile/pull/1 "adding an `.npmignore` file" |
39
index.js
@@ -1,2 +0,2 @@ | ||
var fs = require('fs') | ||
var _fs = require('fs') | ||
@@ -9,2 +9,17 @@ function readFile (file, options, callback) { | ||
if (typeof options === 'string') { | ||
options = {encoding: options} | ||
} | ||
options = options || {} | ||
var fs = options.fs || _fs | ||
var shouldThrow = true | ||
// DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead | ||
if ('passParsingErrors' in options) { | ||
shouldThrow = options.passParsingErrors | ||
} else if ('throws' in options) { | ||
shouldThrow = options.throws | ||
} | ||
fs.readFile(file, options, function (err, data) { | ||
@@ -17,4 +32,8 @@ if (err) return callback(err) | ||
} catch (err2) { | ||
err2.message = file + ': ' + err2.message | ||
return callback(err2) | ||
if (shouldThrow) { | ||
err2.message = file + ': ' + err2.message | ||
return callback(err2) | ||
} else { | ||
return callback(null, null) | ||
} | ||
} | ||
@@ -32,3 +51,12 @@ | ||
var shouldThrow = 'throws' in options ? options.throws : true | ||
var fs = options.fs || _fs | ||
var shouldThrow = true | ||
// DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead | ||
if ('passParsingErrors' in options) { | ||
shouldThrow = options.passParsingErrors | ||
} else if ('throws' in options) { | ||
shouldThrow = options.throws | ||
} | ||
var content = fs.readFileSync(file, options) | ||
@@ -53,2 +81,4 @@ | ||
} | ||
options = options || {} | ||
var fs = options.fs || _fs | ||
@@ -72,2 +102,3 @@ var spaces = typeof options === 'object' && options !== null | ||
options = options || {} | ||
var fs = options.fs || _fs | ||
@@ -74,0 +105,0 @@ var spaces = typeof options === 'object' && options !== null |
{ | ||
"name": "jsonfile", | ||
"version": "2.2.3", | ||
"version": "2.3.0", | ||
"description": "Easily read/write JSON files.", | ||
@@ -22,9 +22,12 @@ "repository": { | ||
"mocha": "2.x", | ||
"mock-fs": "^3.8.0", | ||
"rimraf": "^2.4.0", | ||
"standard": "4.x" | ||
"standard": "^6.0.8" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && mocha" | ||
"lint": "standard", | ||
"test": "npm run lint && npm run unit", | ||
"unit": "mocha" | ||
} | ||
} |
@@ -6,4 +6,7 @@ Node.js - jsonfile | ||
[![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile) | ||
[![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.svg)](http://travis-ci.org/jprichardson/node-jsonfile) | ||
[![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-jsonfile/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-jsonfile/branch/master) | ||
<a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a> | ||
@@ -20,3 +23,3 @@ Why? | ||
npm install jsonfile --save | ||
npm install --save jsonfile | ||
@@ -30,3 +33,5 @@ | ||
`options`: Pass in any `fs.readFile` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
`options` (`object`, default `undefined`): Pass in any `fs.readFile` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
- `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback. | ||
If `false`, returns `null` for the object. | ||
@@ -36,4 +41,2 @@ | ||
var jsonfile = require('jsonfile') | ||
var util = require('util') | ||
var file = '/tmp/data.json' | ||
@@ -48,9 +51,8 @@ jsonfile.readFile(file, function(err, obj) { | ||
`options`: Pass in any `fs.readFileSync` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). Also `throws` set to `false` if you don't ever want this method | ||
to throw on invalid JSON. Will return `null` instead. Defaults to `true`. | ||
`options` (`object`, default `undefined`): Pass in any `fs.readFileSync` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
- `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, throw the error. | ||
If `false`, returns `null` for the object. | ||
```js | ||
var jsonfile = require('jsonfile') | ||
var util = require('util') | ||
var file = '/tmp/data.json' | ||
@@ -62,3 +64,3 @@ | ||
### writeFile(filename, [options], callback) | ||
### writeFile(filename, obj, [options], callback) | ||
@@ -93,3 +95,3 @@ `options`: Pass in any `fs.writeFile` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`. | ||
### writeFileSync(filename, [options]) | ||
### writeFileSync(filename, obj, [options]) | ||
@@ -129,3 +131,3 @@ `options`: Pass in any `fs.writeFileSync` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`. | ||
jsonfile.spaces = 4; | ||
jsonfile.spaces = 4 | ||
@@ -168,7 +170,2 @@ var file = '/tmp/data.json' | ||
Copyright 2012-2015, JP Richardson <jprichardson@gmail.com> | ||
Copyright 2012-2016, JP Richardson <jprichardson@gmail.com> |
16222
7
97
4
163