Skip to content

Commit 782f185

Browse files
committed
Remove global spaces option
1 parent bad7587 commit 782f185

File tree

6 files changed

+2
-140
lines changed

6 files changed

+2
-140
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- **BREAKING:** Remove global `spaces` option.
12
- Added `EOL` override option to `writeFile` when using `spaces`. [#89]
23

34
3.0.1 / 2017-07-05

README.md

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -160,48 +160,6 @@ var obj = {name: 'JP'}
160160
jsonfile.writeFileSync(file, obj, {flag: 'a'})
161161
```
162162

163-
### spaces
164-
165-
Global configuration to set spaces to indent JSON files.
166-
167-
**default:** `null`
168-
169-
```js
170-
var jsonfile = require('jsonfile')
171-
172-
jsonfile.spaces = 4
173-
174-
var file = '/tmp/data.json'
175-
var obj = {name: 'JP'}
176-
177-
// json file has four space indenting now
178-
jsonfile.writeFile(file, obj, function (err) {
179-
console.error(err)
180-
})
181-
```
182-
183-
Note, it's bound to `this.spaces`. So, if you do this:
184-
185-
```js
186-
var myObj = {}
187-
myObj.writeJsonSync = jsonfile.writeFileSync
188-
// => this.spaces = null
189-
```
190-
191-
Could do the following:
192-
193-
```js
194-
var jsonfile = require('jsonfile')
195-
jsonfile.spaces = 4
196-
jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
197-
198-
var myCrazyObj = {spaces: 32}
199-
myCrazyObj.writeJsonSync = jsonfile.writeFileSync
200-
myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
201-
myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
202-
```
203-
204-
205163
License
206164
-------
207165

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ function readFileSync (file, options) {
7878
}
7979

8080
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
81+
var spaces
8582
var EOL = '\n'
8683
if (typeof options === 'object' && options !== null) {
8784
if (options.spaces) {
@@ -134,7 +131,6 @@ function stripBom (content) {
134131
}
135132

136133
var jsonfile = {
137-
spaces: null,
138134
readFile: readFile,
139135
readFileSync: readFileSync,
140136
writeFile: writeFile,

test/test.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/write-file-sync.test.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,6 @@ describe('+ writeFileSync()', function () {
3434
assert.equal(data, '{"name":"JP"}\n')
3535
})
3636

37-
describe('> when global spaces is set', function () {
38-
it('should write JSON with spacing', function () {
39-
var file = path.join(TEST_DIR, 'somefile.json')
40-
var obj = {name: 'JP'}
41-
jf.spaces = 2
42-
jf.writeFileSync(file, obj)
43-
44-
var data = fs.readFileSync(file, 'utf8')
45-
assert.equal(data, '{\n "name": "JP"\n}\n')
46-
47-
// restore default
48-
jf.spaces = null
49-
})
50-
51-
it('still uses global when context lost', function () {
52-
var file = path.join(TEST_DIR, 'somefile.json')
53-
var obj = {name: 'JP'}
54-
jf.spaces = 2
55-
var writeFileSync = jf.writeFileSync
56-
writeFileSync(file, obj)
57-
58-
var data = fs.readFileSync(file, 'utf8')
59-
assert.equal(data, '{\n "name": "JP"\n}\n')
60-
61-
// restore default
62-
jf.spaces = null
63-
})
64-
})
65-
6637
describe('> when JSON replacer is set', function () {
6738
it('should replace JSON', function () {
6839
var file = path.join(TEST_DIR, 'somefile.json')

test/write-file.test.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,6 @@ describe('+ writeFile()', function () {
3939
})
4040
})
4141

42-
describe('> when global spaces is set', function () {
43-
it('should write JSON with spacing', function (done) {
44-
var file = path.join(TEST_DIR, 'somefile.json')
45-
var obj = {name: 'JP'}
46-
jf.spaces = 2
47-
jf.writeFile(file, obj, function (err) {
48-
assert.ifError(err)
49-
50-
var data = fs.readFileSync(file, 'utf8')
51-
assert.equal(data, '{\n "name": "JP"\n}\n')
52-
53-
// restore default
54-
jf.spaces = null
55-
done()
56-
})
57-
})
58-
59-
it('still uses global when context lost', function (done) {
60-
var file = path.join(TEST_DIR, 'somefile.json')
61-
var obj = {name: 'JP'}
62-
jf.spaces = 2
63-
var writeFile = jf.writeFile
64-
writeFile(file, obj, function (err) {
65-
assert.ifError(err)
66-
67-
var data = fs.readFileSync(file, 'utf8')
68-
assert.equal(data, '{\n "name": "JP"\n}\n')
69-
70-
// restore default
71-
jf.spaces = null
72-
done()
73-
})
74-
})
75-
})
76-
7742
describe('> when JSON replacer is set', function () {
7843
it('should replace JSON', function (done) {
7944
var file = path.join(TEST_DIR, 'somefile.json')

0 commit comments

Comments
 (0)