forked from rdf-ext/rdf-parser-csvw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.57 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
const CsvParser = require('./lib/CsvParser')
const parseMetadata = require('./lib/metadata')
const rdf = require('@rdfjs/data-model')
const ObjectParserTransform = require('./lib/ObjectParserTransform')
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)
}
}
module.exports = Parser