1
1
' use strict'
2
2
3
3
fs = require ' fs'
4
- os = require ' os '
4
+ Args = require ' args-js '
5
5
promise = require ' cb2promise'
6
6
Errorifier = require ' errorifier'
7
7
parseJSON = require ' json-parse-async'
8
8
9
- _stringifySync = (data ) -> JSON .stringify (data, null , 2 ) + os .EOL
9
+ _stringify = (data , replacer , space ) ->
10
+ JSON .stringify (data, replacer, space) + ' \n '
11
+
12
+ _stringifyAsync = ->
13
+ {data , replacer , space , cb } = Args ([
14
+ { data : Args .OBJECT | Args .Required }
15
+ { replacer : Args .FUNCTION | Args .Optional }
16
+ { space : Args .NUMBER | Args .Optional , _default : 2 }
17
+ { cb : Args .FUNCTION | Args .Optional }
18
+ ], arguments )
10
19
11
- _stringify = (data , cb ) ->
12
20
try
13
- content = JSON .stringify (data, null , 2 ) + os . EOL
21
+ content = JSON .stringify (data, replacer, space ) + ' \n '
14
22
catch err
15
23
content = {}
16
24
error = new Errorifier
@@ -20,40 +28,74 @@ _stringify = (data, cb) ->
20
28
process .nextTick ->
21
29
cb error, content
22
30
23
- _read = (filename , cb ) ->
24
- fs .readFile filename, encoding : ' utf8 ' , cb
31
+ _loadAsync = (filepath , opts , cb ) ->
32
+ fs .readFile filepath, opts , cb
25
33
26
- _readSync = (filename ) ->
27
- fs .readFile filename, encoding : ' utf8 '
34
+ _load = (filepath , opts ) ->
35
+ fs .readFileSync filepath, opts
28
36
29
- _save = (filename , data , cb ) ->
30
- _stringify data, (err , data ) ->
37
+ _saveAsync = (filepath , data , opts , cb ) ->
38
+ _stringifyAsync data, opts . replacer , (err , data ) ->
31
39
return cb err, data if err
32
- fs .writeFile filename, data, encoding : ' utf8' , cb
33
-
34
- _saveSync = (filename , data ) ->
35
- fs .writeFile filename, _stringifySync (data), encoding : ' utf8'
40
+ fs .writeFile filepath, data, opts, cb
36
41
42
+ _save = (filepath , data , opts ) ->
43
+ fs .writeFileSync filepath, _stringify (data), opts
37
44
38
45
module .exports =
39
46
40
- stringify : (data , cb ) ->
41
- return promise _stringify, data if arguments .length is 1
42
- _stringify data, cb
43
-
44
- stringifySync : _stringifySync
45
-
46
- read : (filename , cb ) ->
47
- return promise _read, filename if arguments .length is 1
48
- _read filename, cb
49
-
50
- readSync : _readSync
51
-
52
- save : (filename , data , cb ) ->
53
- _save filename, data, cb
54
- return promise _save, filename if arguments .length is 1
55
-
56
- saveSync : _saveSync
57
-
58
- parse : parseJSON
59
- parseSync : JSON .parse
47
+ stringifyAsync : (data , replacer , space , cb )->
48
+ return promise _stringifyAsync, data unless cb
49
+ _stringifyAsync data, cb
50
+
51
+ stringify : ->
52
+ {data , replacer , space } = Args ([
53
+ { data : Args .OBJECT | Args .Required }
54
+ { replacer : Args .FUNCTION | Args .Optional }
55
+ { space : Args .NUMBER | Args .Optional _DEFAULT : 2 }
56
+ ], arguments )
57
+
58
+ _stringify data, replacer, space
59
+
60
+ parseAsync : parseJSON
61
+
62
+ parse : JSON .parse
63
+
64
+ loadAsync : ->
65
+ OPTIONS =
66
+ encoding : ' utf8'
67
+ {filepath , opts , cb } = Args ([
68
+ { filepath : Args .STRING | Args .Required }
69
+ { opts : Args .OBJECT | Args .Optional , _default : encoding : ' utf8' }
70
+ { cb : Args .FUNCTION | Args .Optional }
71
+ ], arguments )
72
+
73
+ return promise _loadAsync, filepath, opts unless cb
74
+ _loadAsync filepath, opts, cb
75
+
76
+ load : ->
77
+ {filepath , opts } = Args ([
78
+ { filepath : Args .STRING | Args .Required }
79
+ { opts : Args .OBJECT | Args .Optional , _default : encoding : ' utf8' }
80
+ ], arguments )
81
+
82
+ _load filepath, opts
83
+
84
+ saveAsync : ->
85
+ {filepath , data , opts , cb } = Args ([
86
+ { filepath : Args .STRING | Args .Required }
87
+ { data : Args .OBJECT | Args .Required }
88
+ { opts : Args .OBJECT | Args .Optional , _default : encoding : ' utf8' }
89
+ { cb : Args .FUNCTION | Args .Optional }
90
+ ], arguments )
91
+
92
+ return promise _saveAsync, filepath, data, opts unless cb
93
+ _saveAsync filepath, data, opts, cb
94
+
95
+ save : ->
96
+ {filepath , data , opts } = Args ([
97
+ { filepath : Args .STRING | Args .Required }
98
+ {opts : Args .OBJECT | Args .Optional , _default : encoding : ' utf8' }
99
+ ], arguments )
100
+
101
+ _save filepath, data, opts
0 commit comments