Skip to content

Commit e3d86e0

Browse files
authored
Major refactor (#128)
* BREAKING: Drop Node 6 & 8 * Upgrade universalify * Refactor writeFileSync() * BREAKING: Refactor writeFile(); do not allow passing null options * Refactor readFileSync() * BREAKING: Refactor readFile(); do not allow passing null options * Refactor internal functions and move them to a seperate file
1 parent 7b53118 commit e3d86e0

File tree

7 files changed

+44
-145
lines changed

7 files changed

+44
-145
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "6"
5-
- "8"
64
- "10"
75
- "12"
86
matrix:

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
environment:
33
matrix:
44
# node.js
5-
- nodejs_version: "6"
6-
- nodejs_version: "8"
75
- nodejs_version: "10"
86
- nodejs_version: "12"
97

index.js

Lines changed: 26 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,46 @@ try {
55
_fs = require('fs')
66
}
77
const universalify = require('universalify')
8+
const { stringify, stripBom } = require('./utils')
89

9-
function readFileWithCallback (file, options, callback) {
10-
if (callback == null) {
11-
callback = options
12-
options = {}
13-
}
14-
10+
async function _readFile (file, options = {}) {
1511
if (typeof options === 'string') {
1612
options = { encoding: options }
1713
}
1814

19-
options = options || {}
2015
const fs = options.fs || _fs
2116

22-
let shouldThrow = true
23-
if ('throws' in options) {
24-
shouldThrow = options.throws
25-
}
17+
const shouldThrow = 'throws' in options ? options.throws : true
18+
19+
let data = await universalify.fromCallback(fs.readFile)(file, options)
2620

27-
fs.readFile(file, options, (err, data) => {
28-
if (err) return callback(err)
29-
30-
data = stripBom(data)
31-
32-
let obj
33-
try {
34-
obj = JSON.parse(data, options ? options.reviver : null)
35-
} catch (err2) {
36-
if (shouldThrow) {
37-
err2.message = `${file}: ${err2.message}`
38-
return callback(err2)
39-
} else {
40-
return callback(null, null)
41-
}
21+
data = stripBom(data)
22+
23+
let obj
24+
try {
25+
obj = JSON.parse(data, options ? options.reviver : null)
26+
} catch (err) {
27+
if (shouldThrow) {
28+
err.message = `${file}: ${err.message}`
29+
throw err
30+
} else {
31+
return null
4232
}
33+
}
4334

44-
callback(null, obj)
45-
})
35+
return obj
4636
}
4737

48-
const readFile = universalify.fromCallback(readFileWithCallback)
38+
const readFile = universalify.fromPromise(_readFile)
4939

50-
function readFileSync (file, options) {
51-
options = options || {}
40+
function readFileSync (file, options = {}) {
5241
if (typeof options === 'string') {
5342
options = { encoding: options }
5443
}
5544

5645
const fs = options.fs || _fs
5746

58-
let shouldThrow = true
59-
if ('throws' in options) {
60-
shouldThrow = options.throws
61-
}
47+
const shouldThrow = 'throws' in options ? options.throws : true
6248

6349
try {
6450
let content = fs.readFileSync(file, options)
@@ -74,59 +60,24 @@ function readFileSync (file, options) {
7460
}
7561
}
7662

77-
function stringify (obj, options) {
78-
let spaces
79-
let EOL = '\n'
80-
if (typeof options === 'object' && options !== null) {
81-
if (options.spaces) {
82-
spaces = options.spaces
83-
}
84-
if (options.EOL) {
85-
EOL = options.EOL
86-
}
87-
}
88-
89-
const str = JSON.stringify(obj, options ? options.replacer : null, spaces)
90-
91-
return str.replace(/\n/g, EOL) + EOL
92-
}
93-
94-
function writeFileWithCallback (file, obj, options, callback) {
95-
if (callback == null) {
96-
callback = options
97-
options = {}
98-
}
99-
options = options || {}
63+
async function _writeFile (file, obj, options = {}) {
10064
const fs = options.fs || _fs
10165

102-
let str = ''
103-
try {
104-
str = stringify(obj, options)
105-
} catch (err) {
106-
return callback(err, null)
107-
}
66+
const str = stringify(obj, options)
10867

109-
fs.writeFile(file, str, options, callback)
68+
await universalify.fromCallback(fs.writeFile)(file, str, options)
11069
}
11170

112-
const writeFile = universalify.fromCallback(writeFileWithCallback)
71+
const writeFile = universalify.fromPromise(_writeFile)
11372

114-
function writeFileSync (file, obj, options) {
115-
options = options || {}
73+
function writeFileSync (file, obj, options = {}) {
11674
const fs = options.fs || _fs
11775

11876
const str = stringify(obj, options)
11977
// not sure if fs.writeFileSync returns anything, but just in case
12078
return fs.writeFileSync(file, str, options)
12179
}
12280

123-
function stripBom (content) {
124-
// we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
125-
if (Buffer.isBuffer(content)) content = content.toString('utf8')
126-
content = content.replace(/^\uFEFF/, '')
127-
return content
128-
}
129-
13081
const jsonfile = {
13182
readFile,
13283
readFileSync,

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "JP Richardson <[email protected]>",
1818
"license": "MIT",
1919
"dependencies": {
20-
"universalify": "^0.1.2"
20+
"universalify": "^0.2.0"
2121
},
2222
"optionalDependencies": {
2323
"graceful-fs": "^4.1.6"
@@ -29,7 +29,8 @@
2929
},
3030
"main": "index.js",
3131
"files": [
32-
"index.js"
32+
"index.js",
33+
"utils.js"
3334
],
3435
"scripts": {
3536
"lint": "standard",

test/read-file.test.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -206,44 +206,6 @@ describe('+ readFile()', () => {
206206
})
207207
})
208208

209-
describe('> when passing null as options and callback', () => {
210-
it('should not throw an error', (done) => {
211-
const file = path.join(TEST_DIR, 'somefile.json')
212-
213-
const obj = {
214-
name: 'jp'
215-
}
216-
fs.writeFileSync(file, JSON.stringify(obj))
217-
218-
jf.readFile(file, null, (err) => {
219-
assert.ifError(err)
220-
assert.strictEqual(obj.name, 'jp')
221-
done()
222-
})
223-
})
224-
})
225-
226-
describe('> when passing null as options and expecting a promise', () => {
227-
it('should resolve the promise', (done) => {
228-
const file = path.join(TEST_DIR, 'somefile.json')
229-
230-
const obj = {
231-
name: 'jp'
232-
}
233-
fs.writeFileSync(file, JSON.stringify(obj))
234-
235-
jf.readFile(file, null)
236-
.then(data => {
237-
assert.strictEqual(data.name, obj.name)
238-
done()
239-
})
240-
.catch(err => {
241-
assert.ifError(err)
242-
done()
243-
})
244-
})
245-
})
246-
247209
describe('> when passing encoding string as option', () => {
248210
let file, obj
249211

test/write-file.test.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,6 @@ describe('+ writeFile()', () => {
107107
})
108108
})
109109

110-
describe('> when passing null as options and callback', () => {
111-
it('should not throw an error', (done) => {
112-
const file = path.join(TEST_DIR, 'somefile.json')
113-
const obj = { name: 'jp' }
114-
jf.writeFile(file, obj, null, (err) => {
115-
assert.ifError(err)
116-
done()
117-
})
118-
})
119-
})
120-
121-
describe('> when passing null as options and No callback', () => {
122-
it('should not throw an error', (done) => {
123-
const file = path.join(TEST_DIR, 'somefile.json')
124-
const obj = { name: 'jp' }
125-
jf.writeFile(file, obj, null)
126-
.then(res => {
127-
done()
128-
})
129-
.catch(err => {
130-
assert.ifError(err)
131-
done()
132-
})
133-
})
134-
})
135-
136110
describe('> when spaces passed as an option', () => {
137111
let file, obj
138112
beforeEach((done) => {

utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function stringify (obj, options = {}) {
2+
let EOL = options.EOL || '\n'
3+
4+
const str = JSON.stringify(obj, options ? options.replacer : null, options.spaces)
5+
6+
return str.replace(/\n/g, EOL) + EOL
7+
}
8+
9+
function stripBom (content) {
10+
// we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
11+
if (Buffer.isBuffer(content)) content = content.toString('utf8')
12+
return content.replace(/^\uFEFF/, '')
13+
}
14+
15+
module.exports = { stringify, stripBom }

0 commit comments

Comments
 (0)