-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
78 lines (71 loc) · 2.26 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* eslint-disable no-loops/no-loops */
const Excel = require('exceljs')
const duplex = require('duplexify')
const { Readable, PassThrough, finished } = require('stream')
const matchSelector = (selector, worksheet) =>
selector.includes('*') || selector.includes(worksheet.name)
const handleError = (err, isEnded) => {
if (!err) return
if (isEnded && err.message === 'FILE_ENDED') return
if (err.message && err.message.includes('invalid signature')) {
err = new Error('Legacy XLS files are not supported, use an XLSX file instead!')
}
throw err
}
module.exports = ({ mapHeaders, mapValues, selector = '*' } = {}) => {
if (selector && !Array.isArray(selector)) selector = [ selector ]
let isEnded = false
const input = new PassThrough()
const reader = new Excel.stream.xlsx.WorkbookReader(input)
const createReader = async function* () {
try {
for await (const worksheet of reader) {
if (!matchSelector(selector, worksheet)) continue
let headers
for await (const row of worksheet) {
if (row.values.length === 0) continue // blank
if (!headers) {
headers = mapHeaders ? row.values.map(mapHeaders) : row.values
out.emit('header', headers)
continue
}
const item = row.values.reduce((acc, v, idx) => {
acc[headers[idx]] = mapValues ? mapValues(v) : v
return acc
}, {})
yield item
}
}
} catch (err) {
handleError(err, isEnded)
}
}
const out = Readable.from(createReader())
const final = duplex.obj(input, out)
finished(input, () => isEnded = true)
finished(out, (err) => {
isEnded = true
if (err) out.emit('error', err)
})
return final
}
module.exports.getSelectors = () => {
let isEnded = false
const input = new PassThrough()
const reader = new Excel.stream.xlsx.WorkbookReader(input)
const createReader = async function* () {
yield '*'
try {
for await (const worksheet of reader) {
yield worksheet.name
}
} catch (err) {
handleError(err, isEnded)
}
}
const out = Readable.from(createReader())
const final = duplex.obj(input, out)
finished(out, () => isEnded = true)
finished(input, () => isEnded = true)
return final
}