-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (48 loc) · 1.37 KB
/
index.js
File metadata and controls
66 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var fs = require('fs');
/**
* ### js file to json file
* options.replacer - function or null or undefined
* options.space - how many space, default 'use strict'
*/
function js2json(jsfile, jsonfile, options, callback) {
if (arguments.length < 3) {
return new Error('wrong params');
}
if (arguments.length === 3) {
callback = options;
options = {};
}
options = options || {};
var jsObj = require(jsfile);
var str = JSON.stringify(jsObj, options.replacer , options.space || 2);
fs.writeFile(jsonfile, str, callback);
}
/**
* ### json file to js file
* options.prefix - add some chars to the head file, default 'use strict'
* options.replacer - function or null or undefined
* options.space - how many space, default 'use strict'
*/
function json2js(jsonfile, jsfile, options, callback) {
if (arguments.length < 3) {
return new Error('wrong params');
}
if (arguments.length === 3) {
callback = options;
options = {};
}
options = options || {};
var jsonObj = require(jsonfile);
var str = '';
if (options.prefix) {
str += options.prefix;
} else {
str += "'use strict';";
}
str += '\n\nmodule.exports = ';
str += JSON.stringify(jsonObj, options.replacer, options.space || 2);
fs.writeFile(jsfile, str, callback);
}
module.exports.js2json = js2json;
module.exports.json2js = json2js;