[Pkg-javascript-commits] [node-jsonfile] 03/09: New upstream version 2.4.0

Julien Puydt julien.puydt at laposte.net
Sun Oct 16 18:31:50 UTC 2016


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 5a14c08fd55305615bef38ad8dc6082c28951047
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun Oct 16 19:47:43 2016 +0200

    New upstream version 2.4.0
---
 .travis.yml                  |  16 ++-
 CHANGELOG.md                 |  98 ++++++++++++++++-
 LICENSE                      |   8 +-
 README.md                    | 128 ++++++++++++++--------
 appveyor.yml                 |  28 +++++
 index.js                     | 133 +++++++++++++++++++++++
 lib/jsonfile.js              |  59 -----------
 package.json                 |  29 ++---
 test/jsonfile.test.js        | 102 ------------------
 test/mocha.opts              |   3 -
 test/read-file-sync.test.js  | 175 ++++++++++++++++++++++++++++++
 test/read-file.test.js       | 247 +++++++++++++++++++++++++++++++++++++++++++
 test/test.js                 |  29 +++++
 test/write-file-sync.test.js | 104 ++++++++++++++++++
 test/write-file.test.js      | 136 ++++++++++++++++++++++++
 15 files changed, 1064 insertions(+), 231 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 9457120..da0d28c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,4 +1,16 @@
+sudo: false
 language: node_js
 node_js:
-  - 0.11
-  - 0.10
\ No newline at end of file
+  - "0.10"
+  - "0.12"
+  - "io.js"
+  - "4"
+  - "5"
+  - "6"
+matrix:
+  include:
+    - node_js: "4"
+      env: TEST_SUITE=lint
+env:
+  - TEST_SUITE=unit
+script: npm run-script $TEST_SUITE
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 271739b..66fcbb4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,51 @@
+2.4.0 / 2016-09-15
+------------------
+### Changed
+- added optional support for `graceful-fs` [#62]
+
+2.3.1 / 2016-05-13
+------------------
+- fix to support BOM. [#45][#45]
+
+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
+------------------
+- include file name in parse error. See: https://github.com/jprichardson/node-jsonfile/pull/34
+
+2.2.2 / 2015-09-16
+------------------
+- split out tests into separate files
+- fixed `throws` when set to `true` in `readFileSync()`. See: https://github.com/jprichardson/node-jsonfile/pull/33
+
+2.2.1 / 2015-06-25
+------------------
+- fixed regression when passing in string as encoding for options in `writeFile()` and `writeFileSync()`. See: https://github.com/jprichardson/node-jsonfile/issues/28
+
+2.2.0 / 2015-06-25
+------------------
+- added `options.spaces` to `writeFile()` and `writeFileSync()`
+
+2.1.2 / 2015-06-22
+------------------
+- fixed if passed `readFileSync(file, 'utf8')`. See: https://github.com/jprichardson/node-jsonfile/issues/25
+
+2.1.1 / 2015-06-19
+------------------
+- fixed regressions if `null` is passed for options. See: https://github.com/jprichardson/node-jsonfile/issues/24
+
+2.1.0 / 2015-06-19
+------------------
+- cleanup: JavaScript Standard Style, rename files, dropped terst for assert
+- methods now support JSON revivers/replacers
+
+2.0.1 / 2015-05-24
+------------------
+- update license attribute https://github.com/jprichardson/node-jsonfile/pull/21
+
 2.0.0 / 2014-07-28
 ------------------
 * added `\n` to end of file on write. [#14](https://github.com/jprichardson/node-jsonfile/pull/14)
@@ -7,8 +55,8 @@
 1.2.0 / 2014-06-29
 ------------------
 * removed semicolons
-* bugfix: passed `options` to `fs.readFile` and `fs.readFileSync`. This technically changes behavior, but 
-changes it according to docs. #12
+* bugfix: passed `options` to `fs.readFile` and `fs.readFileSync`. This technically changes behavior, but
+changes it according to docs. [#12][#12]
 
 1.1.1 / 2013-11-11
 ------------------
@@ -30,3 +78,49 @@ changes it according to docs. #12
 0.0.1 / 2012-09-10
 ------------------
 * Initial release.
+
+[#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"
+[#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"
diff --git a/LICENSE b/LICENSE
index 83642bf..cb7e807 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,15 +1,15 @@
 (The MIT License)
 
-Copyright (c) 2012, JP Richardson <jprichardson at gmail.com>
+Copyright (c) 2012-2015, JP Richardson <jprichardson at gmail.com>
 
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
+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 
+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.
diff --git a/README.md b/README.md
index 0e67ca3..54bca05 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,13 @@
-[![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.png)](http://travis-ci.org/jprichardson/node-jsonfile)
-
 Node.js - jsonfile
 ================
 
-Easily read/write JSON files. 
+Easily read/write JSON files.
+
+[![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>
 
 Why?
 ----
@@ -16,7 +19,7 @@ Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs
 Installation
 ------------
 
-    npm install jsonfile --save
+    npm install --save jsonfile
 
 
 
@@ -25,102 +28,135 @@ API
 
 ### readFile(filename, [options], callback)
 
-```javascript
-var jf = require('jsonfile')
-var util = require('util')
+`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.
 
+
+```js
+var jsonfile = require('jsonfile')
 var file = '/tmp/data.json'
-jf.readFile(file, function(err, obj) {
-  console.log(util.inspect(obj))
+jsonfile.readFile(file, function(err, obj) {
+  console.dir(obj)
 })
 ```
 
 
 ### readFileSync(filename, [options])
 
-```javascript
-var jf = require('jsonfile')
-var util = require('util')
+`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 file = '/tmp/data.json'
 
-console.log(util.inspect(jf.readFileSync(file)))
+console.dir(jsonfile.readFileSync(file))
 ```
 
-**options**: `throws`. Set to `false` if you don't ever want this method to throw on invalid JSON. Will return `null` instead. Defaults to `true`. Others passed directly to `fs.readFileSync`. 
 
+### 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`.
 
-### writeFile(filename, [options], callback)
 
-```javascript
-var jf = require('jsonfile')
+```js
+var jsonfile = require('jsonfile')
 
 var file = '/tmp/data.json'
 var obj = {name: 'JP'}
 
-jf.writeFile(file, obj, function(err) {
-  console.log(err)
+jsonfile.writeFile(file, obj, function (err) {
+  console.error(err)
 })
 ```
 
-### writeFileSync(filename, [options])
+**formatting with spaces:**
 
-```javascript
-var jf = require('jsonfile')
+```js
+var jsonfile = require('jsonfile')
 
 var file = '/tmp/data.json'
 var obj = {name: 'JP'}
 
-jf.writeFileSync(file, obj)
+jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
+  console.error(err)
+})
 ```
 
 
-### spaces
+### writeFileSync(filename, obj, [options])
 
-Number of spaces to indent JSON files. 
+`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`.
 
-**default:** 2
+```js
+var jsonfile = require('jsonfile')
 
+var file = '/tmp/data.json'
+var obj = {name: 'JP'}
+
+jsonfile.writeFileSync(file, obj)
 ```
-var jf = require('jsonfile')
 
-jf.spaces = 4;
+**formatting with spaces:**
+
+```js
+var jsonfile = require('jsonfile')
 
 var file = '/tmp/data.json'
 var obj = {name: 'JP'}
 
-jf.writeFile(file, obj, function(err) { //json file has four space indenting now
-  console.log(err)
-})
+jsonfile.writeFileSync(file, obj, {spaces: 2})
 ```
 
 
-Contributions
--------------
 
-If you contribute to this library, please don't change the version numbers in your pull request.
+### spaces
+
+Global configuration to set spaces to indent JSON files.
+
+**default:** `null`
 
+```js
+var jsonfile = require('jsonfile')
 
-### Contributors
+jsonfile.spaces = 4
 
-(You can add your name, or I'll add it if you forget)
+var file = '/tmp/data.json'
+var obj = {name: 'JP'}
 
-- [*] [JP Richardson](https://github.com/jprichardson)
-- [2] [Sean O'Dell](https://github.com/seanodell)
-- [1] [Federico Fissore](https://github.com/ffissore)
-- [1] [Ivan McCarthy](https://github.com/imcrthy)
-- [1] [Pablo Vallejo](https://github.com/PabloVallejo)
-- [1] [Miroslav Bajtoš](https://github.com/bajtos)
+// 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:
 
-License
--------
+```js
+var myObj = {}
+myObj.writeJsonSync = jsonfile.writeFileSync
+// => this.spaces = null
+```
 
-(MIT License)
+Could do the following:
 
-Copyright 2012-2014, JP Richardson  <jprichardson at gmail.com>
+```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
+```
 
 
+License
+-------
 
+(MIT License)
 
+Copyright 2012-2016, JP Richardson  <jprichardson at gmail.com>
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..872af18
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,28 @@
+# Test against this version of Node.js
+environment:
+  matrix:
+    # node.js
+    - nodejs_version: "0.10"
+    - nodejs_version: "0.12"
+    - nodejs_version: "4"
+    - nodejs_version: "5"
+    - nodejs_version: "6"
+
+# Install scripts. (runs after repo cloning)
+install:
+  # Get the latest stable version of Node.js or io.js
+  - ps: Install-Product node $env:nodejs_version
+  # install modules
+  - npm config set loglevel warn
+  - npm install --silent
+
+# Post-install test scripts.
+test_script:
+  # Output useful info for debugging.
+  - node --version
+  - npm --version
+  # run tests
+  - npm test
+
+# Don't actually build.
+build: off
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..7111e15
--- /dev/null
+++ b/index.js
@@ -0,0 +1,133 @@
+var _fs
+try {
+  _fs = require('graceful-fs')
+} catch (_) {
+  _fs = require('fs')
+}
+
+function readFile (file, options, callback) {
+  if (callback == null) {
+    callback = options
+    options = {}
+  }
+
+  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) {
+    if (err) return callback(err)
+
+    data = stripBom(data)
+
+    var obj
+    try {
+      obj = JSON.parse(data, options ? options.reviver : null)
+    } catch (err2) {
+      if (shouldThrow) {
+        err2.message = file + ': ' + err2.message
+        return callback(err2)
+      } else {
+        return callback(null, null)
+      }
+    }
+
+    callback(null, obj)
+  })
+}
+
+function readFileSync (file, options) {
+  options = options || {}
+  if (typeof options === 'string') {
+    options = {encoding: 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
+  }
+
+  var content = fs.readFileSync(file, options)
+  content = stripBom(content)
+
+  try {
+    return JSON.parse(content, options.reviver)
+  } catch (err) {
+    if (shouldThrow) {
+      err.message = file + ': ' + err.message
+      throw err
+    } else {
+      return null
+    }
+  }
+}
+
+function writeFile (file, obj, options, callback) {
+  if (callback == null) {
+    callback = options
+    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 = ''
+  try {
+    str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
+  } catch (err) {
+    if (callback) return callback(err, null)
+  }
+
+  fs.writeFile(file, str, options, callback)
+}
+
+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'
+  // not sure if fs.writeFileSync returns anything, but just in case
+  return fs.writeFileSync(file, str, options)
+}
+
+function stripBom (content) {
+  // we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
+  if (Buffer.isBuffer(content)) content = content.toString('utf8')
+  content = content.replace(/^\uFEFF/, '')
+  return content
+}
+
+var jsonfile = {
+  spaces: null,
+  readFile: readFile,
+  readFileSync: readFileSync,
+  writeFile: writeFile,
+  writeFileSync: writeFileSync
+}
+
+module.exports = jsonfile
diff --git a/lib/jsonfile.js b/lib/jsonfile.js
deleted file mode 100644
index a18e2c0..0000000
--- a/lib/jsonfile.js
+++ /dev/null
@@ -1,59 +0,0 @@
-var fs = require('fs')
-
-var me = module.exports
-
-me.spaces = 2
-
-me.readFile = function(file, options, callback) {
-  if (callback == undefined) {
-    callback = options
-    options = null
-  }
-
-  fs.readFile(file, options, function(err, data) {
-    if (err) return callback(err, null)
-
-    var obj = null
-    try {
-      obj = JSON.parse(data)
-    } catch (err2) {
-      return callback(err2, null)
-    }
-
-    callback(null, obj)
-  })
-}
-
-me.readFileSync = function(file, options) {
-  var noThrow = options && !options.throws
-
-  if (!noThrow) //i.e. throw on invalid JSON
-    return JSON.parse(fs.readFileSync(file, options))
-  else 
-    try {
-      return JSON.parse(fs.readFileSync(file, options))
-    } catch (err) {
-      return null
-    }
-}
-
-me.writeFile = function(file, obj, options, callback) {
-  if (callback == undefined) {
-    callback = options
-    options = null
-  }
-
-  var str = ''
-  try {
-    str = JSON.stringify(obj, null, me.spaces) + '\n';
-  } catch (err) {
-    if (callback) return callback(err, null)
-  }
-
-  fs.writeFile(file, str, options, callback)
-}
-
-me.writeFileSync = function(file, obj, options) {
-  var str = JSON.stringify(obj, null, me.spaces) + '\n';
-  return fs.writeFileSync(file, str, options) //not sure if fs.writeFileSync returns anything, but just in case
-}
diff --git a/package.json b/package.json
index b93b3b9..ea7ac1e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "jsonfile",
-  "version": "2.0.0",
+  "version": "2.4.0",
   "description": "Easily read/write JSON files.",
   "repository": {
     "type": "git",
@@ -10,23 +10,26 @@
     "read",
     "write",
     "file",
-    "json"
+    "json",
+    "fs",
+    "fs-extra"
   ],
   "author": "JP Richardson <jprichardson at gmail.com>",
-  "licenses": [
-    {
-      "type": "MIT",
-      "url": ""
-    }
-  ],
+  "license": "MIT",
   "dependencies": {},
+  "optionalDependencies": {
+    "graceful-fs": "^4.1.6"
+  },
   "devDependencies": {
-    "testutil": "^0.7.0",
-    "mocha": "*",
-    "terst": "^0.2.0"
+    "mocha": "2.x",
+    "mock-fs": "^3.8.0",
+    "rimraf": "^2.4.0",
+    "standard": "^6.0.8"
   },
-  "main": "./lib/jsonfile.js",
+  "main": "index.js",
   "scripts": {
-    "test": "mocha test"
+    "lint": "standard",
+    "test": "npm run lint && npm run unit",
+    "unit": "mocha"
   }
 }
diff --git a/test/jsonfile.test.js b/test/jsonfile.test.js
deleted file mode 100644
index 5e259fb..0000000
--- a/test/jsonfile.test.js
+++ /dev/null
@@ -1,102 +0,0 @@
-var fs = require('fs')
-var path = require('path')
-var testutil = require('testutil')
-var jf = require('../')
-
-require('terst')
-
-var TEST_DIR = ''
-
-beforeEach(function() {
-  TEST_DIR = testutil.createTestDir('jsonfile')
-})
-
-suite('jsonfile')
-
-test('- readFile()', function(done) {
-  var file = path.join(TEST_DIR, 'somefile.json')
-  var obj = {name: 'JP'}
-  fs.writeFileSync(file, JSON.stringify(obj))
-
-  jf.readFile(file, function(err, obj2) {
-    F (err)
-    T (obj2.name === obj.name)
-    done()
-  })
-})
-
-test('- writeFile()', function(done) {
-  var file = path.join(TEST_DIR, 'somefile2.json')
-  var obj = {name: 'JP'}
-
-  jf.writeFile(file, obj, function(err) {
-    F (err)
-    fs.readFile(file, 'utf8', function(err,data) {
-      var obj2 = JSON.parse(data)
-      T (obj2.name === obj.name)
-      done()
-    })
-  })
-})
-
-test('- writeFile() adds EOL at EOF', function(done) {
-  var file = path.join(TEST_DIR, 'eol.json')
-  var obj = {name: 'JP'}
-
-  jf.writeFile(file, obj, function(err) {
-    F (err)
-    fs.readFile(file, 'utf8', function(err,data) {
-      T (data[data.length-1] === '\n')
-      done()
-    })
-  })
-})
-
-test('- readFileSync()', function(done) {
-  var file = path.join(TEST_DIR, 'somefile3.json')
-  var obj = {name: 'JP'}
-  fs.writeFileSync(file, JSON.stringify(obj))
-
-  try {
-    var obj2 = jf.readFileSync(file)
-    T (obj2.name === obj.name)
-    done()
-  } catch (err) {
-    done(err)
-  }
-})
-
-test(' - readFileSync() / when invalid JSON and throws option set to false', function() {
-  var file = path.join(TEST_DIR, 'somefile4-invalid.json')
-  var data = "{not valid JSON"
-  fs.writeFileSync(file, data)
-
-  THROWS(function() {
-    jf.readFileSync(file)
-  })
-
-  var obj = jf.readFileSync(file, {throws: false})
-  EQ (obj, null)
-})
-
-test('- writeFileSync()', function(done) {
-  var file = path.join(TEST_DIR, 'somefile4.json')
-  var obj = {name: 'JP'}
-
-  jf.writeFileSync(file, obj)
-
-  var obj2 = JSON.parse(fs.readFileSync(file, 'utf8'))
-  T (obj2.name === obj.name)
-  done()
-})
-
-test('- writeFileSync() adds EOL at EOF', function(done) {
-  var file = path.join(TEST_DIR, 'eol-sync.json')
-  var obj = {name: 'JP'}
-
-  jf.writeFileSync(file, obj)
-
-  var data = fs.readFileSync(file, 'utf8')
-  T (data[data.length-1] === '\n')
-  done()
-})
diff --git a/test/mocha.opts b/test/mocha.opts
deleted file mode 100644
index d779542..0000000
--- a/test/mocha.opts
+++ /dev/null
@@ -1,3 +0,0 @@
---reporter spec
---ui qunit
---timeout 2000
\ No newline at end of file
diff --git a/test/read-file-sync.test.js b/test/read-file-sync.test.js
new file mode 100644
index 0000000..d106fb5
--- /dev/null
+++ b/test/read-file-sync.test.js
@@ -0,0 +1,175 @@
+var assert = require('assert')
+var fs = require('fs')
+var mockfs = require('mock-fs')
+var os = require('os')
+var path = require('path')
+var rimraf = require('rimraf')
+var jf = require('../')
+
+/* global describe it beforeEach afterEach */
+
+describe('+ readFileSync()', function () {
+  var TEST_DIR
+
+  beforeEach(function (done) {
+    TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-readfile-sync')
+    rimraf.sync(TEST_DIR)
+    fs.mkdir(TEST_DIR, done)
+  })
+
+  afterEach(function (done) {
+    rimraf.sync(TEST_DIR)
+    done()
+  })
+
+  it('should read and parse JSON', function () {
+    var file = path.join(TEST_DIR, 'somefile3.json')
+    var obj = {name: 'JP'}
+    fs.writeFileSync(file, JSON.stringify(obj))
+
+    try {
+      var obj2 = jf.readFileSync(file)
+      assert.equal(obj2.name, obj.name)
+    } catch (err) {
+      assert(err)
+    }
+  })
+
+  describe('> when invalid JSON', function () {
+    it('should include the filename in the error', function () {
+      var fn = 'somefile.json'
+      var file = path.join(TEST_DIR, fn)
+      fs.writeFileSync(file, '{')
+
+      assert.throws(function () {
+        jf.readFileSync(file)
+      }, function (err) {
+        assert(err instanceof Error)
+        assert(err.message.match(fn))
+        return true
+      })
+    })
+  })
+
+  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')
+      var data = '{not valid JSON'
+      fs.writeFileSync(file, data)
+
+      assert.throws(function () {
+        jf.readFileSync(file)
+      })
+
+      var obj = jf.readFileSync(file, {throws: false})
+      assert.strictEqual(obj, null)
+    })
+  })
+
+  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 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 JSON reviver is set', function () {
+    it('should transform the JSON', function () {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var sillyReviver = function (k, v) {
+        if (typeof v !== 'string') return v
+        if (v.indexOf('date:') < 0) return v
+        return new Date(v.split('date:')[1])
+      }
+
+      var obj = {
+        name: 'jp',
+        day: 'date:2015-06-19T11:41:26.815Z'
+      }
+
+      fs.writeFileSync(file, JSON.stringify(obj))
+      var data = jf.readFileSync(file, {reviver: sillyReviver})
+      assert.strictEqual(data.name, 'jp')
+      assert(data.day instanceof Date)
+      assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z')
+    })
+  })
+
+  describe('> when passing encoding string as option', function () {
+    it('should not throw an error', function () {
+      var file = path.join(TEST_DIR, 'somefile.json')
+
+      var obj = {
+        name: 'jp'
+      }
+      fs.writeFileSync(file, JSON.stringify(obj))
+
+      try {
+        var data = jf.readFileSync(file, 'utf8')
+      } catch (err) {
+        assert.ifError(err)
+      }
+      assert.strictEqual(data.name, 'jp')
+    })
+  })
+
+  describe('mockfs', function () {
+    it('should read from a mockfs', function () {
+      var mfs = mockfs.fs()
+      var dataOut = {name: 'JP'}
+      mfs.writeFileSync('test.json', JSON.stringify(dataOut), 'utf8')
+      var dataIn = jf.readFileSync('test.json', { fs: mfs })
+      assert.deepEqual(dataOut, dataIn)
+    })
+  })
+
+  describe('> w/ BOM', function () {
+    it('should properly parse', function () {
+      var file = path.join(TEST_DIR, 'file-bom.json')
+      var obj = { name: 'JP' }
+      fs.writeFileSync(file, '\uFEFF' + JSON.stringify(obj))
+      var data = jf.readFileSync(file)
+      assert.deepEqual(obj, data)
+    })
+  })
+})
diff --git a/test/read-file.test.js b/test/read-file.test.js
new file mode 100644
index 0000000..5de9832
--- /dev/null
+++ b/test/read-file.test.js
@@ -0,0 +1,247 @@
+var assert = require('assert')
+var fs = require('fs')
+var mockfs = require('mock-fs')
+var os = require('os')
+var path = require('path')
+var rimraf = require('rimraf')
+var jf = require('../')
+
+/* global describe it beforeEach afterEach */
+
+describe('+ readFile()', function () {
+  var TEST_DIR
+
+  beforeEach(function (done) {
+    TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-readfile')
+    rimraf.sync(TEST_DIR)
+    fs.mkdir(TEST_DIR, done)
+  })
+
+  afterEach(function (done) {
+    rimraf.sync(TEST_DIR)
+    done()
+  })
+
+  it('should read and parse JSON', function (done) {
+    var file = path.join(TEST_DIR, 'somefile.json')
+    var obj = {name: 'JP'}
+    fs.writeFileSync(file, JSON.stringify(obj))
+
+    jf.readFile(file, function (err, obj2) {
+      assert.ifError(err)
+      assert.equal(obj2.name, obj.name)
+      done()
+    })
+  })
+
+  describe('> when invalid JSON', function () {
+    it('should include the filename in the error', function (done) {
+      var fn = 'somefile.json'
+      var file = path.join(TEST_DIR, fn)
+      fs.writeFileSync(file, '{')
+
+      jf.readFile(file, function (err, obj2) {
+        assert(err instanceof Error)
+        assert(err.message.match(fn))
+        done()
+      })
+    })
+  })
+
+  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 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'
+      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 JSON reviver is set', function () {
+    it('should transform the JSON', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var sillyReviver = function (k, v) {
+        if (typeof v !== 'string') return v
+        if (v.indexOf('date:') < 0) return v
+        return new Date(v.split('date:')[1])
+      }
+
+      var obj = {
+        name: 'jp',
+        day: 'date:2015-06-19T11:41:26.815Z'
+      }
+
+      fs.writeFileSync(file, JSON.stringify(obj))
+      jf.readFile(file, {reviver: sillyReviver}, function (err, data) {
+        assert.ifError(err)
+        assert.strictEqual(data.name, 'jp')
+        assert(data.day instanceof Date)
+        assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z')
+        done()
+      })
+    })
+  })
+
+  describe('> when passing null and callback', function () {
+    it('should not throw an error', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+
+      var obj = {
+        name: 'jp'
+      }
+      fs.writeFileSync(file, JSON.stringify(obj))
+
+      jf.readFile(file, null, function (err) {
+        assert.ifError(err)
+        assert.strictEqual(obj.name, 'jp')
+        done()
+      })
+    })
+  })
+
+  describe('> when passing encoding string as option', function () {
+    it('should not throw an error', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+
+      var obj = {
+        name: 'jp'
+      }
+      fs.writeFileSync(file, JSON.stringify(obj))
+
+      jf.readFile(file, 'utf8', function (err) {
+        assert.ifError(err)
+        assert.strictEqual(obj.name, 'jp')
+        done()
+      })
+    })
+  })
+
+  describe('mockfs', function () {
+    it('should read from a mockfs', function (done) {
+      var mfs = mockfs.fs()
+      var dataOut = {name: 'JP'}
+      mfs.writeFileSync('test.json', JSON.stringify(dataOut), 'utf8')
+      jf.readFile('test.json', { fs: mfs }, function (err, dataIn) {
+        assert.ifError(err)
+        assert.deepEqual(dataOut, dataIn)
+        done()
+      })
+    })
+  })
+
+  describe('> w/ BOM', function () {
+    it('should properly parse', function (done) {
+      var file = path.join(TEST_DIR, 'file-bom.json')
+      var obj = { name: 'JP' }
+      fs.writeFileSync(file, '\uFEFF' + JSON.stringify(obj))
+      jf.readFile(file, function (err, data) {
+        assert.ifError(err)
+        assert.deepEqual(obj, data)
+        done()
+      })
+    })
+  })
+})
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..66e8a08
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,29 @@
+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
new file mode 100644
index 0000000..0b6ff6b
--- /dev/null
+++ b/test/write-file-sync.test.js
@@ -0,0 +1,104 @@
+var assert = require('assert')
+var fs = require('fs')
+var mockfs = require('mock-fs')
+var os = require('os')
+var path = require('path')
+var rimraf = require('rimraf')
+var jf = require('../')
+
+/* global describe it beforeEach afterEach */
+
+describe('+ writeFileSync()', function () {
+  var TEST_DIR
+
+  beforeEach(function (done) {
+    TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-writefile-sync')
+    rimraf.sync(TEST_DIR)
+    fs.mkdir(TEST_DIR, done)
+  })
+
+  afterEach(function (done) {
+    rimraf.sync(TEST_DIR)
+    done()
+  })
+
+  it('should serialize the JSON and write it to file', function () {
+    var file = path.join(TEST_DIR, 'somefile4.json')
+    var obj = {name: 'JP'}
+
+    jf.writeFileSync(file, obj)
+
+    var data = fs.readFileSync(file, 'utf8')
+    var obj2 = JSON.parse(data)
+    assert.equal(obj2.name, obj.name)
+    assert.equal(data[data.length - 1], '\n')
+    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')
+      var sillyReplacer = function (k, v) {
+        if (!(v instanceof RegExp)) return v
+        return 'regex:' + v.toString()
+      }
+
+      var obj = {
+        name: 'jp',
+        reg: new RegExp(/hello/g)
+      }
+
+      jf.writeFileSync(file, obj, {replacer: sillyReplacer})
+      var data = JSON.parse(fs.readFileSync(file))
+      assert.strictEqual(data.name, 'jp')
+      assert.strictEqual(typeof data.reg, 'string')
+      assert.strictEqual(data.reg, 'regex:/hello/g')
+    })
+  })
+
+  describe('> when spaces passed as an option', function () {
+    it('should write file with spaces', function () {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'JP' }
+      jf.writeFileSync(file, obj, {spaces: 8})
+      var data = fs.readFileSync(file, 'utf8')
+      assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
+    })
+  })
+
+  describe('> when passing encoding string as options', function () {
+    it('should not error', function () {
+      var file = path.join(TEST_DIR, 'somefile6.json')
+      var obj = { name: 'jp' }
+      jf.writeFileSync(file, obj, 'utf8')
+      var data = fs.readFileSync(file, 'utf8')
+      assert.strictEqual(data, JSON.stringify(obj) + '\n')
+    })
+  })
+
+  describe('mockfs', function () {
+    it('should write to mockfs', function () {
+      var mfs = mockfs.fs()
+      var dataOut = { name: 'JP' }
+      var file = 'somefile.json'
+      jf.writeFileSync(file, dataOut, { fs: mfs })
+      var dataIn = JSON.parse(mfs.readFileSync(file, 'utf8'))
+      assert.deepEqual(dataOut, dataIn)
+    })
+  })
+})
diff --git a/test/write-file.test.js b/test/write-file.test.js
new file mode 100644
index 0000000..183a6a5
--- /dev/null
+++ b/test/write-file.test.js
@@ -0,0 +1,136 @@
+var assert = require('assert')
+var fs = require('fs')
+var mockfs = require('mock-fs')
+var os = require('os')
+var path = require('path')
+var rimraf = require('rimraf')
+var jf = require('../')
+
+/* global describe it beforeEach afterEach */
+
+describe('+ writeFile()', function () {
+  var TEST_DIR
+
+  beforeEach(function (done) {
+    TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-writefile')
+    rimraf.sync(TEST_DIR)
+    fs.mkdir(TEST_DIR, done)
+  })
+
+  afterEach(function (done) {
+    rimraf.sync(TEST_DIR)
+    done()
+  })
+
+  it('should serialize and write JSON', function (done) {
+    var file = path.join(TEST_DIR, 'somefile2.json')
+    var obj = {name: 'JP'}
+
+    jf.writeFile(file, obj, function (err) {
+      assert.ifError(err)
+      fs.readFile(file, 'utf8', function (err, data) {
+        assert.ifError(err)
+        var obj2 = JSON.parse(data)
+        assert.equal(obj2.name, obj.name)
+
+        // verify EOL
+        assert.equal(data[data.length - 1], '\n')
+        done()
+      })
+    })
+  })
+
+  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')
+      var sillyReplacer = function (k, v) {
+        if (!(v instanceof RegExp)) return v
+        return 'regex:' + v.toString()
+      }
+
+      var obj = {
+        name: 'jp',
+        reg: new RegExp(/hello/g)
+      }
+
+      jf.writeFile(file, obj, {replacer: sillyReplacer}, function (err) {
+        assert.ifError(err)
+
+        var data = JSON.parse(fs.readFileSync(file))
+        assert.strictEqual(data.name, 'jp')
+        assert.strictEqual(typeof data.reg, 'string')
+        assert.strictEqual(data.reg, 'regex:/hello/g')
+        done()
+      })
+    })
+  })
+
+  describe('> when passing null and callback', function () {
+    it('should not throw an error', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'jp' }
+      jf.writeFile(file, obj, null, function (err) {
+        assert.ifError(err)
+        done()
+      })
+    })
+  })
+
+  describe('> when spaces passed as an option', function () {
+    it('should write file with spaces', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'jp' }
+      jf.writeFile(file, obj, {spaces: 8}, function (err) {
+        assert.ifError(err)
+        var data = fs.readFileSync(file, 'utf8')
+        assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
+        done()
+      })
+    })
+  })
+
+  describe('> when passing encoding string as options', function () {
+    it('should not error', function (done) {
+      var file = path.join(TEST_DIR, 'somefile.json')
+      var obj = { name: 'jp' }
+      jf.writeFile(file, obj, 'utf8', function (err) {
+        assert.ifError(err)
+        var data = fs.readFileSync(file, 'utf8')
+        assert.strictEqual(data, JSON.stringify(obj) + '\n')
+        done()
+      })
+    })
+  })
+
+  describe('mockfs', function () {
+    it('should write to mockfs', function (done) {
+      var mfs = mockfs.fs()
+      var dataOut = { name: 'JP' }
+      var file = 'somefile.json'
+      jf.writeFile(file, dataOut, { fs: mfs }, function (err) {
+        assert.ifError(err)
+        var dataIn = JSON.parse(mfs.readFileSync(file, 'utf8'))
+        assert.deepEqual(dataOut, dataIn)
+        done()
+      })
+    })
+  })
+})

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