Socket
Socket
Sign inDemoInstall

jsonfile

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonfile - npm Package Compare versions

Comparing version 4.0.0 to 5.0.0

7

CHANGELOG.md

@@ -0,1 +1,8 @@

5.0.0 / 2018-09-08
------------------
- **BREAKING:** Drop Node 4 support
- **BREAKING:** If no callback is passed to an asynchronous method, a promise is now returned ([#109](https://github.com/jprichardson/node-jsonfile/pull/109))
- Cleanup docs
4.0.0 / 2017-07-12

@@ -2,0 +9,0 @@ ------------------

61

index.js

@@ -1,2 +0,2 @@

var _fs
let _fs
try {

@@ -7,4 +7,5 @@ _fs = require('graceful-fs')

}
const universalify = require('universalify')
function readFile (file, options, callback) {
function readFileWithCallback (file, options, callback) {
if (callback == null) {

@@ -16,9 +17,9 @@ callback = options

if (typeof options === 'string') {
options = {encoding: options}
options = { encoding: options }
}
options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs
var shouldThrow = true
let shouldThrow = true
if ('throws' in options) {

@@ -28,3 +29,3 @@ shouldThrow = options.throws

fs.readFile(file, options, function (err, data) {
fs.readFile(file, options, (err, data) => {
if (err) return callback(err)

@@ -34,3 +35,3 @@

var obj
let obj
try {

@@ -40,3 +41,3 @@ obj = JSON.parse(data, options ? options.reviver : null)

if (shouldThrow) {
err2.message = file + ': ' + err2.message
err2.message = `${file}: ${err2.message}`
return callback(err2)

@@ -52,11 +53,13 @@ } else {

const readFile = universalify.fromCallback(readFileWithCallback)
function readFileSync (file, options) {
options = options || {}
if (typeof options === 'string') {
options = {encoding: options}
options = { encoding: options }
}
var fs = options.fs || _fs
const fs = options.fs || _fs
var shouldThrow = true
let shouldThrow = true
if ('throws' in options) {

@@ -67,3 +70,3 @@ shouldThrow = options.throws

try {
var content = fs.readFileSync(file, options)
let content = fs.readFileSync(file, options)
content = stripBom(content)

@@ -73,3 +76,3 @@ return JSON.parse(content, options.reviver)

if (shouldThrow) {
err.message = file + ': ' + err.message
err.message = `${file}: ${err.message}`
throw err

@@ -83,4 +86,4 @@ } else {

function stringify (obj, options) {
var spaces
var EOL = '\n'
let spaces
let EOL = '\n'
if (typeof options === 'object' && options !== null) {

@@ -95,3 +98,3 @@ if (options.spaces) {

var str = JSON.stringify(obj, options ? options.replacer : null, spaces)
const str = JSON.stringify(obj, options ? options.replacer : null, spaces)

@@ -101,3 +104,3 @@ return str.replace(/\n/g, EOL) + EOL

function writeFile (file, obj, options, callback) {
function writeFileWithCallback (file, obj, options, callback) {
if (callback == null) {

@@ -108,11 +111,9 @@ callback = options

options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs
var str = ''
let str = ''
try {
str = stringify(obj, options)
} catch (err) {
// Need to return whether a callback was passed or not
if (callback) callback(err, null)
return
return callback(err, null)
}

@@ -123,7 +124,9 @@

const writeFile = universalify.fromCallback(writeFileWithCallback)
function writeFileSync (file, obj, options) {
options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs
var str = stringify(obj, options)
const str = stringify(obj, options)
// not sure if fs.writeFileSync returns anything, but just in case

@@ -140,9 +143,9 @@ return fs.writeFileSync(file, str, options)

var jsonfile = {
readFile: readFile,
readFileSync: readFileSync,
writeFile: writeFile,
writeFileSync: writeFileSync
const jsonfile = {
readFile,
readFileSync,
writeFile,
writeFileSync
}
module.exports = jsonfile
{
"name": "jsonfile",
"version": "4.0.0",
"version": "5.0.0",
"description": "Easily read/write JSON files.",

@@ -19,3 +19,5 @@ "repository": {

"license": "MIT",
"dependencies": {},
"dependencies": {
"universalify": "^0.1.2"
},
"optionalDependencies": {

@@ -25,5 +27,5 @@ "graceful-fs": "^4.1.6"

"devDependencies": {
"mocha": "2.x",
"mocha": "^5.2.0",
"rimraf": "^2.4.0",
"standard": "^10.0.3"
"standard": "^12.0.1"
},

@@ -30,0 +32,0 @@ "main": "index.js",

Node.js - jsonfile
================
Easily read/write JSON files.
Easily read/write JSON files in Node.js. _Note: this module cannot be used in the browser._

@@ -37,5 +37,6 @@ [![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile)

```js
var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
jsonfile.readFile(file, function(err, obj) {
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file, function (err, obj) {
if (err) console.error(err)
console.dir(obj)

@@ -45,3 +46,12 @@ })

You can also use this method with promises. The `readFile` method will return a promise if you do not pass a callback function.
```js
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file)
.then(obj => console.dir(obj))
.catch(error => console.error(error))
```
### readFileSync(filename, [options])

@@ -53,4 +63,4 @@

```js
var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'

@@ -67,22 +77,37 @@ console.dir(jsonfile.readFileSync(file))

```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFile(file, obj, function (err) {
console.error(err)
if (err) console.error(err)
})
```
Or use with promises as follows:
```js
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFile(file, obj)
.then(res => {
console.log('Write complete')
})
.catch(error => console.error(error))
```
**formatting with spaces:**
```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
console.error(err)
jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
if (err) console.error(err)
})

@@ -94,9 +119,9 @@ ```

```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
console.error(err)
jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
if (err) console.error(err)
})

@@ -107,12 +132,12 @@ ```

You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
You can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.
```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/mayAlreadyExistedData.json'
var obj = {name: 'JP'}
const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }
jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
console.error(err)
jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
if (err) console.error(err)
})

@@ -126,6 +151,6 @@ ```

```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }

@@ -138,8 +163,8 @@ jsonfile.writeFileSync(file, obj)

```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFileSync(file, obj, {spaces: 2})
jsonfile.writeFileSync(file, obj, { spaces: 2 })
```

@@ -150,8 +175,8 @@

```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
const file = '/tmp/data.json'
const obj = { name: 'JP' }
jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
```

@@ -161,11 +186,11 @@

You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
You can use `fs.writeFileSync` option `{ flag: 'a' }` to achieve this.
```js
var jsonfile = require('jsonfile')
const jsonfile = require('jsonfile')
var file = '/tmp/mayAlreadyExistedData.json'
var obj = {name: 'JP'}
const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }
jsonfile.writeFileSync(file, obj, {flag: 'a'})
jsonfile.writeFileSync(file, obj, { flag: 'a' })
```

@@ -172,0 +197,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc