Skip to content

Commit 94e5b5b

Browse files
authored
Merge pull request #110 from jprichardson/develop
Developments
2 parents 86f924b + 3bd9181 commit 94e5b5b

File tree

9 files changed

+330
-329
lines changed

9 files changed

+330
-329
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "4"
54
- "6"
65
- "8"
6+
- "10"
77
matrix:
88
include:
9-
- node_js: "8"
9+
- node_js: "10"
1010
env: TEST_SUITE=lint
1111
env:
1212
- TEST_SUITE=unit

README.md

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@ API
3434

3535

3636
```js
37-
var jsonfile = require('jsonfile')
38-
var file = '/tmp/data.json'
39-
jsonfile.readFile(file, function(err, obj) {
37+
const jsonfile = require('jsonfile')
38+
const file = '/tmp/data.json'
39+
jsonfile.readFile(file, function (err, obj) {
40+
if (err) console.error(err)
4041
console.dir(obj)
4142
})
4243
```
4344

44-
You can also use this method with promises. The readFile() method will return a promise if you do not pass a callback function.
45+
You can also use this method with promises. The `readFile` method will return a promise if you do not pass a callback function.
4546

4647
```js
47-
var jsonfile = require('jsonfile')
48-
var file = '/tmp/data.json'
48+
const jsonfile = require('jsonfile')
49+
const file = '/tmp/data.json'
4950
jsonfile.readFile(file)
50-
.then(obj => console.dir(obj)))
51-
.catch(error => console.log(error));
51+
.then(obj => console.dir(obj))
52+
.catch(error => console.error(error))
5253
```
5354

5455
### readFileSync(filename, [options])
@@ -57,8 +58,8 @@ jsonfile.readFile(file)
5758
- `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
5859

5960
```js
60-
var jsonfile = require('jsonfile')
61-
var file = '/tmp/data.json'
61+
const jsonfile = require('jsonfile')
62+
const file = '/tmp/data.json'
6263

6364
console.dir(jsonfile.readFileSync(file))
6465
```
@@ -70,69 +71,69 @@ console.dir(jsonfile.readFileSync(file))
7071

7172

7273
```js
73-
var jsonfile = require('jsonfile')
74+
const jsonfile = require('jsonfile')
7475

75-
var file = '/tmp/data.json'
76-
var obj = {name: 'JP'}
76+
const file = '/tmp/data.json'
77+
const obj = { name: 'JP' }
7778

7879
jsonfile.writeFile(file, obj, function (err) {
79-
console.error(err)
80+
if (err) console.error(err)
8081
})
8182
```
8283
Or use with promises as follows:
8384

8485
```js
85-
var jsonfile = require('jsonfile')
86+
const jsonfile = require('jsonfile')
8687

87-
var file = '/tmp/data.json'
88-
var obj = {name: 'JP'}
88+
const file = '/tmp/data.json'
89+
const obj = { name: 'JP' }
8990

9091
jsonfile.writeFile(file, obj)
9192
.then(res => {
92-
console.log("Write complete");
93+
console.log('Write complete')
9394
})
94-
.catch(error => console.log(error));
95+
.catch(error => console.error(error))
9596
```
9697

9798

9899
**formatting with spaces:**
99100

100101
```js
101-
var jsonfile = require('jsonfile')
102+
const jsonfile = require('jsonfile')
102103

103-
var file = '/tmp/data.json'
104-
var obj = {name: 'JP'}
104+
const file = '/tmp/data.json'
105+
const obj = { name: 'JP' }
105106

106-
jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
107-
console.error(err)
107+
jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
108+
if (err) console.error(err)
108109
})
109110
```
110111

111112
**overriding EOL:**
112113

113114
```js
114-
var jsonfile = require('jsonfile')
115+
const jsonfile = require('jsonfile')
115116

116-
var file = '/tmp/data.json'
117-
var obj = {name: 'JP'}
117+
const file = '/tmp/data.json'
118+
const obj = { name: 'JP' }
118119

119-
jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
120-
console.error(err)
120+
jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
121+
if (err) console.error(err)
121122
})
122123
```
123124

124125
**appending to an existing JSON file:**
125126

126-
You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
127+
You can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.
127128

128129
```js
129-
var jsonfile = require('jsonfile')
130+
const jsonfile = require('jsonfile')
130131

131-
var file = '/tmp/mayAlreadyExistedData.json'
132-
var obj = {name: 'JP'}
132+
const file = '/tmp/mayAlreadyExistedData.json'
133+
const obj = { name: 'JP' }
133134

134-
jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
135-
console.error(err)
135+
jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
136+
if (err) console.error(err)
136137
})
137138
```
138139

@@ -141,47 +142,47 @@ jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
141142
`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` and override `EOL` string.
142143

143144
```js
144-
var jsonfile = require('jsonfile')
145+
const jsonfile = require('jsonfile')
145146

146-
var file = '/tmp/data.json'
147-
var obj = {name: 'JP'}
147+
const file = '/tmp/data.json'
148+
const obj = { name: 'JP' }
148149

149150
jsonfile.writeFileSync(file, obj)
150151
```
151152

152153
**formatting with spaces:**
153154

154155
```js
155-
var jsonfile = require('jsonfile')
156+
const jsonfile = require('jsonfile')
156157

157-
var file = '/tmp/data.json'
158-
var obj = {name: 'JP'}
158+
const file = '/tmp/data.json'
159+
const obj = { name: 'JP' }
159160

160-
jsonfile.writeFileSync(file, obj, {spaces: 2})
161+
jsonfile.writeFileSync(file, obj, { spaces: 2 })
161162
```
162163

163164
**overriding EOL:**
164165

165166
```js
166-
var jsonfile = require('jsonfile')
167+
const jsonfile = require('jsonfile')
167168

168-
var file = '/tmp/data.json'
169-
var obj = {name: 'JP'}
169+
const file = '/tmp/data.json'
170+
const obj = { name: 'JP' }
170171

171-
jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
172+
jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
172173
```
173174

174175
**appending to an existing JSON file:**
175176

176-
You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
177+
You can use `fs.writeFileSync` option `{ flag: 'a' }` to achieve this.
177178

178179
```js
179-
var jsonfile = require('jsonfile')
180+
const jsonfile = require('jsonfile')
180181

181-
var file = '/tmp/mayAlreadyExistedData.json'
182-
var obj = {name: 'JP'}
182+
const file = '/tmp/mayAlreadyExistedData.json'
183+
const obj = { name: 'JP' }
183184

184-
jsonfile.writeFileSync(file, obj, {flag: 'a'})
185+
jsonfile.writeFileSync(file, obj, { flag: 'a' })
185186
```
186187

187188
License

appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
environment:
33
matrix:
44
# node.js
5-
- nodejs_version: "4"
65
- nodejs_version: "6"
76
- nodejs_version: "8"
7+
- nodejs_version: "10"
8+
89

910
# Install scripts. (runs after repo cloning)
1011
install:

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"graceful-fs": "^4.1.6"
2424
},
2525
"devDependencies": {
26-
"mocha": "2.x",
26+
"mocha": "^5.2.0",
2727
"rimraf": "^2.4.0",
28-
"standard": "^10.0.3"
28+
"standard": "^12.0.1"
2929
},
3030
"main": "index.js",
3131
"files": [

0 commit comments

Comments
 (0)