[Pkg-javascript-commits] [node-jsonfile] 01/05: New upstream version 3.0.0

Julien Puydt julien.puydt at laposte.net
Mon Jul 3 14:29:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-jsonfile.

commit 21d83bc5155d61dc9dc21d65fb55ab7ee7e99a97
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Wed Jun 28 07:31:15 2017 +0200

    New upstream version 3.0.0
---
 .npmignore                  |  3 ++-
 CHANGELOG.md                |  5 +++++
 README.md                   | 30 +++++++++++++++++++++++++++---
 index.js                    |  5 ++---
 package.json                |  2 +-
 test/read-file-sync.test.js | 19 +++++++++++++++++--
 6 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/.npmignore b/.npmignore
index cefaa67..874ae58 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,2 +1,3 @@
 test/
-.travis.yml
\ No newline at end of file
+.travis.yml
+appveyor.yml
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 66fcbb4..70cf7c5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+3.0.0 / 2017-04-25
+------------------
+
+- Changed behavior of `throws` option for `readFileSync`; now does not throw filesystem errors when `throws` is `false`
+
 2.4.0 / 2016-09-15
 ------------------
 ### Changed
diff --git a/README.md b/README.md
index 54bca05..1f214df 100644
--- a/README.md
+++ b/README.md
@@ -44,9 +44,8 @@ jsonfile.readFile(file, function(err, obj) {
 
 ### readFileSync(filename, [options])
 
-`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.
+`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 an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
 
 ```js
 var jsonfile = require('jsonfile')
@@ -85,6 +84,20 @@ jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
 })
 ```
 
+**appending to an existing JSON file:**
+
+You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
+
+```js
+var jsonfile = require('jsonfile')
+
+var file = '/tmp/mayAlreadyExistedData.json'
+var obj = {name: 'JP'}
+
+jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
+  console.error(err)
+})
+```
 
 ### writeFileSync(filename, obj, [options])
 
@@ -110,7 +123,18 @@ var obj = {name: 'JP'}
 jsonfile.writeFileSync(file, obj, {spaces: 2})
 ```
 
+**appending to an existing JSON file:**
+
+You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
+
+```js
+var jsonfile = require('jsonfile')
+
+var file = '/tmp/mayAlreadyExistedData.json'
+var obj = {name: 'JP'}
 
+jsonfile.writeFileSync(file, obj, {flag: 'a'})
+```
 
 ### spaces
 
diff --git a/index.js b/index.js
index 7111e15..5ac06cf 100644
--- a/index.js
+++ b/index.js
@@ -63,10 +63,9 @@ function readFileSync (file, options) {
     shouldThrow = options.throws
   }
 
-  var content = fs.readFileSync(file, options)
-  content = stripBom(content)
-
   try {
+    var content = fs.readFileSync(file, options)
+    content = stripBom(content)
     return JSON.parse(content, options.reviver)
   } catch (err) {
     if (shouldThrow) {
diff --git a/package.json b/package.json
index ea7ac1e..7dfb90b 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "jsonfile",
-  "version": "2.4.0",
+  "version": "3.0.0",
   "description": "Easily read/write JSON files.",
   "repository": {
     "type": "git",
diff --git a/test/read-file-sync.test.js b/test/read-file-sync.test.js
index d106fb5..2208de9 100644
--- a/test/read-file-sync.test.js
+++ b/test/read-file-sync.test.js
@@ -98,14 +98,29 @@ describe('+ readFileSync()', function () {
   })
 
   describe('> when invalid JSON and throws set to true', function () {
-    it('should return null', function () {
+    it('should throw an exception', function () {
       var file = path.join(TEST_DIR, 'somefile4-invalid.json')
       var data = '{not valid JSON'
       fs.writeFileSync(file, data)
 
       assert.throws(function () {
-        jf.readFileSync(file)
+        jf.readFileSync(file, {throws: true})
       })
+    })
+  })
+
+  describe('> when json file is missing and throws set to false', function () {
+    it('should return null', function () {
+      var file = path.join(TEST_DIR, 'somefile4-invalid.json')
+
+      var obj = jf.readFileSync(file, {throws: false})
+      assert.strictEqual(obj, null)
+    })
+  })
+
+  describe('> when json file is missing and throws set to true', function () {
+    it('should throw an exception', function () {
+      var file = path.join(TEST_DIR, 'somefile4-invalid.json')
 
       assert.throws(function () {
         jf.readFileSync(file, {throws: true})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsonfile.git



More information about the Pkg-javascript-commits mailing list