Skip to content

Commit 709c99a

Browse files
kartik2406RyanZim
authored andcommitted
Add promise support using universalify (#109)
Added new tests, refactored existing tests
1 parent 5d0f9e9 commit 709c99a

File tree

7 files changed

+341
-60
lines changed

7 files changed

+341
-60
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ jsonfile.readFile(file, function(err, obj) {
4141
})
4242
```
4343

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+
46+
```js
47+
var jsonfile = require('jsonfile')
48+
var file = '/tmp/data.json'
49+
jsonfile.readFile(file)
50+
.then(obj => console.dir(obj)))
51+
.catch(error => console.log(error));
52+
```
4453

4554
### readFileSync(filename, [options])
4655

@@ -70,6 +79,21 @@ jsonfile.writeFile(file, obj, function (err) {
7079
console.error(err)
7180
})
7281
```
82+
Or use with promises as follows:
83+
84+
```js
85+
var jsonfile = require('jsonfile')
86+
87+
var file = '/tmp/data.json'
88+
var obj = {name: 'JP'}
89+
90+
jsonfile.writeFile(file, obj)
91+
.then(res => {
92+
console.log("Write complete");
93+
})
94+
.catch(error => console.log(error));
95+
```
96+
7397

7498
**formatting with spaces:**
7599

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ try {
44
} catch (_) {
55
_fs = require('fs')
66
}
7+
const universalify = require('universalify')
78

8-
function readFile (file, options, callback) {
9+
function readFileWithCallback (file, options, callback) {
910
if (callback == null) {
1011
callback = options
1112
options = {}
1213
}
1314

1415
if (typeof options === 'string') {
15-
options = {encoding: options}
16+
options = { encoding: options }
1617
}
1718

1819
options = options || {}
@@ -44,10 +45,12 @@ function readFile (file, options, callback) {
4445
})
4546
}
4647

48+
const readFile = universalify.fromCallback(readFileWithCallback)
49+
4750
function readFileSync (file, options) {
4851
options = options || {}
4952
if (typeof options === 'string') {
50-
options = {encoding: options}
53+
options = { encoding: options }
5154
}
5255

5356
var fs = options.fs || _fs
@@ -88,7 +91,7 @@ function stringify (obj, options) {
8891
return str.replace(/\n/g, EOL) + EOL
8992
}
9093

91-
function writeFile (file, obj, options, callback) {
94+
function writeFileWithCallback (file, obj, options, callback) {
9295
if (callback == null) {
9396
callback = options
9497
options = {}
@@ -108,6 +111,8 @@ function writeFile (file, obj, options, callback) {
108111
fs.writeFile(file, str, options, callback)
109112
}
110113

114+
const writeFile = universalify.fromCallback(writeFileWithCallback)
115+
111116
function writeFileSync (file, obj, options) {
112117
options = options || {}
113118
var fs = options.fs || _fs

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
],
1717
"author": "JP Richardson <[email protected]>",
1818
"license": "MIT",
19-
"dependencies": {},
19+
"dependencies": {
20+
"universalify": "^0.1.2"
21+
},
2022
"optionalDependencies": {
2123
"graceful-fs": "^4.1.6"
2224
},

test/read-file-sync.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('+ readFileSync()', function () {
2323

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

2929
try {
@@ -60,7 +60,7 @@ describe('+ readFileSync()', function () {
6060
jf.readFileSync(file)
6161
})
6262

63-
var obj = jf.readFileSync(file, {throws: false})
63+
var obj = jf.readFileSync(file, { throws: false })
6464
assert.strictEqual(obj, null)
6565
})
6666
})
@@ -72,7 +72,7 @@ describe('+ readFileSync()', function () {
7272
fs.writeFileSync(file, data)
7373

7474
assert.throws(function () {
75-
jf.readFileSync(file, {throws: true})
75+
jf.readFileSync(file, { throws: true })
7676
})
7777
})
7878
})
@@ -81,7 +81,7 @@ describe('+ readFileSync()', function () {
8181
it('should return null', function () {
8282
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
8383

84-
var obj = jf.readFileSync(file, {throws: false})
84+
var obj = jf.readFileSync(file, { throws: false })
8585
assert.strictEqual(obj, null)
8686
})
8787
})
@@ -91,7 +91,7 @@ describe('+ readFileSync()', function () {
9191
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
9292

9393
assert.throws(function () {
94-
jf.readFileSync(file, {throws: true})
94+
jf.readFileSync(file, { throws: true })
9595
})
9696
})
9797
})
@@ -111,7 +111,7 @@ describe('+ readFileSync()', function () {
111111
}
112112

113113
fs.writeFileSync(file, JSON.stringify(obj))
114-
var data = jf.readFileSync(file, {reviver: sillyReviver})
114+
var 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')

0 commit comments

Comments
 (0)