-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (31 loc) · 796 Bytes
/
index.js
File metadata and controls
41 lines (31 loc) · 796 Bytes
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
'use strict';
module.exports = stringify;
function stringify( opts ){
opts = opts || {};
var indent = opts.indent || '';
var open = opts.open || '';
var separator = opts.separator || '\n';
var close = opts.close || '\n';
var stringifier = opts.stringifier || JSON.stringify;
return function( read ){
var opened = false;
var closed = false;
return function( abort, cb ){
if (closed)
return cb(true);
read(abort, function( end, data ){
if (end) {
if (end === true)
return (closed = true) && cb(null, opened
? close
: open+close);
return cb(end); // error
}
var json = stringifier(data, null, indent);
if (!opened)
return (opened = true) && cb(null, open+json);
return cb(null, separator+json);
});
}
}
}