-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
41 lines (36 loc) · 894 Bytes
/
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
'use strict'
var through = require('pull-through')
module.exports = function split (matcher, mapper, reverse, last) {
var soFar = ''
if('function' === typeof matcher)
mapper = matcher, matcher = null
if (!matcher)
matcher = '\n'
function map(stream, piece) {
if(mapper) {
piece = mapper(piece)
if('undefined' !== typeof piece)
stream.queue(piece)
}
else
stream.queue(piece)
}
return through(function (buffer) {
var stream = this
, pieces = ( reverse
? buffer + soFar
: soFar + buffer
).split(matcher)
soFar = reverse ? pieces.shift() : pieces.pop()
var l = pieces.length
for (var i = 0; i < l; i++) {
map(stream, pieces[reverse ? l - 1 - i : i ])
}
},
function () {
if(last && soFar == '')
return this.queue(null)
map(this, soFar)
this.queue(null)
})
}