-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (26 loc) · 723 Bytes
/
index.js
File metadata and controls
31 lines (26 loc) · 723 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
var through = require('through2')
var split = require('split2')
var EOL = require('os').EOL
var stringify = require('json-stringify-safe')
module.exports = parse
module.exports.serialize = module.exports.stringify = serialize
module.exports.parse = parse
function parse (opts) {
opts = opts || {}
opts.strict = opts.strict !== false
function parseRow (row) {
try {
if (row) return JSON.parse(row)
} catch (e) {
if (opts.strict) {
this.emit('error', new Error('Could not parse row ' + row.slice(0, 50) + '...'))
}
}
}
return split(parseRow, opts)
}
function serialize (opts) {
return through.obj(opts, function(obj, enc, cb) {
cb(null, stringify(obj) + EOL)
})
}