[Pkg-javascript-commits] [node-jsonfile] 01/08: New upstream version 4.0.0

Julien Puydt julien.puydt at laposte.net
Sun Oct 1 19:35:30 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 469bd3cc75129fea3e32d9caaec6c38492374f74
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Fri Sep 15 06:57:30 2017 +0200

    New upstream version 4.0.0
---
 .editorconfig                |  7 +++++
 .travis.yml                  |  7 ++---
 CHANGELOG.md                 | 11 +++++++-
 README.md                    | 62 ++++++++++++++++----------------------------
 appveyor.yml                 |  4 +--
 index.js                     | 42 +++++++++++++++---------------
 package.json                 |  7 +++--
 test/read-file-sync.test.js  | 31 ----------------------
 test/read-file.test.js       | 56 ---------------------------------------
 test/test.js                 | 29 ---------------------
 test/write-file-sync.test.js | 23 ++++++----------
 test/write-file.test.js      | 29 ++++++++-------------
 12 files changed, 87 insertions(+), 221 deletions(-)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..6ad2458
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,7 @@
+root = true
+
+[*]
+trim_trailing_whitespace = true
+insert_final_newline = true
+indent_style = space
+indent_size = 2
diff --git a/.travis.yml b/.travis.yml
index da0d28c..2fb1ad0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,15 +1,12 @@
 sudo: false
 language: node_js
 node_js:
-  - "0.10"
-  - "0.12"
-  - "io.js"
   - "4"
-  - "5"
   - "6"
+  - "8"
 matrix:
   include:
-    - node_js: "4"
+    - node_js: "8"
       env: TEST_SUITE=lint
 env:
   - TEST_SUITE=unit
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ff0c4e3..7718857 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+4.0.0 / 2017-07-12
+------------------
+
+- **BREAKING:** Remove global `spaces` option.
+- **BREAKING:** Drop support for Node 0.10, 0.12, and io.js.
+- Remove undocumented `passParsingErrors` option.
+- Added `EOL` override option to `writeFile` when using `spaces`. [#89]
+
 3.0.1 / 2017-07-05
 ------------------
 
@@ -89,12 +97,13 @@ changes it according to docs. [#12][#12]
 ------------------
 * Initial release.
 
+[#89]: https://github.com/jprichardson/node-jsonfile/pull/89
 [#45]: https://github.com/jprichardson/node-jsonfile/issues/45    "Reading of UTF8-encoded (w/ BOM) files fails"
 [#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"
+[#40]: https://github.com/jprichardson/node-jsonfile/issues/40    "autocreate folder doesn't 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"
diff --git a/README.md b/README.md
index 1f214df..721685c 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ console.dir(jsonfile.readFileSync(file))
 
 ### writeFile(filename, obj, [options], callback)
 
-`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`.
+`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` and override `EOL` string.
 
 
 ```js
@@ -84,6 +84,19 @@ jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
 })
 ```
 
+**overriding EOL:**
+
+```js
+var jsonfile = require('jsonfile')
+
+var file = '/tmp/data.json'
+var obj = {name: 'JP'}
+
+jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
+  console.error(err)
+})
+```
+
 **appending to an existing JSON file:**
 
 You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
@@ -101,7 +114,7 @@ jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
 
 ### writeFileSync(filename, obj, [options])
 
-`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`.
+`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` and override `EOL` string.
 
 ```js
 var jsonfile = require('jsonfile')
@@ -123,61 +136,30 @@ 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.
+**overriding EOL:**
 
 ```js
 var jsonfile = require('jsonfile')
 
-var file = '/tmp/mayAlreadyExistedData.json'
+var file = '/tmp/data.json'
 var obj = {name: 'JP'}
 
-jsonfile.writeFileSync(file, obj, {flag: 'a'})
+jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
 ```
 
-### spaces
-
-Global configuration to set spaces to indent JSON files.
+**appending to an existing JSON file:**
 
-**default:** `null`
+You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
 
 ```js
 var jsonfile = require('jsonfile')
 
-jsonfile.spaces = 4
-
-var file = '/tmp/data.json'
+var file = '/tmp/mayAlreadyExistedData.json'
 var obj = {name: 'JP'}
 
-// json file has four space indenting now
-jsonfile.writeFile(file, obj, function (err) {
-  console.error(err)
-})
-```
-
-Note, it's bound to `this.spaces`. So, if you do this:
-
-```js
-var myObj = {}
-myObj.writeJsonSync = jsonfile.writeFileSync
-// => this.spaces = null
-```
-
-Could do the following:
-
-```js
-var jsonfile = require('jsonfile')
-jsonfile.spaces = 4
-jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
-
-var myCrazyObj = {spaces: 32}
-myCrazyObj.writeJsonSync = jsonfile.writeFileSync
-myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
-myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
+jsonfile.writeFileSync(file, obj, {flag: 'a'})
 ```
 
-
 License
 -------
 
diff --git a/appveyor.yml b/appveyor.yml
index 872af18..9174d7a 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -2,11 +2,9 @@
 environment:
   matrix:
     # node.js
-    - nodejs_version: "0.10"
-    - nodejs_version: "0.12"
     - nodejs_version: "4"
-    - nodejs_version: "5"
     - nodejs_version: "6"
+    - nodejs_version: "8"
 
 # Install scripts. (runs after repo cloning)
 install:
diff --git a/index.js b/index.js
index a28684f..d1e5827 100644
--- a/index.js
+++ b/index.js
@@ -19,10 +19,7 @@ function readFile (file, options, callback) {
   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) {
+  if ('throws' in options) {
     shouldThrow = options.throws
   }
 
@@ -56,10 +53,7 @@ function readFileSync (file, 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) {
+  if ('throws' in options) {
     shouldThrow = options.throws
   }
 
@@ -77,6 +71,23 @@ function readFileSync (file, options) {
   }
 }
 
+function stringify (obj, options) {
+  var spaces
+  var EOL = '\n'
+  if (typeof options === 'object' && options !== null) {
+    if (options.spaces) {
+      spaces = options.spaces
+    }
+    if (options.EOL) {
+      EOL = options.EOL
+    }
+  }
+
+  var str = JSON.stringify(obj, options ? options.replacer : null, spaces)
+
+  return str.replace(/\n/g, EOL) + EOL
+}
+
 function writeFile (file, obj, options, callback) {
   if (callback == null) {
     callback = options
@@ -85,14 +96,9 @@ function writeFile (file, obj, options, callback) {
   options = options || {}
   var fs = options.fs || _fs
 
-  var spaces = typeof options === 'object' && options !== null
-    ? 'spaces' in options
-    ? options.spaces : this.spaces
-    : this.spaces
-
   var str = ''
   try {
-    str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
+    str = stringify(obj, options)
   } catch (err) {
     // Need to return whether a callback was passed or not
     if (callback) callback(err, null)
@@ -106,12 +112,7 @@ function writeFileSync (file, obj, options) {
   options = options || {}
   var fs = options.fs || _fs
 
-  var spaces = typeof options === 'object' && options !== null
-    ? 'spaces' in options
-    ? options.spaces : this.spaces
-    : this.spaces
-
-  var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
+  var str = stringify(obj, options)
   // not sure if fs.writeFileSync returns anything, but just in case
   return fs.writeFileSync(file, str, options)
 }
@@ -124,7 +125,6 @@ function stripBom (content) {
 }
 
 var jsonfile = {
-  spaces: null,
   readFile: readFile,
   readFileSync: readFileSync,
   writeFile: writeFile,
diff --git a/package.json b/package.json
index 52719ed..29e783f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "jsonfile",
-  "version": "3.0.1",
+  "version": "4.0.0",
   "description": "Easily read/write JSON files.",
   "repository": {
     "type": "git",
@@ -23,9 +23,12 @@
   "devDependencies": {
     "mocha": "2.x",
     "rimraf": "^2.4.0",
-    "standard": "^6.0.8"
+    "standard": "^10.0.3"
   },
   "main": "index.js",
+  "files": [
+    "index.js"
+  ],
   "scripts": {
     "lint": "standard",
     "test": "npm run lint && npm run unit",
diff --git a/test/read-file-sync.test.js b/test/read-file-sync.test.js
index 906e299..5c53c82 100644
--- a/test/read-file-sync.test.js
+++ b/test/read-file-sync.test.js
@@ -50,21 +50,6 @@ describe('+ readFileSync()', function () {
     })
   })
 
-  describe('> when invalid JSON and passParsingErrors set to false', function () {
-    it('should return null', 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)
-      })
-
-      var obj = jf.readFileSync(file, {passParsingErrors: false})
-      assert.strictEqual(obj, null)
-    })
-  })
-
   describe('> when invalid JSON and throws set to false', function () {
     it('should return null', function () {
       var file = path.join(TEST_DIR, 'somefile4-invalid.json')
@@ -80,22 +65,6 @@ describe('+ readFileSync()', function () {
     })
   })
 
-  describe('> when invalid JSON and passParsingErrors set to true', function () {
-    it('should return null', 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)
-      })
-
-      assert.throws(function () {
-        jf.readFileSync(file, {throws: true})
-      })
-    })
-  })
-
   describe('> when invalid JSON and throws set to true', function () {
     it('should throw an exception', function () {
       var file = path.join(TEST_DIR, 'somefile4-invalid.json')
diff --git a/test/read-file.test.js b/test/read-file.test.js
index 9777e62..ac98c2a 100644
--- a/test/read-file.test.js
+++ b/test/read-file.test.js
@@ -75,62 +75,6 @@ describe('+ readFile()', function () {
     })
   })
 
-  describe('> when invalid JSON and throws set to false', function () {
-    it('should return null and no error', function (done) {
-      var fn = 'somefile4-invalid.json'
-      var file = path.join(TEST_DIR, fn)
-      var data = '{not valid JSON'
-      var bothDone = false
-      fs.writeFileSync(file, data)
-
-      jf.readFile(file, function (err, obj2) {
-        assert(err instanceof Error)
-        assert(err.message.match(fn))
-        if (bothDone) {
-          done()
-        }
-        bothDone = true
-      })
-
-      jf.readFile(file, {throws: false}, function (err, obj2) {
-        assert.ifError(err)
-        assert.strictEqual(obj2, null)
-        if (bothDone) {
-          done()
-        }
-        bothDone = true
-      })
-    })
-  })
-
-  describe('> when invalid JSON and throws set to true', function () {
-    it('should return an error', function (done) {
-      var fn = 'somefile4-invalid.json'
-      var file = path.join(TEST_DIR, fn)
-      var data = '{not valid JSON'
-      var bothDone = false
-      fs.writeFileSync(file, data)
-
-      jf.readFile(file, function (err, obj2) {
-        assert(err instanceof Error)
-        assert(err.message.match(fn))
-        if (bothDone) {
-          done()
-        }
-        bothDone = true
-      })
-
-      jf.readFile(file, {throws: true}, function (err, obj2) {
-        assert(err instanceof Error)
-        assert(err.message.match(fn))
-        if (bothDone) {
-          done()
-        }
-        bothDone = true
-      })
-    })
-  })
-
   describe('> when invalid JSON and throws set to true', function () {
     it('should return an error', function (done) {
       var fn = 'somefile4-invalid.json'
diff --git a/test/test.js b/test/test.js
deleted file mode 100644
index 66e8a08..0000000
--- a/test/test.js
+++ /dev/null
@@ -1,29 +0,0 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var rimraf = require('rimraf')
-var jf = require('../')
-
-/* global describe it beforeEach afterEach */
-
-describe('jsonfile', function () {
-  var TEST_DIR
-
-  beforeEach(function (done) {
-    TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests')
-    rimraf.sync(TEST_DIR)
-    fs.mkdir(TEST_DIR, done)
-  })
-
-  afterEach(function (done) {
-    rimraf.sync(TEST_DIR)
-    done()
-  })
-
-  describe('spaces', function () {
-    it('should default to null', function () {
-      assert.strictEqual(jf.spaces, null)
-    })
-  })
-})
diff --git a/test/write-file-sync.test.js b/test/write-file-sync.test.js
index 0905061..e70acc5 100644
--- a/test/write-file-sync.test.js
+++ b/test/write-file-sync.test.js
@@ -34,21 +34,6 @@ describe('+ writeFileSync()', function () {
     assert.equal(data, '{"name":"JP"}\n')
   })
 
-  describe('> when global spaces is set', function () {
-    it('should write JSON with spacing', function () {
-      var file = path.join(TEST_DIR, 'somefile.json')
-      var obj = {name: 'JP'}
-      jf.spaces = 2
-      jf.writeFileSync(file, obj)
-
-      var data = fs.readFileSync(file, 'utf8')
-      assert.equal(data, '{\n  "name": "JP"\n}\n')
-
-      // restore default
-      jf.spaces = null
-    })
-  })
-
   describe('> when JSON replacer is set', function () {
     it('should replace JSON', function () {
       var file = path.join(TEST_DIR, 'somefile.json')
@@ -78,6 +63,14 @@ describe('+ writeFileSync()', function () {
       var data = fs.readFileSync(file, 'utf8')
       assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
     })
+
+    it('should use EOL override', function () {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'JP' }
+      jf.writeFileSync(file, obj, {spaces: 2, EOL: '***'})
+      var data = fs.readFileSync(file, 'utf8')
+      assert.strictEqual(data, '{***  "name": "JP"***}***')
+    })
   })
 
   describe('> when passing encoding string as options', function () {
diff --git a/test/write-file.test.js b/test/write-file.test.js
index 6eb1d31..0334c54 100644
--- a/test/write-file.test.js
+++ b/test/write-file.test.js
@@ -39,24 +39,6 @@ describe('+ writeFile()', function () {
     })
   })
 
-  describe('> when global spaces is set', function () {
-    it('should write JSON with spacing', function (done) {
-      var file = path.join(TEST_DIR, 'somefile.json')
-      var obj = {name: 'JP'}
-      jf.spaces = 2
-      jf.writeFile(file, obj, function (err) {
-        assert.ifError(err)
-
-        var data = fs.readFileSync(file, 'utf8')
-        assert.equal(data, '{\n  "name": "JP"\n}\n')
-
-        // restore default
-        jf.spaces = null
-        done()
-      })
-    })
-  })
-
   describe('> when JSON replacer is set', function () {
     it('should replace JSON', function (done) {
       var file = path.join(TEST_DIR, 'somefile.json')
@@ -104,6 +86,17 @@ describe('+ writeFile()', function () {
         done()
       })
     })
+
+    it('should use EOL override', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'jp' }
+      jf.writeFile(file, obj, {spaces: 2, EOL: '***'}, function (err) {
+        assert.ifError(err)
+        var data = fs.readFileSync(file, 'utf8')
+        assert.strictEqual(data, '{***  "name": "jp"***}***')
+        done()
+      })
+    })
   })
 
   describe('> when passing encoding string as options', function () {

-- 
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