Skip to content

Commit 5187bf3

Browse files
authored
Add option to override final EOL in file (#137)
1 parent 78937d2 commit 5187bf3

File tree

5 files changed

+108
-7
lines changed

5 files changed

+108
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules/
22
/.project
3+
.vscode/
4+

README.md

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

7878
### writeFile(filename, obj, [options], callback)
7979

80-
`options`: Pass in any [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback) 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.
80+
`options`: Pass in any [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback) 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`, or override `EOL` string or set `finalEOL` flag as `false` to not save the file with `EOL` at the end.
8181

8282

8383
```js
@@ -132,6 +132,20 @@ jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
132132
})
133133
```
134134

135+
136+
**disabling the EOL at the end of file:**
137+
138+
```js
139+
const jsonfile = require('jsonfile')
140+
141+
const file = '/tmp/data.json'
142+
const obj = { name: 'JP' }
143+
144+
jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
145+
if (err) console.log(err)
146+
})
147+
```
148+
135149
**appending to an existing JSON file:**
136150

137151
You can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.
@@ -151,7 +165,7 @@ jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
151165

152166
### writeFileSync(filename, obj, [options])
153167

154-
`options`: Pass in any [`fs.writeFileSync`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options) 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.
168+
`options`: Pass in any [`fs.writeFileSync`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options) 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`, or override `EOL` string or set `finalEOL` flag as `false` to not save the file with `EOL` at the end.
155169

156170
```js
157171
const jsonfile = require('jsonfile')
@@ -184,6 +198,17 @@ const obj = { name: 'JP' }
184198
jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
185199
```
186200

201+
**disabling the EOL at the end of file:**
202+
203+
```js
204+
const jsonfile = require('jsonfile')
205+
206+
const file = '/tmp/data.json'
207+
const obj = { name: 'JP' }
208+
209+
jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })
210+
```
211+
187212
**appending to an existing JSON file:**
188213

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

test/write-file-sync.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,38 @@ describe('+ writeFileSync()', () => {
8282
assert.strictEqual(data, `${JSON.stringify(obj)}\n`)
8383
})
8484
})
85+
describe('> when EOF option is set to a falsey value', () => {
86+
beforeEach((done) => {
87+
TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-writefile-sync')
88+
rimraf.sync(TEST_DIR)
89+
fs.mkdir(TEST_DIR, done)
90+
})
91+
92+
afterEach((done) => {
93+
rimraf.sync(TEST_DIR)
94+
done()
95+
})
96+
97+
it('should not have a the EOL symbol at the end of file', (done) => {
98+
const file = path.join(TEST_DIR, 'somefile2.json')
99+
const obj = { name: 'jp' }
100+
jf.writeFileSync(file, obj, { finalEOL: false })
101+
const rawData = fs.readFileSync(file, 'utf8')
102+
const data = JSON.parse(rawData)
103+
assert.strictEqual(rawData[rawData.length - 1], '}')
104+
assert.strictEqual(data.name, obj.name)
105+
done()
106+
})
107+
108+
it('should have a the EOL symbol at the end of file when finalEOL is a truth value in options', (done) => {
109+
const file = path.join(TEST_DIR, 'somefile2.json')
110+
const obj = { name: 'jp' }
111+
jf.writeFileSync(file, obj, { finalEOL: true })
112+
const rawData = fs.readFileSync(file, 'utf8')
113+
const data = JSON.parse(rawData)
114+
assert.strictEqual(rawData[rawData.length - 1], '\n')
115+
assert.strictEqual(data.name, obj.name)
116+
done()
117+
})
118+
})
85119
})

test/write-file.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,47 @@ describe('+ writeFile()', () => {
199199
})
200200
})
201201

202+
describe('> when EOF option is set to a falsey value', () => {
203+
beforeEach((done) => {
204+
TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-writefile')
205+
rimraf.sync(TEST_DIR)
206+
fs.mkdir(TEST_DIR, done)
207+
})
208+
209+
afterEach((done) => {
210+
rimraf.sync(TEST_DIR)
211+
done()
212+
})
213+
214+
it('should not have a the EOL symbol at the end of file', (done) => {
215+
const file = path.join(TEST_DIR, 'somefile2.json')
216+
const obj = { name: 'jp' }
217+
jf.writeFile(file, obj, { finalEOL: false }, (err) => {
218+
assert.ifError(err)
219+
fs.readFile(file, 'utf8', (_, rawData) => {
220+
const data = JSON.parse(rawData)
221+
assert.strictEqual(rawData[rawData.length - 1], '}')
222+
assert.strictEqual(data.name, obj.name)
223+
done()
224+
})
225+
})
226+
})
227+
228+
it('should have a the EOL symbol at the end of file when finalEOL is a truth value in options', (done) => {
229+
const file = path.join(TEST_DIR, 'somefile2.json')
230+
const obj = { name: 'jp' }
231+
jf.writeFile(file, obj, { finalEOL: true }, (err) => {
232+
assert.ifError(err)
233+
fs.readFile(file, 'utf8', (_, rawData) => {
234+
const data = JSON.parse(rawData)
235+
assert.strictEqual(rawData[rawData.length - 1], '\n')
236+
assert.strictEqual(data.name, obj.name)
237+
done()
238+
})
239+
})
240+
})
241+
})
242+
202243
// Prevent https://github.com/jprichardson/node-jsonfile/issues/81 from happening
203244
describe("> when callback isn't passed & can't serialize", () => {
204245
it('should not write an empty file, should reject the promise', function (done) {

utils.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
function stringify (obj, options = {}) {
2-
const EOL = options.EOL || '\n'
1+
function stringify (obj, { EOL = '\n', finalEOL = true, replacer = null, spaces } = {}) {
2+
const EOF = finalEOL ? EOL : ''
3+
const str = JSON.stringify(obj, replacer, spaces)
34

4-
const str = JSON.stringify(obj, options ? options.replacer : null, options.spaces)
5-
6-
return str.replace(/\n/g, EOL) + EOL
5+
return str.replace(/\n/g, EOL) + EOF
76
}
87

98
function stripBom (content) {

0 commit comments

Comments
 (0)