Skip to content

Commit 09d3ab7

Browse files
committed
remember to map the last value
1 parent 2b8441f commit 09d3ab7

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
var through = require('pull-through')
22

3-
module.exports = function split (matcher, mapper, reverse) {
3+
module.exports = function split (matcher, mapper, reverse, last) {
44
var soFar = ''
55
if('function' === typeof matcher)
66
mapper = matcher, matcher = null
77
if (!matcher)
88
matcher = '\n'
99

10+
function map(stream, piece) {
11+
if(mapper) {
12+
piece = mapper(piece)
13+
if('undefined' !== typeof piece)
14+
stream.queue(piece)
15+
}
16+
else
17+
stream.queue(piece)
18+
}
19+
1020
return through(function (buffer) {
1121
var stream = this
1222
, pieces = ( reverse
@@ -15,23 +25,21 @@ module.exports = function split (matcher, mapper, reverse) {
1525
).split(matcher)
1626

1727
soFar = reverse ? pieces.shift() : pieces.pop()
18-
1928
var l = pieces.length
2029
for (var i = 0; i < l; i++) {
21-
var piece = pieces[reverse ? l - 1 - i : i ]
22-
if(mapper) {
23-
piece = mapper(piece)
24-
if('undefined' !== typeof piece)
25-
stream.queue(piece)
26-
}
27-
else
28-
stream.queue(piece)
30+
map(stream, pieces[reverse ? l - 1 - i : i ])
2931
}
3032
},
3133
function () {
32-
if(soFar != null)
33-
this.queue(soFar)
34+
if(last && soFar == '')
35+
return this.queue(null)
36+
else (soFar != null) // && (last && soFar != ''))
37+
map(this, soFar)
38+
3439
this.queue(null)
3540
})
3641
}
3742

43+
44+
45+

0 commit comments

Comments
 (0)