Skip to content

Commit e4d8e67

Browse files
committed
jsonfile: added options.spaces
1 parent ecfc7aa commit e4d8e67

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

README.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ console.dir(jf.readFileSync(file))
5656

5757
### writeFile(filename, [options], callback)
5858

59-
`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).
59+
`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`.
6060

6161

6262
```js
@@ -70,10 +70,23 @@ jf.writeFile(file, obj, function(err) {
7070
})
7171
```
7272

73+
**formatting with spaces:**
74+
75+
```js
76+
var jf = require('jsonfile')
77+
78+
var file = '/tmp/data.json'
79+
var obj = {name: 'JP'}
80+
81+
jf.writeFile(file, obj, {spaces: 2}, function(err) {
82+
console.log(err)
83+
})
84+
```
85+
7386

7487
### writeFileSync(filename, [options])
7588

76-
`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).
89+
`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`.
7790

7891
```js
7992
var jf = require('jsonfile')
@@ -84,10 +97,22 @@ var obj = {name: 'JP'}
8497
jf.writeFileSync(file, obj)
8598
```
8699

100+
**formatting with spaces:**
101+
102+
```js
103+
var jf = require('jsonfile')
104+
105+
var file = '/tmp/data.json'
106+
var obj = {name: 'JP'}
107+
108+
jf.writeFileSync(file, obj, {spaces: 2})
109+
```
110+
111+
87112

88113
### spaces
89114

90-
Number of spaces to indent JSON files.
115+
Global configuration to set spaces to indent JSON files.
91116

92117
**default:** `null`
93118

@@ -104,6 +129,28 @@ jf.writeFile(file, obj, function(err) { //json file has four space indenting now
104129
})
105130
```
106131

132+
Note, it's bound to `this.spaces`. So, if you do this:
133+
134+
```js
135+
var myObj = {}
136+
myObj.writeJsonSync = jf.writeFileSync
137+
// => this.spaces = null
138+
```
139+
140+
Could do the following:
141+
142+
```js
143+
var jf = require('jsonfile')
144+
jf.spaces = 4
145+
jf.writeFileSync(file, obj) // will have 4 spaces indentation
146+
147+
var myCrazyObj = {spaces: 32}
148+
myCrazyObj.writeJsonSync = jf.writeFileSync
149+
myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
150+
myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
151+
```
152+
153+
107154

108155
Contributions
109156
-------------

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ function readFileSync (file, options) {
4242
function writeFile (file, obj, options, callback) {
4343
if (callback == null) {
4444
callback = options
45-
options = {}
45+
options = options || {}
4646
}
4747

48+
var spaces = options
49+
? 'spaces' in options
50+
? options.spaces : this.spaces
51+
: this.spaces
52+
4853
var str = ''
4954
try {
50-
str = JSON.stringify(obj, options ? options.replacer : null, this.spaces) + '\n'
55+
str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
5156
} catch (err) {
5257
if (callback) return callback(err, null)
5358
}
@@ -57,7 +62,8 @@ function writeFile (file, obj, options, callback) {
5762

5863
function writeFileSync (file, obj, options) {
5964
options = options || {}
60-
var str = JSON.stringify(obj, options.replacer, this.spaces) + '\n'
65+
var spaces = 'spaces' in options ? options.spaces : this.spaces
66+
var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
6167
// not sure if fs.writeFileSync returns anything, but just in case
6268
return fs.writeFileSync(file, str, options)
6369
}

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ describe('jsonfile', function () {
238238
})
239239
})
240240
})
241+
242+
describe('> when spaces passed as an option', function () {
243+
it('should write file with spaces', function (done) {
244+
var file = path.join(TEST_DIR, 'somefile.json')
245+
var obj = { name: 'jp' }
246+
jf.writeFile(file, obj, {spaces: 8}, function (err) {
247+
assert.ifError(err)
248+
var data = fs.readFileSync(file, 'utf8')
249+
assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
250+
done()
251+
})
252+
})
253+
})
241254
})
242255

243256
describe('+ writeFileSync()', function () {
@@ -289,6 +302,16 @@ describe('jsonfile', function () {
289302
assert.strictEqual(data.reg, 'regex:/hello/g')
290303
})
291304
})
305+
306+
describe('> when spaces passed as an option', function () {
307+
it('should write file with spaces', function () {
308+
var file = path.join(TEST_DIR, 'somefile.json')
309+
var obj = { name: 'JP' }
310+
jf.writeFileSync(file, obj, {spaces: 8})
311+
var data = fs.readFileSync(file, 'utf8')
312+
assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n')
313+
})
314+
})
292315
})
293316

294317
describe('spaces', function () {

0 commit comments

Comments
 (0)