You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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`.
`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`.
77
90
78
91
```js
79
92
var jf =require('jsonfile')
@@ -84,10 +97,22 @@ var obj = {name: 'JP'}
84
97
jf.writeFileSync(file, obj)
85
98
```
86
99
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
+
87
112
88
113
### spaces
89
114
90
-
Number of spaces to indent JSON files.
115
+
Global configuration to set spaces to indent JSON files.
91
116
92
117
**default:**`null`
93
118
@@ -104,6 +129,28 @@ jf.writeFile(file, obj, function(err) { //json file has four space indenting now
104
129
})
105
130
```
106
131
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
0 commit comments