Socket
Socket
Sign inDemoInstall

jsonfile

Package Overview
Dependencies
1
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 4.0.0

11

CHANGELOG.md

@@ -0,1 +1,9 @@

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

@@ -92,2 +100,3 @@ ------------------

[#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"

@@ -98,3 +107,3 @@ [#44]: https://github.com/jprichardson/node-jsonfile/issues/44 "Extra characters in written file"

[#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)"

@@ -101,0 +110,0 @@ [#38]: https://github.com/jprichardson/node-jsonfile/pull/38 "Update README.md writeFile[Sync] signature"

42

index.js

@@ -22,6 +22,3 @@ var _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

@@ -60,6 +57,3 @@ }

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

@@ -82,2 +76,19 @@ }

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

@@ -91,10 +102,5 @@ if (callback == null) {

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

@@ -113,8 +119,3 @@ // Need to return whether a callback was passed or not

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

@@ -132,3 +133,2 @@ return fs.writeFileSync(file, str, options)

var jsonfile = {
spaces: null,
readFile: readFile,

@@ -135,0 +135,0 @@ readFileSync: readFileSync,

{
"name": "jsonfile",
"version": "3.0.1",
"version": "4.0.0",
"description": "Easily read/write JSON files.",

@@ -26,5 +26,8 @@ "repository": {

"rimraf": "^2.4.0",
"standard": "^6.0.8"
"standard": "^10.0.3"
},
"main": "index.js",
"files": [
"index.js"
],
"scripts": {

@@ -31,0 +34,0 @@ "lint": "standard",

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

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

@@ -88,2 +88,15 @@

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

@@ -106,3 +119,3 @@

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

@@ -129,57 +142,26 @@ ```js

**appending to an existing JSON file:**
**overriding EOL:**
You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
```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
**appending to an existing JSON file:**
Global configuration to set spaces to indent JSON files.
You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
**default:** `null`
```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)
})
jsonfile.writeFileSync(file, obj, {flag: 'a'})
```
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
```
License

@@ -186,0 +168,0 @@ -------

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc