-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
60 lines (49 loc) · 1.56 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
48
49
50
51
52
53
54
55
56
57
58
59
60
import rdf from '@rdfjs/data-model'
import CsvParser from './lib/CsvParser.js'
import parseMetadata from './lib/metadata/index.js'
import ObjectParserTransform from './lib/ObjectParserTransform.js'
class Parser {
constructor ({ metadata, baseIRI = '', factory = rdf, timezone, relaxColumnCount, skipLinesWithError } = {}) {
this.metadata = metadata
this.baseIRI = baseIRI
this.factory = factory
this.timezone = timezone
this.relaxColumnCount = relaxColumnCount
this.skipLinesWithError = skipLinesWithError
}
import (input, {
metadata = this.metadata,
baseIRI = this.baseIRI,
factory = this.factory,
timezone = this.timezone,
relaxColumnCount = this.relaxColumnCount,
skipLinesWithError = this.skipLinesWithError
} = {}) {
const parsedMetadata = parseMetadata(metadata, { baseIRI, factory, timezone })
const reader = new CsvParser({
delimiter: parsedMetadata.delimiter,
lineTerminators: parsedMetadata.lineTerminators,
quoteChar: parsedMetadata.quoteChar,
relaxColumnCount,
skipLinesWithError
})
const output = new ObjectParserTransform({ baseIRI, factory, metadata: parsedMetadata, timezone })
input.on('end', () => {
if (!output.readable) {
output.end()
}
})
reader.on('error', err => {
output.destroy(err)
})
input.on('error', err => {
output.destroy(err)
})
input.pipe(reader).pipe(output)
return output
}
static import (input, options) {
return (new Parser(options)).import(input)
}
}
export default Parser