Skip to content

Commit d9f13ae

Browse files
author
Kelly Selden
committed
allow overriding the EOL string
1 parent bc44e79 commit d9f13ae

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ console.dir(jsonfile.readFileSync(file))
5757

5858
### writeFile(filename, obj, [options], callback)
5959

60-
`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`.
60+
`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.
6161

6262

6363
```js
@@ -84,6 +84,19 @@ jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
8484
})
8585
```
8686

87+
**overriding EOL:**
88+
89+
```js
90+
var jsonfile = require('jsonfile')
91+
92+
var file = '/tmp/data.json'
93+
var obj = {name: 'JP'}
94+
95+
jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
96+
console.error(err)
97+
})
98+
```
99+
87100
**appending to an existing JSON file:**
88101

89102
You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
@@ -101,7 +114,7 @@ jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
101114

102115
### writeFileSync(filename, obj, [options])
103116

104-
`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`.
117+
`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.
105118

106119
```js
107120
var jsonfile = require('jsonfile')
@@ -123,6 +136,17 @@ var obj = {name: 'JP'}
123136
jsonfile.writeFileSync(file, obj, {spaces: 2})
124137
```
125138

139+
**overriding EOL:**
140+
141+
```js
142+
var jsonfile = require('jsonfile')
143+
144+
var file = '/tmp/data.json'
145+
var obj = {name: 'JP'}
146+
147+
jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
148+
```
149+
126150
**appending to an existing JSON file:**
127151

128152
You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.

index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ function readFileSync (file, options) {
7777
}
7878
}
7979

80+
function stringify (obj, options) {
81+
// `jsonfile` and not `this` because people might destructure
82+
// and use `writeFile` instead of `fs.writeFile`, in which case
83+
// `this` would be `undefined`
84+
var spaces = jsonfile.spaces
85+
var EOL = '\n'
86+
if (typeof options === 'object' && options !== null) {
87+
if (options.spaces) {
88+
spaces = options.spaces
89+
}
90+
if (options.EOL) {
91+
EOL = options.EOL
92+
}
93+
}
94+
95+
var str = JSON.stringify(obj, options ? options.replacer : null, spaces)
96+
97+
return str.replace(/\n/g, EOL) + EOL
98+
}
99+
80100
function writeFile (file, obj, options, callback) {
81101
if (callback == null) {
82102
callback = options
@@ -85,14 +105,9 @@ function writeFile (file, obj, options, callback) {
85105
options = options || {}
86106
var fs = options.fs || _fs
87107

88-
var spaces = typeof options === 'object' && options !== null
89-
? 'spaces' in options
90-
? options.spaces : this.spaces
91-
: this.spaces
92-
93108
var str = ''
94109
try {
95-
str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
110+
str = stringify(obj, options)
96111
} catch (err) {
97112
// Need to return whether a callback was passed or not
98113
if (callback) callback(err, null)
@@ -106,12 +121,7 @@ function writeFileSync (file, obj, options) {
106121
options = options || {}
107122
var fs = options.fs || _fs
108123

109-
var spaces = typeof options === 'object' && options !== null
110-
? 'spaces' in options
111-
? options.spaces : this.spaces
112-
: this.spaces
113-
114-
var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
124+
var str = stringify(obj, options)
115125
// not sure if fs.writeFileSync returns anything, but just in case
116126
return fs.writeFileSync(file, str, options)
117127
}

test/write-file-sync.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ describe('+ writeFileSync()', function () {
7878
var data = fs.readFileSync(file, 'utf8')
7979
assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
8080
})
81+
82+
it('should use EOL override', function () {
83+
var file = path.join(TEST_DIR, 'somefile.json')
84+
var obj = { name: 'JP' }
85+
jf.writeFileSync(file, obj, {spaces: 2, EOL: '***'})
86+
var data = fs.readFileSync(file, 'utf8')
87+
assert.strictEqual(data, '{*** "name": "JP"***}***')
88+
})
8189
})
8290

8391
describe('> when passing encoding string as options', function () {

test/write-file.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ describe('+ writeFile()', function () {
104104
done()
105105
})
106106
})
107+
108+
it('should use EOL override', function (done) {
109+
var file = path.join(TEST_DIR, 'somefile.json')
110+
var obj = { name: 'jp' }
111+
jf.writeFile(file, obj, {spaces: 2, EOL: '***'}, function (err) {
112+
assert.ifError(err)
113+
var data = fs.readFileSync(file, 'utf8')
114+
assert.strictEqual(data, '{*** "name": "jp"***}***')
115+
done()
116+
})
117+
})
107118
})
108119

109120
describe('> when passing encoding string as options', function () {

0 commit comments

Comments
 (0)