Skip to content

Commit 2c38bf4

Browse files
committed
Modernize code
1 parent 72b60b5 commit 2c38bf4

File tree

5 files changed

+259
-260
lines changed

5 files changed

+259
-260
lines changed

index.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _fs
1+
let _fs
22
try {
33
_fs = require('graceful-fs')
44
} catch (_) {
@@ -17,24 +17,24 @@ function readFileWithCallback (file, options, callback) {
1717
}
1818

1919
options = options || {}
20-
var fs = options.fs || _fs
20+
const fs = options.fs || _fs
2121

22-
var shouldThrow = true
22+
let shouldThrow = true
2323
if ('throws' in options) {
2424
shouldThrow = options.throws
2525
}
2626

27-
fs.readFile(file, options, function (err, data) {
27+
fs.readFile(file, options, (err, data) => {
2828
if (err) return callback(err)
2929

3030
data = stripBom(data)
3131

32-
var obj
32+
let obj
3333
try {
3434
obj = JSON.parse(data, options ? options.reviver : null)
3535
} catch (err2) {
3636
if (shouldThrow) {
37-
err2.message = file + ': ' + err2.message
37+
err2.message = `${file}: ${err2.message}`
3838
return callback(err2)
3939
} else {
4040
return callback(null, null)
@@ -53,20 +53,20 @@ function readFileSync (file, options) {
5353
options = { encoding: options }
5454
}
5555

56-
var fs = options.fs || _fs
56+
const fs = options.fs || _fs
5757

58-
var shouldThrow = true
58+
let shouldThrow = true
5959
if ('throws' in options) {
6060
shouldThrow = options.throws
6161
}
6262

6363
try {
64-
var content = fs.readFileSync(file, options)
64+
let content = fs.readFileSync(file, options)
6565
content = stripBom(content)
6666
return JSON.parse(content, options.reviver)
6767
} catch (err) {
6868
if (shouldThrow) {
69-
err.message = file + ': ' + err.message
69+
err.message = `${file}: ${err.message}`
7070
throw err
7171
} else {
7272
return null
@@ -75,8 +75,8 @@ function readFileSync (file, options) {
7575
}
7676

7777
function stringify (obj, options) {
78-
var spaces
79-
var EOL = '\n'
78+
let spaces
79+
let EOL = '\n'
8080
if (typeof options === 'object' && options !== null) {
8181
if (options.spaces) {
8282
spaces = options.spaces
@@ -86,7 +86,7 @@ function stringify (obj, options) {
8686
}
8787
}
8888

89-
var str = JSON.stringify(obj, options ? options.replacer : null, spaces)
89+
const str = JSON.stringify(obj, options ? options.replacer : null, spaces)
9090

9191
return str.replace(/\n/g, EOL) + EOL
9292
}
@@ -97,15 +97,13 @@ function writeFileWithCallback (file, obj, options, callback) {
9797
options = {}
9898
}
9999
options = options || {}
100-
var fs = options.fs || _fs
100+
const fs = options.fs || _fs
101101

102-
var str = ''
102+
let str = ''
103103
try {
104104
str = stringify(obj, options)
105105
} catch (err) {
106-
// Need to return whether a callback was passed or not
107-
if (callback) callback(err, null)
108-
return
106+
return callback(err, null)
109107
}
110108

111109
fs.writeFile(file, str, options, callback)
@@ -115,9 +113,9 @@ const writeFile = universalify.fromCallback(writeFileWithCallback)
115113

116114
function writeFileSync (file, obj, options) {
117115
options = options || {}
118-
var fs = options.fs || _fs
116+
const fs = options.fs || _fs
119117

120-
var str = stringify(obj, options)
118+
const str = stringify(obj, options)
121119
// not sure if fs.writeFileSync returns anything, but just in case
122120
return fs.writeFileSync(file, str, options)
123121
}
@@ -129,11 +127,11 @@ function stripBom (content) {
129127
return content
130128
}
131129

132-
var jsonfile = {
133-
readFile: readFile,
134-
readFileSync: readFileSync,
135-
writeFile: writeFile,
136-
writeFileSync: writeFileSync
130+
const jsonfile = {
131+
readFile,
132+
readFileSync,
133+
writeFile,
134+
writeFileSync
137135
}
138136

139137
module.exports = jsonfile

test/read-file-sync.test.js

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,148 @@
1-
var assert = require('assert')
2-
var fs = require('fs')
3-
var os = require('os')
4-
var path = require('path')
5-
var rimraf = require('rimraf')
6-
var jf = require('../')
1+
const assert = require('assert')
2+
const fs = require('fs')
3+
const os = require('os')
4+
const path = require('path')
5+
const rimraf = require('rimraf')
6+
const jf = require('../')
77

88
/* global describe it beforeEach afterEach */
99

10-
describe('+ readFileSync()', function () {
11-
var TEST_DIR
10+
describe('+ readFileSync()', () => {
11+
let TEST_DIR
1212

13-
beforeEach(function (done) {
13+
beforeEach((done) => {
1414
TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-readfile-sync')
1515
rimraf.sync(TEST_DIR)
1616
fs.mkdir(TEST_DIR, done)
1717
})
1818

19-
afterEach(function (done) {
19+
afterEach((done) => {
2020
rimraf.sync(TEST_DIR)
2121
done()
2222
})
2323

24-
it('should read and parse JSON', function () {
25-
var file = path.join(TEST_DIR, 'somefile3.json')
26-
var obj = { name: 'JP' }
24+
it('should read and parse JSON', () => {
25+
const file = path.join(TEST_DIR, 'somefile3.json')
26+
const obj = { name: 'JP' }
2727
fs.writeFileSync(file, JSON.stringify(obj))
2828

2929
try {
30-
var obj2 = jf.readFileSync(file)
30+
const obj2 = jf.readFileSync(file)
3131
assert.strictEqual(obj2.name, obj.name)
3232
} catch (err) {
3333
assert(err)
3434
}
3535
})
3636

37-
describe('> when invalid JSON', function () {
38-
it('should include the filename in the error', function () {
39-
var fn = 'somefile.json'
40-
var file = path.join(TEST_DIR, fn)
37+
describe('> when invalid JSON', () => {
38+
it('should include the filename in the error', () => {
39+
const fn = 'somefile.json'
40+
const file = path.join(TEST_DIR, fn)
4141
fs.writeFileSync(file, '{')
4242

43-
assert.throws(function () {
43+
assert.throws(() => {
4444
jf.readFileSync(file)
45-
}, function (err) {
45+
}, (err) => {
4646
assert(err instanceof Error)
4747
assert(err.message.match(fn))
4848
return true
4949
})
5050
})
5151
})
5252

53-
describe('> when invalid JSON and throws set to false', function () {
54-
it('should return null', function () {
55-
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
56-
var data = '{not valid JSON'
53+
describe('> when invalid JSON and throws set to false', () => {
54+
it('should return null', () => {
55+
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
56+
const data = '{not valid JSON'
5757
fs.writeFileSync(file, data)
5858

59-
assert.throws(function () {
59+
assert.throws(() => {
6060
jf.readFileSync(file)
6161
})
6262

63-
var obj = jf.readFileSync(file, { throws: false })
63+
const obj = jf.readFileSync(file, { throws: false })
6464
assert.strictEqual(obj, null)
6565
})
6666
})
6767

68-
describe('> when invalid JSON and throws set to true', function () {
69-
it('should throw an exception', function () {
70-
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
71-
var data = '{not valid JSON'
68+
describe('> when invalid JSON and throws set to true', () => {
69+
it('should throw an exception', () => {
70+
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
71+
const data = '{not valid JSON'
7272
fs.writeFileSync(file, data)
7373

74-
assert.throws(function () {
74+
assert.throws(() => {
7575
jf.readFileSync(file, { throws: true })
7676
})
7777
})
7878
})
7979

80-
describe('> when json file is missing and throws set to false', function () {
81-
it('should return null', function () {
82-
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
80+
describe('> when json file is missing and throws set to false', () => {
81+
it('should return null', () => {
82+
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
8383

84-
var obj = jf.readFileSync(file, { throws: false })
84+
const obj = jf.readFileSync(file, { throws: false })
8585
assert.strictEqual(obj, null)
8686
})
8787
})
8888

89-
describe('> when json file is missing and throws set to true', function () {
90-
it('should throw an exception', function () {
91-
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
89+
describe('> when json file is missing and throws set to true', () => {
90+
it('should throw an exception', () => {
91+
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
9292

93-
assert.throws(function () {
93+
assert.throws(() => {
9494
jf.readFileSync(file, { throws: true })
9595
})
9696
})
9797
})
9898

99-
describe('> when JSON reviver is set', function () {
100-
it('should transform the JSON', function () {
101-
var file = path.join(TEST_DIR, 'somefile.json')
102-
var sillyReviver = function (k, v) {
99+
describe('> when JSON reviver is set', () => {
100+
it('should transform the JSON', () => {
101+
const file = path.join(TEST_DIR, 'somefile.json')
102+
const sillyReviver = function (k, v) {
103103
if (typeof v !== 'string') return v
104104
if (v.indexOf('date:') < 0) return v
105105
return new Date(v.split('date:')[1])
106106
}
107107

108-
var obj = {
108+
const obj = {
109109
name: 'jp',
110110
day: 'date:2015-06-19T11:41:26.815Z'
111111
}
112112

113113
fs.writeFileSync(file, JSON.stringify(obj))
114-
var data = jf.readFileSync(file, { reviver: sillyReviver })
114+
const data = jf.readFileSync(file, { reviver: sillyReviver })
115115
assert.strictEqual(data.name, 'jp')
116116
assert(data.day instanceof Date)
117117
assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z')
118118
})
119119
})
120120

121-
describe('> when passing encoding string as option', function () {
122-
it('should not throw an error', function () {
123-
var file = path.join(TEST_DIR, 'somefile.json')
121+
describe('> when passing encoding string as option', () => {
122+
it('should not throw an error', () => {
123+
const file = path.join(TEST_DIR, 'somefile.json')
124124

125-
var obj = {
125+
const obj = {
126126
name: 'jp'
127127
}
128128
fs.writeFileSync(file, JSON.stringify(obj))
129129

130+
let data
130131
try {
131-
var data = jf.readFileSync(file, 'utf8')
132+
data = jf.readFileSync(file, 'utf8')
132133
} catch (err) {
133134
assert.ifError(err)
134135
}
135136
assert.strictEqual(data.name, 'jp')
136137
})
137138
})
138139

139-
describe('> w/ BOM', function () {
140-
it('should properly parse', function () {
141-
var file = path.join(TEST_DIR, 'file-bom.json')
142-
var obj = { name: 'JP' }
143-
fs.writeFileSync(file, '\uFEFF' + JSON.stringify(obj))
144-
var data = jf.readFileSync(file)
140+
describe('> w/ BOM', () => {
141+
it('should properly parse', () => {
142+
const file = path.join(TEST_DIR, 'file-bom.json')
143+
const obj = { name: 'JP' }
144+
fs.writeFileSync(file, `\uFEFF${JSON.stringify(obj)}`)
145+
const data = jf.readFileSync(file)
145146
assert.deepStrictEqual(obj, data)
146147
})
147148
})

0 commit comments

Comments
 (0)