Skip to content

Commit 3bd9181

Browse files
committed
Cleanup docs examples
1 parent 2c38bf4 commit 3bd9181

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

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

0 commit comments

Comments
 (0)