Skip to content

Commit 1f63a4d

Browse files
committed
feature(writejson) callback -> promise
1 parent c757970 commit 1f63a4d

9 files changed

Lines changed: 101 additions & 150 deletions

File tree

.eslintrc

Lines changed: 0 additions & 22 deletions
This file was deleted.

.eslintrc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"rules": {
3+
"node/no-unsupported-features/node-builtins": "off"
4+
},
5+
"extends": [
6+
"plugin:putout/recommended",
7+
"plugin:node/recommended"
8+
],
9+
"plugins": [
10+
"putout",
11+
"node"
12+
]
13+
}

.eslintrc.test

Lines changed: 0 additions & 18 deletions
This file was deleted.

.madrun.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const {run} = require('madrun');
4+
5+
module.exports = {
6+
'fix:lint': () => run('lint', '--fix'),
7+
'lint': () => 'putout lib test .madrun.js',
8+
'test': () => 'tape test/*.js',
9+
'coverage': () => 'nyc npm test',
10+
'report': () => 'nyc report --reporter=text-lcov | coveralls',
11+
};
12+

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: node_js
22
node_js:
3-
- 8
3+
- 13
4+
- 12
45
- 10
56

67
script:

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Asynchonouse write stringified object.
1818
```js
1919
const writejson = require('writejson');
2020

21-
writejson('data.json', {hello: 'world'}, (error) => {
22-
if (error)
23-
console.error(error.message);
24-
});
21+
const [error] = await tryToCatch(writejson, 'data.json', {hello: 'world'});
22+
23+
if (error)
24+
console.error(error.message);
2525

2626
const options = {
2727
replacer: ['hello'], // properties to put in json
@@ -32,10 +32,7 @@ const options = {
3232
flag: 'w', // default
3333
};
3434

35-
writejson('data.json', {hello: 'world'}, options, (error) => {
36-
if (error)
37-
console.error(error.message);
38-
});
35+
await writejson('data.json', {hello: 'world'}, options);
3936

4037
```
4138
#### writejson.sync(name, object[, options])

lib/writejson.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const fs = require('fs');
4+
const {writeFile} = fs.promises;
5+
46
const tryCatch = require('try-catch');
57

68
const getWriteOptions = (options) => ({
@@ -9,25 +11,19 @@ const getWriteOptions = (options) => ({
911
flag: options.flag,
1012
});
1113

12-
module.exports = (name, json, options, callback) => {
13-
if (!callback) {
14-
callback = options;
15-
options = {};
16-
}
17-
14+
module.exports = async (name, json, options = {}) => {
1815
check(name, json, options);
19-
checkCB(callback);
2016

2117
const str = stringify(json, options);
2218
const writeOptions = getWriteOptions(options);
2319

24-
fs.writeFile(name, str, writeOptions, callback);
20+
await writeFile(name, str, writeOptions);
2521
};
2622

2723
module.exports.sync = sync;
2824

2925
function sync(name, data, options) {
30-
options = options || {};
26+
options = options || {};
3127
check(name, data, options);
3228

3329
const writeOptions = getWriteOptions(options);
@@ -38,8 +34,7 @@ module.exports.sync.try = (name, data, options) => {
3834
options = options || {};
3935
check(name, data, options);
4036

41-
const result = tryCatch(sync, name, data, options);
42-
const error = result[0];
37+
const [error] = tryCatch(sync, name, data, options);
4338

4439
return error;
4540
};
@@ -58,7 +53,7 @@ function stringify(data, options) {
5853
defaultOptions(options);
5954

6055
const result = JSON.stringify(data, options.replacer, options.space);
61-
const eof = options.eof;
56+
const {eof} = options;
6257

6358
return maybeAddNewLine(result, {
6459
eof,
@@ -83,8 +78,3 @@ function check(name, json, options) {
8378
throw Error('options should be object!');
8479
}
8580

86-
function checkCB(callback) {
87-
if (typeof callback !== 'function')
88-
throw Error('callback should be function!');
89-
}
90-

package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
},
1010
"main": "lib/writejson.js",
1111
"scripts": {
12-
"lint": "redrun lint:*",
13-
"lint:lib": "eslint lib",
14-
"lint:test": "eslint -c .eslintrc.test --no-eslintrc test",
15-
"test": "tape test/*.js",
16-
"coverage": "nyc npm test",
17-
"report": "nyc report --reporter=text-lcov | coveralls"
12+
"fix:lint": "madrun fix:lint",
13+
"lint": "madrun lint",
14+
"test": "madrun test",
15+
"coverage": "madrun coverage",
16+
"report": "madrun report"
1817
},
1918
"repository": {
2019
"type": "git",
@@ -32,13 +31,17 @@
3231
"coveralls": "^3.0.0",
3332
"es6-promisify": "^6.0.0",
3433
"eslint": "^4.19.1",
35-
"eslint-plugin-node": "^6.0.1",
34+
"eslint-plugin-node": "^11.0.0",
35+
"eslint-plugin-putout": "^3.2.1",
36+
"madrun": "^5.4.4",
3637
"nyc": "^12.0.2",
38+
"putout": "^7.13.4",
3739
"redrun": "^7.0.0",
40+
"supertape": "^1.2.4",
3841
"tape": "^4.2.2",
3942
"try-to-catch": "^1.0.2"
4043
},
4144
"engines": {
42-
"node": ">=4.0.0"
45+
"node": ">=10.0.0"
4346
}
4447
}

0 commit comments

Comments
 (0)