Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonfile

Package Overview
Dependencies
Maintainers
1
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 2.1.2 to 2.2.0

4

CHANGELOG.md

@@ -0,1 +1,5 @@

2.2.0 / 2015-06-25
------------------
- added `options.spaces` to `writeFile()` and `writeFileSync()`
2.1.2 / 2015-06-22

@@ -2,0 +6,0 @@ ------------------

12

index.js

@@ -45,8 +45,13 @@ var fs = require('fs')

callback = options
options = {}
options = options || {}
}
var spaces = options
? 'spaces' in options
? options.spaces : this.spaces
: this.spaces
var str = ''
try {
str = JSON.stringify(obj, options ? options.replacer : null, this.spaces) + '\n'
str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
} catch (err) {

@@ -61,3 +66,4 @@ if (callback) return callback(err, null)

options = options || {}
var str = JSON.stringify(obj, options.replacer, this.spaces) + '\n'
var spaces = 'spaces' in options ? options.spaces : this.spaces
var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
// not sure if fs.writeFileSync returns anything, but just in case

@@ -64,0 +70,0 @@ return fs.writeFileSync(file, str, options)

{
"name": "jsonfile",
"version": "2.1.2",
"version": "2.2.0",
"description": "Easily read/write JSON files.",

@@ -5,0 +5,0 @@ "repository": {

@@ -6,3 +6,3 @@ Node.js - jsonfile

[![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.png)](http://travis-ci.org/jprichardson/node-jsonfile)
[![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.svg)](http://travis-ci.org/jprichardson/node-jsonfile)

@@ -33,7 +33,7 @@

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

@@ -50,3 +50,3 @@ })

```js
var jf = require('jsonfile')
var jsonfile = require('jsonfile')
var util = require('util')

@@ -56,3 +56,3 @@

console.dir(jf.readFileSync(file))
console.dir(jsonfile.readFileSync(file))
```

@@ -63,7 +63,7 @@

`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).
`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`.
```js
var jf = require('jsonfile')
var jsonfile = require('jsonfile')

@@ -73,3 +73,3 @@ var file = '/tmp/data.json'

jf.writeFile(file, obj, function(err) {
jsonfile.writeFile(file, obj, function(err) {
console.log(err)

@@ -79,9 +79,22 @@ })

**formatting with spaces:**
```js
var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
console.log(err)
})
```
### writeFileSync(filename, [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).
`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`.
```js
var jf = require('jsonfile')
var jsonfile = require('jsonfile')

@@ -91,9 +104,21 @@ var file = '/tmp/data.json'

jf.writeFileSync(file, obj)
jsonfile.writeFileSync(file, obj)
```
**formatting with spaces:**
```js
var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
var obj = {name: 'JP'}
jsonfile.writeFileSync(file, obj, {spaces: 2})
```
### spaces
Number of spaces to indent JSON files.
Global configuration to set spaces to indent JSON files.

@@ -103,5 +128,5 @@ **default:** `null`

```js
var jf = require('jsonfile')
var jsonfile = require('jsonfile')
jf.spaces = 4;
jsonfile.spaces = 4;

@@ -111,3 +136,3 @@ var file = '/tmp/data.json'

jf.writeFile(file, obj, function(err) { //json file has four space indenting now
jsonfile.writeFile(file, obj, function(err) { //json file has four space indenting now
console.log(err)

@@ -117,21 +142,24 @@ })

Note, it's bound to `this.spaces`. So, if you do this:
Contributions
-------------
```js
var myObj = {}
myObj.writeJsonSync = jsonfile.writeFileSync
// => this.spaces = null
```
If you contribute to this library, please don't change the version numbers in your pull request.
Could do the following:
```js
var jsonfile = require('jsonfile')
jsonfile.spaces = 4
jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
### Contributors
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
```
(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

@@ -138,0 +166,0 @@ -------

@@ -241,2 +241,15 @@ var assert = require('assert')

})
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()
})
})
})
})

@@ -293,2 +306,12 @@

})
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')
})
})
})

@@ -295,0 +318,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