Skip to content

Commit 9aa76a1

Browse files
committed
feature(writejson) add
0 parents  commit 9aa76a1

7 files changed

Lines changed: 231 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
npm-debug.log*
3+

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: node_js
2+
node_js:
3+
- 5
4+
- 4
5+
6+
script:
7+
- npm test
8+
9+
notifications:
10+
email:
11+
on_success: never
12+
on_failure: change
13+
14+
sudo: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Writejson
2+
3+
Write stringified object to file.
4+
5+
## Install
6+
7+
```
8+
npm i writejson --save
9+
```
10+
## How to use?
11+
12+
```js
13+
var writejson = require('writejson');
14+
15+
writejson('data.json', {hello: 'world'}, function(error) {
16+
if (error)
17+
console.error(error.message);
18+
});
19+
20+
try {
21+
writejson.sync('data.json', {hello: 'world'});
22+
} catch(error) {
23+
console.log(error.message);
24+
}
25+
26+
writejson.sync.try('data.json', {hello: 'world'});
27+
```
28+
29+
## License
30+
31+
MIT
32+

lib/writejson.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var fs = require('fs'),
4+
tryCatch = require('try-catch');
5+
6+
module.exports = function(name, json, callback) {
7+
check(name, json);
8+
checkCB(callback);
9+
10+
var str,
11+
error = tryCatch(function() {
12+
str = JSON.stringify(json);
13+
});
14+
15+
if (error)
16+
callback(error);
17+
else
18+
fs.writeFile(name, str, callback);
19+
};
20+
21+
module.exports.sync = sync;
22+
23+
function sync(name, data) {
24+
check(name, data);
25+
fs.writeFileSync(name, JSON.stringify(data));
26+
}
27+
28+
module.exports.sync.try = function(name, data) {
29+
check(name, data);
30+
31+
var error = tryCatch(function() {
32+
sync(name, data);
33+
});
34+
35+
return error;
36+
};
37+
38+
function check(name, json, callback) {
39+
if (typeof name !== 'string')
40+
throw Error('name should be string!');
41+
42+
if (typeof json !== 'object')
43+
throw Error('json should be object!');
44+
45+
}
46+
47+
function checkCB(callback) {
48+
if (typeof callback !== 'function')
49+
throw Error('callback should be function!');
50+
}
51+

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "writejson",
3+
"version": "1.0.0",
4+
"description": "Write stringified object to file",
5+
"author": "coderaiser <mnemonic.enemy@gmail.com> (http://coderaiser.github.io/)",
6+
"license": "MIT",
7+
"bugs": {
8+
"url": "https://github.com/coderaiser/node-writejson/issues"
9+
},
10+
"main": "lib/writejson.js",
11+
"scripts": {
12+
"test": "tape test/*.js"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "http://github.com/coderaiser/node-writejson.git"
17+
},
18+
"keywords": [
19+
"read",
20+
"parse",
21+
"json"
22+
],
23+
"dependencies": {
24+
"try-catch": "~1.0.0"
25+
},
26+
"devDependencies": {
27+
"tape": "^4.2.2"
28+
}
29+
}

test/writejson.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
let os = require('os');
4+
let path = require('path');
5+
let fs = require('fs');
6+
let test = require('tape');
7+
let writejson = require('..');
8+
9+
let tmp = os.tmpdir();
10+
let name = path.join(tmp, String(Math.random()));
11+
let json = {
12+
hello: 'world'
13+
};
14+
15+
test('writejson: should write json data to file', t => {
16+
writejson(name, json, error => {
17+
t.notOk(error, 'no write error');
18+
19+
fs.readFile(name, 'utf8', (error, data) => {
20+
t.notOk(error, 'no read error');
21+
t.deepEqual(json, JSON.parse(data), 'data should be equal');
22+
23+
fs.unlink(name, error => {
24+
t.notOk(error, 'no remove error');
25+
t.end();
26+
});
27+
});
28+
});
29+
});
30+
31+
test('writejson: no args', t => {
32+
t.throws(writejson, /name should be string!/, 'name check');
33+
t.end();
34+
});
35+
36+
test('writejson: no json', t => {
37+
let fn = () => writejson('hello');
38+
39+
t.throws(fn, /json should be object!/, 'json check');
40+
t.end();
41+
});
42+
43+
test('writejson: no callback', t => {
44+
let fn = () => writejson('hello', [1,2,3]);
45+
46+
t.throws(fn, /callback should be function!/, 'callback check');
47+
t.end();
48+
});
49+
50+
test('writejson.sync: should write json data to file synchonously', t => {
51+
writejson.sync(name, json);
52+
let data = fs.readFileSync(name, 'utf8');
53+
t.ok(data, 'data should be read');
54+
t.deepEqual(json, JSON.parse(data), 'data should be equal');
55+
fs.unlinkSync(name);
56+
t.end();
57+
});
58+
59+
test('writejson.sync: no args', t => {
60+
t.throws(writejson.sync, /name should be string!/, 'name check');
61+
t.end();
62+
});
63+
64+
test('writejson.sync: no json', t => {
65+
let fn = () => writejson.sync('hello');
66+
67+
t.throws(fn, /json should be object!/, 'json check');
68+
t.end();
69+
});
70+
71+
test('writejson.sync.try: no args', t => {
72+
t.throws(writejson.sync.try, /name should be string!/, 'name check');
73+
t.end();
74+
});
75+
76+
test('writejson.sync.try: no json', t => {
77+
let fn = () => writejson.sync.try('hello');
78+
79+
t.throws(fn, /json should be object!/, 'json check');
80+
t.end();
81+
});

0 commit comments

Comments
 (0)