-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (42 loc) · 1.08 KB
/
index.js
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
const jsesc = require('jsesc')
const debug = require('debug')('json-line-parser')
const MAX_ERROR = 50
let error_counter = 0
const handle_error = function (error, line, skip_errors) {
error_counter += 1
debug(line)
if (error && error_counter >= MAX_ERROR && !skip_errors) { // @TODO: turn this into an error rate?
throw new Error('Reached max number of errors')
}
}
const standard_parser = function (line) {
if (line.length > 0) {
try {
return JSON.parse(line)
} catch (e) {
handle_error(e, line, false)
return undefined
}
}
}
// this is probably an optimization killer, use parsimoniously
const kissmetrics_parser = function (line) {
if (line.length > 0) {
try {
return JSON.parse(jsesc(line).replace("\\'", "'"))
} catch (e) {
return undefined
}
}
}
/**
* @param {object} [options]
* @param {object} [options.kissmetrics] set to true to use a kissmetrics specific parser
*/
module.exports = function (options = {}) {
if (options.kissmetrics) {
return kissmetrics_parser
} else {
return standard_parser
}
}