Skip to content

Commit 7501c6d

Browse files
committed
feature(writejson) es2015-ify
1 parent 0f88836 commit 7501c6d

6 files changed

Lines changed: 101 additions & 56 deletions

File tree

.eslintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 2017,
8+
},
9+
"rules": {
10+
"indent": ["error", 4],
11+
"semi": "error",
12+
"no-console": 0,
13+
"no-use-before-define": ["error", "nofunc"]
14+
},
15+
"extends": [
16+
"eslint:recommended",
17+
"plugin:node/recommended"
18+
],
19+
"plugins": [
20+
"node"
21+
]
22+
}

.eslintrc.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 2017,
8+
},
9+
"rules": {
10+
"indent": ["error", 4],
11+
"semi": "error",
12+
"no-console": 0,
13+
"no-use-before-define": ["error", "nofunc"]
14+
},
15+
"extends": [
16+
"eslint:recommended",
17+
]
18+
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ To handle formating optional argument `options` could be used according to [JSON
1616
Asynchonouse write stringified object.
1717

1818
```js
19-
var writejson = require('writejson');
19+
const writejson = require('writejson');
2020

21-
writejson('data.json', {hello: 'world'}, function(error) {
21+
writejson('data.json', {hello: 'world'}, (error) => {
2222
if (error)
2323
console.error(error.message);
2424
});
2525

26-
var options = {
26+
const options = {
2727
replacer: ['hello'],// properties to put in json
2828
space: 4 // default space count
2929
eof: true // default new line at end of file
3030
};
3131

32-
writejson('data.json', {hello: 'world'}, options, function(error) {
32+
writejson('data.json', {hello: 'world'}, options, (error) => {
3333
if (error)
3434
console.error(error.message);
3535
});

lib/writejson.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict';
22

3-
var fs = require('fs');
4-
var tryCatch = require('try-catch');
3+
const fs = require('fs');
4+
const tryCatch = require('try-catch');
55

6-
module.exports = function(name, json, options, callback) {
7-
var str;
8-
6+
module.exports = (name, json, options, callback) => {
97
if (!callback) {
108
callback = options;
119
options = {};
@@ -14,7 +12,7 @@ module.exports = function(name, json, options, callback) {
1412
check(name, json, options);
1513
checkCB(callback);
1614

17-
str = stringify(json, options);
15+
const str = stringify(json, options);
1816
fs.writeFile(name, str, callback);
1917
};
2018

@@ -27,12 +25,12 @@ function sync(name, data, options) {
2725
fs.writeFileSync(name, stringify(data, options));
2826
}
2927

30-
module.exports.sync.try = function(name, data, options) {
28+
module.exports.sync.try = (name, data, options) => {
3129
options = options || {};
3230
check(name, data, options);
3331

34-
var result = tryCatch(sync, name, data, options);
35-
var error = result[0];
32+
const result = tryCatch(sync, name, data, options);
33+
const error = result[0];
3634

3735
return error;
3836
};
@@ -48,16 +46,21 @@ function defaultOptions(options) {
4846
}
4947

5048
function stringify(data, options) {
51-
var result;
52-
5349
defaultOptions(options);
5450

55-
result = JSON.stringify(data, options.replacer, options.space);
51+
const result = JSON.stringify(data, options.replacer, options.space);
52+
const eof = options.eof;
5653

57-
if (options.eof)
58-
result += '\n';
54+
return maybeAddNewLine(result, {
55+
eof,
56+
});
57+
}
58+
59+
function maybeAddNewLine(str, options) {
60+
if (!options.eof)
61+
return str;
5962

60-
return result;
63+
return `${str}\n`;
6164
}
6265

6366
function check(name, json, options) {
@@ -66,7 +69,7 @@ function check(name, json, options) {
6669

6770
if (typeof json !== 'object')
6871
throw Error('json should be object!');
69-
72+
7073
if (typeof options !== 'object')
7174
throw Error('options should be object!');
7275
}

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"main": "lib/writejson.js",
1111
"scripts": {
12-
"jscs": "jscs --esnext lib test",
13-
"jshint": "jshint lib test",
14-
"lint": "redrun jshint jscs",
12+
"lint": "redrun lint:*",
13+
"lint:lib": "eslint lib",
14+
"lint:test": "eslint -c .eslintrc.test --no-eslintrc test",
1515
"test": "tape test/*.js",
1616
"coverage": "nyc npm test",
1717
"report": "nyc report --reporter=text-lcov | coveralls"
@@ -30,10 +30,13 @@
3030
},
3131
"devDependencies": {
3232
"coveralls": "^3.0.0",
33-
"jscs": "~3.0.3",
34-
"jshint": "^2.8.0",
33+
"eslint": "^4.19.1",
34+
"eslint-plugin-node": "^6.0.1",
3535
"nyc": "^12.0.2",
3636
"redrun": "^6.0.0",
3737
"tape": "^4.2.2"
38+
},
39+
"engines": {
40+
"node": ">=4.0.0"
3841
}
3942
}

test/writejson.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
let os = require('os');
4-
let path = require('path');
5-
let fs = require('fs');
6-
let test = require('tape');
7-
let writejson = require('..');
3+
const os = require('os');
4+
const path = require('path');
5+
const fs = require('fs');
6+
const test = require('tape');
7+
const writejson = require('..');
88

99
const tmp = os.tmpdir();
1010
const NAME = path.join(tmp, String(Math.random()));
@@ -13,7 +13,7 @@ const json = {
1313
bye: 'sword'
1414
};
1515

16-
test('writejson: should write json data to file', t => {
16+
test('writejson: should write json data to file', (t) => {
1717
writejson(NAME, json, error => {
1818
t.notOk(error, 'no write error');
1919

@@ -29,18 +29,18 @@ test('writejson: should write json data to file', t => {
2929
});
3030
});
3131

32-
test('writejson: should write json data to file with options', t => {
33-
let result = {
32+
test('writejson: should write json data to file with options', (t) => {
33+
const result = {
3434
hello: 'world'
3535
};
3636

37-
let options = {
37+
const options = {
3838
replacer: ['hello'],
3939
space: 2,
4040
eof: false
4141
};
4242

43-
let resultStr = JSON.stringify(json, options.replacer, options.space);
43+
const resultStr = JSON.stringify(json, options.replacer, options.space);
4444

4545
writejson(NAME, json, options, error => {
4646
t.notOk(error, 'no write error');
@@ -59,9 +59,8 @@ test('writejson: should write json data to file with options', t => {
5959
});
6060
});
6161

62-
test('writejson: should write json data to file with default options', t => {
63-
let resultStr = JSON.stringify(json, null, 4);
64-
resultStr += '\n';
62+
test('writejson: should write json data to file with default options', (t) => {
63+
const resultStr = JSON.stringify(json, null, 4) + '\n'
6564

6665
writejson(NAME, json, error => {
6766
t.notOk(error, 'no write error');
@@ -80,74 +79,74 @@ test('writejson: should write json data to file with default options', t => {
8079
});
8180
});
8281

83-
test('writejson: write error', t => {
82+
test('writejson: write error', (t) => {
8483
writejson('/hello.json', json, error => {
8584
t.ok(error, 'should return error: ' + error.message);
8685
t.end();
8786
});
8887
});
8988

90-
test('writejson.sync.try: write error', t => {
91-
let error = writejson.sync.try('/hello.json', json);
89+
test('writejson.sync.try: write error', (t) => {
90+
const error = writejson.sync.try('/hello.json', json);
9291

9392
t.ok(error, 'should return error: ' + error.message);
9493
t.end();
9594
});
9695

97-
test('writejson: no args', t => {
96+
test('writejson: no args', (t) => {
9897
t.throws(writejson, /name should be string!/, 'NAME check');
9998
t.end();
10099
});
101100

102-
test('writejson: no json', t => {
103-
let fn = () => writejson('hello');
101+
test('writejson: no json', (t) => {
102+
const fn = () => writejson('hello');
104103

105104
t.throws(fn, /json should be object!/, 'json check');
106105
t.end();
107106
});
108107

109-
test('writejson: options not object', t => {
110-
let fn = () => writejson('hello', {}, 'options', () => {});
108+
test('writejson: options not object', (t) => {
109+
const fn = () => writejson('hello', {}, 'options', () => {});
111110

112111
t.throws(fn, /options should be object!/, 'options check');
113112
t.end();
114113
});
115114

116-
test('writejson: no callback', t => {
117-
let fn = () => writejson('hello', [1,2,3]);
115+
test('writejson: no callback', (t) => {
116+
const fn = () => writejson('hello', [1,2,3]);
118117

119118
t.throws(fn, /callback should be function!/, 'callback check');
120119
t.end();
121120
});
122121

123-
test('writejson.sync: should write json data to file synchonously', t => {
122+
test('writejson.sync: should write json data to file synchonously', (t) => {
124123
writejson.sync(NAME, json);
125-
let data = fs.readFileSync(NAME, 'utf8');
124+
const data = fs.readFileSync(NAME, 'utf8');
126125
t.ok(data, 'data should be read');
127126
t.deepEqual(json, JSON.parse(data), 'data should be equal');
128127
fs.unlinkSync(NAME);
129128
t.end();
130129
});
131130

132-
test('writejson.sync: no args', t => {
131+
test('writejson.sync: no args', (t) => {
133132
t.throws(writejson.sync, /name should be string!/, 'NAME check');
134133
t.end();
135134
});
136135

137-
test('writejson.sync: no json', t => {
138-
let fn = () => writejson.sync('hello');
136+
test('writejson.sync: no json', (t) => {
137+
const fn = () => writejson.sync('hello');
139138

140139
t.throws(fn, /json should be object!/, 'json check');
141140
t.end();
142141
});
143142

144-
test('writejson.sync.try: no args', t => {
143+
test('writejson.sync.try: no args', (t) => {
145144
t.throws(writejson.sync.try, /name should be string!/, 'NAME check');
146145
t.end();
147146
});
148147

149-
test('writejson.sync.try: no json', t => {
150-
let fn = () => writejson.sync.try('hello');
148+
test('writejson.sync.try: no json', (t) => {
149+
const fn = () => writejson.sync.try('hello');
151150

152151
t.throws(fn, /json should be object!/, 'json check');
153152
t.end();

0 commit comments

Comments
 (0)