Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ var os = require('os')

module.exports = BinarySplit

function BinarySplit (matcher) {
if (!(this instanceof BinarySplit)) return new BinarySplit(matcher)
matcher = Buffer(matcher || os.EOL)
function BinarySplit (splitOn) {
if (!(this instanceof BinarySplit)) return new BinarySplit(splitOn)
splitOn = splitOn || os.EOL
var matcher = Buffer.from && Buffer.from !== Uint8Array.from
? Buffer.from(splitOn)
: new Buffer(splitOn)
var buffered
return through(write, end)

Expand Down
18 changes: 12 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var test = require('tape')
var fs = require('fs')
var split = require('./')

var bufferFrom = Buffer.from && Buffer.from !== Uint8Array.from

function splitTest (matcher, cb) {
if (!cb) {
cb = matcher
Expand Down Expand Up @@ -35,27 +37,31 @@ test('custom matcher', function (t) {
t.equals(items.length, 5)
t.equals(items.join(' '), 'hello yes this is dog')
t.end()
});

['hello yes ', 'this', ' is d', 'og'].map(function (chunk) {
return bufferFrom ? Buffer.from(chunk) : new Buffer(chunk)
}).forEach(function (chunk) {
splitStream.write(chunk)
})
splitStream.write(new Buffer('hello yes '))
splitStream.write(new Buffer('this'))
splitStream.write(new Buffer(' is d'))
splitStream.write(new Buffer('og'))
splitStream.end()
})

test('long matcher', function (t) {
var data = 'hello yes this is dog'
var splitStream = splitTest('this', function (err, items) {
if (err) throw err
t.equals(items.length, 2)
t.equals(items[0].toString(), 'hello yes ')
t.equals(items[1].toString(), ' is dog')
t.end()
})
splitStream.write(new Buffer('hello yes this is dog'))
splitStream.write(bufferFrom ? Buffer.from(data) : new Buffer(data))
splitStream.end()
})

test('matcher at index 0 check', function (t) {
var data = '\nhello\nmax'
var splitStream = splitTest(function (err, items) {
if (err) throw err

Expand All @@ -65,7 +71,7 @@ test('matcher at index 0 check', function (t) {
t.end()
})

splitStream.write(new Buffer('\nhello\nmax'))
splitStream.write(bufferFrom ? Buffer.from(data) : new Buffer(data))
splitStream.end()
})

Expand Down