Skip to content

Commit af4e280

Browse files
committed
feature(writejson) add options
1 parent e74f3df commit af4e280

File tree

3 files changed

+132
-11
lines changed

3 files changed

+132
-11
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Write stringified object to file.
88
npm i writejson --save
99
```
1010
## How to use?
11+
To handle formating optional argument `options` could be used accroding to [JSON.stringify][StringifyURL].
12+
13+
### API
14+
15+
#### writejson(name, object, [ options, ] callback)
16+
Asynchonouse write stringified object.
1117

1218
```js
1319
var writejson = require('writejson');
@@ -17,19 +23,41 @@ writejson('data.json', {hello: 'world'}, function(error) {
1723
console.error(error.message);
1824
});
1925

26+
var options = {
27+
replacer: ['hello'],// properties to put in json
28+
space: 4 // default space count
29+
eof: true // default new line at end of file
30+
};
31+
32+
writejson('data.json', {hello: 'world'}, options, function(error) {
33+
if (error)
34+
console.error(error.message);
35+
});
36+
37+
```
38+
#### writejson.sync(name, object [, options])
39+
Synchonouse write stringified object.
40+
41+
```js
2042
try {
2143
writejson.sync('data.json', {hello: 'world'});
2244
} catch(error) {
2345
console.log(error.message);
2446
}
47+
```
48+
49+
#### writejson.sync.try(name, object [, options])
50+
Synchonouse try to write stringified object.
2551

52+
```js
2653
writejson.sync.try('data.json', {hello: 'world'});
2754
```
2855

2956
## License
3057

3158
MIT
3259

60+
[StringifyURL]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
3361
[NPMIMGURL]: https://img.shields.io/npm/v/writejson.svg?style=flat
3462
[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/node-writejson/master.svg?style=flat
3563
[DependencyStatusIMGURL]: https://img.shields.io/gemnasium/coderaiser/node-writejson.svg?style=flat

lib/writejson.js

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
var fs = require('fs'),
44
tryCatch = require('try-catch');
55

6-
module.exports = function(name, json, callback) {
7-
check(name, json);
6+
module.exports = function(name, json, options, callback) {
7+
if (!callback) {
8+
callback = options;
9+
options = {};
10+
}
11+
12+
check(name, json, options);
813
checkCB(callback);
914

1015
var str,
1116
error = tryCatch(function() {
12-
str = JSON.stringify(json);
17+
str = stringify(json, options);
1318
});
1419

1520
if (error)
@@ -20,27 +25,56 @@ module.exports = function(name, json, callback) {
2025

2126
module.exports.sync = sync;
2227

23-
function sync(name, data) {
24-
check(name, data);
25-
fs.writeFileSync(name, JSON.stringify(data));
28+
function sync(name, data, options) {
29+
options = options || {};
30+
31+
check(name, data, options);
32+
fs.writeFileSync(name, stringify(data, options));
2633
}
2734

28-
module.exports.sync.try = function(name, data) {
29-
check(name, data);
35+
module.exports.sync.try = function(name, data, options) {
36+
options = options || {};
37+
check(name, data, options);
3038

3139
var error = tryCatch(function() {
32-
sync(name, data);
40+
sync(name, data, options);
3341
});
3442

3543
return error;
3644
};
3745

38-
function check(name, json) {
46+
function defaultOptions(options) {
47+
if (typeof options.space === 'undefined')
48+
options.space = 4;
49+
50+
if (typeof options.eof === 'undefined')
51+
options.eof = true;
52+
53+
return options;
54+
}
55+
56+
function stringify(data, options) {
57+
var result;
58+
59+
defaultOptions(options);
60+
61+
result = JSON.stringify(data, options.replacer, options.space);
62+
63+
if (options.eof)
64+
result += '\n';
65+
66+
return result;
67+
}
68+
69+
function check(name, json, options) {
3970
if (typeof name !== 'string')
4071
throw Error('name should be string!');
4172

4273
if (typeof json !== 'object')
4374
throw Error('json should be object!');
75+
76+
if (typeof options !== 'object')
77+
throw Error('options should be object!');
4478
}
4579

4680
function checkCB(callback) {

test/writejson.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let writejson = require('..');
99
const tmp = os.tmpdir();
1010
const NAME = path.join(tmp, String(Math.random()));
1111
const json = {
12-
hello: 'world'
12+
hello: 'world',
13+
bye: 'sword'
1314
};
1415

1516
test('writejson: should write json data to file', t => {
@@ -28,6 +29,57 @@ test('writejson: should write json data to file', t => {
2829
});
2930
});
3031

32+
test('writejson: should write json data to file with options', t => {
33+
let result = {
34+
hello: 'world'
35+
};
36+
37+
let options = {
38+
replacer: ['hello'],
39+
space: 2,
40+
eof: false
41+
};
42+
43+
let resultStr = JSON.stringify(json, options.replacer, options.space);
44+
45+
writejson(NAME, json, options, error => {
46+
t.notOk(error, 'no write error');
47+
48+
fs.readFile(NAME, 'utf8', (error, data) => {
49+
t.notOk(error, 'no read error');
50+
51+
t.equal(resultStr, data, 'data should be equal');
52+
t.deepEqual(JSON.parse(data), result, 'objects should be equal');
53+
54+
fs.unlink(NAME, error => {
55+
t.notOk(error, 'no remove error');
56+
t.end();
57+
});
58+
});
59+
});
60+
});
61+
62+
test('writejson: should write json data to file with default options', t => {
63+
let resultStr = JSON.stringify(json, null, 4);
64+
resultStr += '\n';
65+
66+
writejson(NAME, json, error => {
67+
t.notOk(error, 'no write error');
68+
69+
fs.readFile(NAME, 'utf8', (error, data) => {
70+
t.notOk(error, 'no read error');
71+
72+
t.equal(resultStr, data, 'data should be equal');
73+
t.deepEqual(JSON.parse(data), json, 'objects should be equal');
74+
75+
fs.unlink(NAME, error => {
76+
t.notOk(error, 'no remove error');
77+
t.end();
78+
});
79+
});
80+
});
81+
});
82+
3183
test('writejson: no args', t => {
3284
t.throws(writejson, /name should be string!/, 'NAME check');
3385
t.end();
@@ -40,6 +92,13 @@ test('writejson: no json', t => {
4092
t.end();
4193
});
4294

95+
test('writejson: options not object', t => {
96+
let fn = () => writejson('hello', {}, 'options', () => {});
97+
98+
t.throws(fn, /options should be object!/, 'options check');
99+
t.end();
100+
});
101+
43102
test('writejson: no callback', t => {
44103
let fn = () => writejson('hello', [1,2,3]);
45104

0 commit comments

Comments
 (0)