[Pkg-javascript-commits] [node-jsonfile] 01/02: Imported Upstream version 2.0.0

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Sat Mar 21 00:52:17 UTC 2015


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

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

commit ffa7b68bd39760f917e748902674d0eba55be3f6
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date:   Sat Mar 21 01:30:53 2015 +0100

    Imported Upstream version 2.0.0
---
 .gitignore            |   2 +
 .npmignore            |   2 +
 .travis.yml           |   4 ++
 CHANGELOG.md          |  32 +++++++++++++
 LICENSE               |  15 ++++++
 README.md             | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/jsonfile.js       |  59 +++++++++++++++++++++++
 package.json          |  32 +++++++++++++
 test/jsonfile.test.js | 102 ++++++++++++++++++++++++++++++++++++++++
 test/mocha.opts       |   3 ++
 10 files changed, 377 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2feef5e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules/
+/.project
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..cefaa67
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,2 @@
+test/
+.travis.yml
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..9457120
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - 0.11
+  - 0.10
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..271739b
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,32 @@
+2.0.0 / 2014-07-28
+------------------
+* added `\n` to end of file on write. [#14](https://github.com/jprichardson/node-jsonfile/pull/14)
+* added `options.throws` to `readFileSync()`
+* dropped support for Node v0.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
+
+1.1.1 / 2013-11-11
+------------------
+* fixed catching of callback bug (ffissore / #5)
+
+1.1.0 / 2013-10-11
+------------------
+* added `options` param to methods, (seanodell / #4)
+
+1.0.1 / 2013-09-05
+------------------
+* removed `homepage` field from package.json to remove NPM warning
+
+1.0.0 / 2013-06-28
+------------------
+* added `.npmignore`, #1
+* changed spacing default from `4` to `2` to follow Node conventions
+
+0.0.1 / 2012-09-10
+------------------
+* Initial release.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..83642bf
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+(The MIT License)
+
+Copyright (c) 2012, 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 
+(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 
+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
new file mode 100644
index 0000000..0e67ca3
--- /dev/null
+++ b/README.md
@@ -0,0 +1,126 @@
+[![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. 
+
+
+Why?
+----
+
+Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.
+
+
+
+Installation
+------------
+
+    npm install jsonfile --save
+
+
+
+API
+---
+
+### readFile(filename, [options], callback)
+
+```javascript
+var jf = require('jsonfile')
+var util = require('util')
+
+var file = '/tmp/data.json'
+jf.readFile(file, function(err, obj) {
+  console.log(util.inspect(obj))
+})
+```
+
+
+### readFileSync(filename, [options])
+
+```javascript
+var jf = require('jsonfile')
+var util = require('util')
+
+var file = '/tmp/data.json'
+
+console.log(util.inspect(jf.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, [options], callback)
+
+```javascript
+var jf = require('jsonfile')
+
+var file = '/tmp/data.json'
+var obj = {name: 'JP'}
+
+jf.writeFile(file, obj, function(err) {
+  console.log(err)
+})
+```
+
+### writeFileSync(filename, [options])
+
+```javascript
+var jf = require('jsonfile')
+
+var file = '/tmp/data.json'
+var obj = {name: 'JP'}
+
+jf.writeFileSync(file, obj)
+```
+
+
+### spaces
+
+Number of spaces to indent JSON files. 
+
+**default:** 2
+
+```
+var jf = require('jsonfile')
+
+jf.spaces = 4;
+
+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)
+})
+```
+
+
+Contributions
+-------------
+
+If you contribute to this library, please don't change the version numbers in your pull request.
+
+
+### Contributors
+
+(You can add your name, or I'll add it if you forget)
+
+- [*] [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)
+
+
+License
+-------
+
+(MIT License)
+
+Copyright 2012-2014, JP Richardson  <jprichardson at gmail.com>
+
+
+
+
+
diff --git a/lib/jsonfile.js b/lib/jsonfile.js
new file mode 100644
index 0000000..a18e2c0
--- /dev/null
+++ b/lib/jsonfile.js
@@ -0,0 +1,59 @@
+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
new file mode 100644
index 0000000..b93b3b9
--- /dev/null
+++ b/package.json
@@ -0,0 +1,32 @@
+{
+  "name": "jsonfile",
+  "version": "2.0.0",
+  "description": "Easily read/write JSON files.",
+  "repository": {
+    "type": "git",
+    "url": "git at github.com:jprichardson/node-jsonfile.git"
+  },
+  "keywords": [
+    "read",
+    "write",
+    "file",
+    "json"
+  ],
+  "author": "JP Richardson <jprichardson at gmail.com>",
+  "licenses": [
+    {
+      "type": "MIT",
+      "url": ""
+    }
+  ],
+  "dependencies": {},
+  "devDependencies": {
+    "testutil": "^0.7.0",
+    "mocha": "*",
+    "terst": "^0.2.0"
+  },
+  "main": "./lib/jsonfile.js",
+  "scripts": {
+    "test": "mocha test"
+  }
+}
diff --git a/test/jsonfile.test.js b/test/jsonfile.test.js
new file mode 100644
index 0000000..5e259fb
--- /dev/null
+++ b/test/jsonfile.test.js
@@ -0,0 +1,102 @@
+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
new file mode 100644
index 0000000..d779542
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,3 @@
+--reporter spec
+--ui qunit
+--timeout 2000
\ No newline at end of file

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