-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsvw.js
96 lines (84 loc) · 2.11 KB
/
csvw.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sinkToDuplex from '@rdfjs/sink-to-duplex'
import CsvwParser from '@zazuko/rdf-parser-csvw'
import tracer from './lib/tracer.js'
import { toDataset } from './lib/stream.js'
/**
* @typedef {{
* metadata: import('@rdfjs/types').DatasetCore
* timezone?: string
* relaxColumnCount?: boolean
* skipLinesWithError?: boolean
* trimHeaders?: boolean
* skipEmptyLines?: boolean
* }} Options
*/
/**
* @overload
* @param {Options} args
* @return {import('duplexify').Duplexify}
*/
/**
* @overload
* @param {import('@rdfjs/types').DatasetCore} metadata
* @return {import('duplexify').Duplexify}
*/
/**
* @this {import('barnard59-core').Context}
* @param {Options | import('@rdfjs/types').DatasetCore} args
*/
function parse(args) {
/**
* @type {import('@rdfjs/types').DatasetCore}
*/
let metadata
let relaxColumnCount = false
let skipLinesWithError = false
let timezone = 'local'
let trimHeaders = true
/**
* @type {Boolean | undefined}
*/
let skipEmptyLines
if ('metadata' in args) {
metadata = args.metadata
if (typeof args.relaxColumnCount !== 'undefined') {
relaxColumnCount = Boolean(args.relaxColumnCount)
}
if (typeof args.skipLinesWithError !== 'undefined') {
skipLinesWithError = Boolean(args.skipLinesWithError)
}
if (typeof args.timezone !== 'undefined') {
timezone = args.timezone
}
if (typeof args.trimHeaders !== 'undefined') {
trimHeaders = Boolean(args.trimHeaders)
}
if (typeof args.skipEmptyLines !== 'undefined') {
skipEmptyLines = Boolean(args.skipEmptyLines)
}
} else {
metadata = args
}
return tracer.startActiveSpan('csvw:parse', async span => {
try {
const dataset = await toDataset(this.env, metadata)
span.addEvent('metadata')
return sinkToDuplex(new CsvwParser({
factory: this.env,
metadata: dataset,
relaxColumnCount,
skipLinesWithError,
timezone,
trimHeaders,
skipEmptyLines,
}), {
readableObjectMode: true,
})
} finally {
span.end()
}
})
}
export {
parse,
}