Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 74ea38a

Browse files
committed
add objectMode option
1 parent 07104aa commit 74ea38a

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ file
4141
```
4242

4343

44+
### Options
45+
46+
##### objectMode
47+
48+
> Enable [objectMode](https://nodejs.org/api/stream.html#stream_object_mode)
49+
50+
default: true
51+
4452
## [License](LICENSE) (MIT)

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ var Excel = require('exceljs')
22
var through = require('through2')
33
var duplex = require('duplexify')
44

5-
module.exports = function exceljsStream() {
5+
var defaultOpts = {
6+
objectMode: true
7+
}
8+
9+
module.exports = function exceljsStream(opts) {
10+
opts = opts || defaultOpts
611
var input = through()
7-
var second = through()
12+
var second = through({ objectMode: opts.objectMode })
813
var workbook = new Excel.Workbook()
914

1015
var headers = null
@@ -21,7 +26,11 @@ module.exports = function exceljsStream() {
2126
if (!headers) return
2227
item[headers[k]] = v
2328
})
24-
second.push(JSON.stringify(item))
29+
if (!opts.objectMode) {
30+
second.push(JSON.stringify(item))
31+
return
32+
}
33+
second.push(item)
2534
})
2635
})
2736
second.emit('end')

test/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,26 @@ describe('exceljs-through-stream', function () {
1717
var res = []
1818
file.pipe(exs())
1919
.on('data', function (d) {
20-
res.push(JSON.parse(String(d)))
20+
res.push(d)
21+
})
22+
.on('end', function () {
23+
should(res.length).equal(4)
24+
should(JSON.stringify(res[0])).equal(
25+
JSON.stringify({
26+
row: 'row1',
27+
date: '2017-02-08T00:00:00.000Z',
28+
cost: 100,
29+
notes: 111
30+
}))
31+
done()
32+
})
33+
})
34+
it('parse xlsx files with string mode', function (done) {
35+
var file = fs.createReadStream(__dirname + '/file.xlsx')
36+
var res = []
37+
file.pipe(exs({ objectMode: false }))
38+
.on('data', function (d) {
39+
res.push(JSON.parse(d))
2140
})
2241
.on('end', function () {
2342
should(res.length).equal(4)

0 commit comments

Comments
 (0)