split2
Advanced tools
Comparing version 1.1.1 to 2.0.0
48
index.js
@@ -17,29 +17,28 @@ /* | ||
'use strict'; | ||
'use strict' | ||
var through = require('through2') | ||
var StringDecoder = require('string_decoder').StringDecoder | ||
function transform(chunk, enc, cb) { | ||
var list = chunk.toString('utf8').split(this.matcher) | ||
, remaining = list.pop() | ||
, i | ||
function transform (chunk, enc, cb) { | ||
this._last += this._decoder.write(chunk) | ||
if (list.length >= 1) { | ||
push(this, this.mapper((this._last + list.shift()))) | ||
} else { | ||
remaining = this._last + remaining | ||
} | ||
var list = this._last.split(this.matcher) | ||
for (i = 0; i < list.length; i++) { | ||
this._last = list.pop() | ||
for (var i = 0; i < list.length; i++) { | ||
push(this, this.mapper(list[i])) | ||
} | ||
this._last = remaining | ||
cb() | ||
} | ||
function flush(cb) { | ||
if (this._last) | ||
function flush (cb) { | ||
// forward any gibberish left in there | ||
this._last += this._decoder.end() | ||
if (this._last) { | ||
push(this, this.mapper(this._last)) | ||
} | ||
@@ -49,13 +48,13 @@ cb() | ||
function push(self, val) { | ||
if (val !== undefined) | ||
function push (self, val) { | ||
if (val !== undefined) { | ||
self.push(val) | ||
} | ||
} | ||
function noop(incoming) { | ||
function noop (incoming) { | ||
return incoming | ||
} | ||
function split(matcher, mapper, options) { | ||
function split (matcher, mapper, options) { | ||
// Set defaults for any arguments not supplied. | ||
@@ -73,5 +72,4 @@ matcher = matcher || /\r?\n/ | ||
matcher = /\r?\n/ | ||
} | ||
// If options is only argument. | ||
else if (typeof matcher === 'object' && !(matcher instanceof RegExp)) { | ||
} else if (typeof matcher === 'object' && !(matcher instanceof RegExp)) { | ||
options = matcher | ||
@@ -88,5 +86,4 @@ matcher = /\r?\n/ | ||
matcher = /\r?\n/ | ||
} | ||
// If matcher and options are arguments. | ||
else if (typeof mapper === 'object') { | ||
} else if (typeof mapper === 'object') { | ||
options = mapper | ||
@@ -100,5 +97,6 @@ mapper = noop | ||
// this stream is in objectMode only in the readable part | ||
stream._readableState.objectMode = true; | ||
stream._readableState.objectMode = true | ||
stream._last = '' | ||
stream._decoder = new StringDecoder('utf8') | ||
stream.matcher = matcher | ||
@@ -105,0 +103,0 @@ stream.mapper = mapper |
{ | ||
"name": "split2", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "split a Text Stream into a Line Stream, using Stream 3", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tap test.js" | ||
"test": "standard && tap -b test.js" | ||
}, | ||
@@ -23,5 +23,7 @@ "pre-commit": [ | ||
"devDependencies": { | ||
"tap": "~0.4.12", | ||
"pre-commit": "0.0.9", | ||
"callback-stream": "~1.0.2" | ||
"callback-stream": "^1.1.0", | ||
"fastbench": "^1.0.0", | ||
"pre-commit": "^1.1.2", | ||
"standard": "^5.4.1", | ||
"tap": "^5.0.0" | ||
}, | ||
@@ -28,0 +30,0 @@ "dependencies": { |
# Split2(matcher, mapper, options) | ||
[![build status](https://secure.travis-ci.org/mcollina/split2.png)](http://travis-ci.org/mcollina/split2) | ||
[![build status](https://secure.travis-ci.org/mcollina/split2.svg)](http://travis-ci.org/mcollina/split2) | ||
@@ -5,0 +5,0 @@ Break up a stream and reassemble it so that each line is a chunk. |
176
test.js
@@ -0,14 +1,16 @@ | ||
'use strict' | ||
var test = require('tap').test | ||
, split = require('./') | ||
, callback = require('callback-stream') | ||
, strcb = callback.bind(null, { decodeStrings: false }) | ||
, objcb = callback.bind(null, { objectMode: true }) | ||
var test = require('tap').test | ||
var split = require('./') | ||
var callback = require('callback-stream') | ||
var strcb = callback.bind(null, { decodeStrings: false }) | ||
var objcb = callback.bind(null, { objectMode: true }) | ||
test('split two lines on end', function(t) { | ||
t.plan(1) | ||
test('split two lines on end', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -20,8 +22,9 @@ })) | ||
test('split two lines on two writes', function(t) { | ||
t.plan(1) | ||
test('split two lines on two writes', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -35,8 +38,9 @@ })) | ||
test('accumulate multiple writes', function(t) { | ||
t.plan(1) | ||
test('accumulate multiple writes', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['helloworld']) | ||
@@ -50,8 +54,9 @@ })) | ||
test('split using a custom string matcher', function(t) { | ||
t.plan(1) | ||
test('split using a custom string matcher', function (t) { | ||
t.plan(2) | ||
var input = split('~') | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -63,8 +68,9 @@ })) | ||
test('split using a custom regexp matcher', function(t) { | ||
t.plan(1) | ||
test('split using a custom regexp matcher', function (t) { | ||
t.plan(2) | ||
var input = split(/~/) | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -76,3 +82,3 @@ })) | ||
test('support an option argument', function(t) { | ||
test('support an option argument', function (t) { | ||
t.plan(2) | ||
@@ -82,4 +88,4 @@ | ||
input.pipe(strcb(function(err, list) { | ||
t.notOk(err, 'no errors') | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -91,11 +97,12 @@ })) | ||
test('support a mapper function', function(t) { | ||
test('support a mapper function', function (t) { | ||
t.plan(2) | ||
var a = { a: '42' } | ||
, b = { b: '24' } | ||
var b = { b: '24' } | ||
var input = split(JSON.parse) | ||
input.pipe(objcb(function(err, list) { | ||
t.notOk(err, 'no errors') | ||
input.pipe(objcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, [a, b]) | ||
@@ -109,8 +116,9 @@ })) | ||
test('split lines windows-style', function(t) { | ||
t.plan(1) | ||
test('split lines windows-style', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -122,8 +130,9 @@ })) | ||
test('splits a buffer', function(t) { | ||
t.plan(1) | ||
test('splits a buffer', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -135,8 +144,9 @@ })) | ||
test('do not end on undefined', function(t) { | ||
t.plan(1) | ||
test('do not end on undefined', function (t) { | ||
t.plan(2) | ||
var input = split(function(line) {}) | ||
var input = split(function (line) {}) | ||
input.pipe(strcb(function(err, list) { | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, []) | ||
@@ -148,8 +158,8 @@ })) | ||
test('has destroy method', function(t) { | ||
test('has destroy method', function (t) { | ||
t.plan(1) | ||
var input = split(function(line) {}) | ||
var input = split(function (line) {}) | ||
input.on('close', function() { | ||
input.on('close', function () { | ||
t.ok(true, 'close emitted') | ||
@@ -162,7 +172,7 @@ t.end() | ||
test('support custom matcher and mapper', function(t) { | ||
test('support custom matcher and mapper', function (t) { | ||
t.plan(4) | ||
var a = { a: '42' } | ||
, b = { b: '24' } | ||
var b = { b: '24' } | ||
var input = split('~', JSON.parse) | ||
@@ -173,3 +183,3 @@ | ||
input.pipe(objcb(function(err, list) { | ||
input.pipe(objcb(function (err, list) { | ||
t.notOk(err, 'no errors') | ||
@@ -184,3 +194,3 @@ t.deepEqual(list, [a, b]) | ||
test('support custom matcher and options', function(t) { | ||
test('support custom matcher and options', function (t) { | ||
t.plan(6) | ||
@@ -195,4 +205,4 @@ | ||
input.pipe(strcb(function(err, list) { | ||
t.notOk(err, 'no errors') | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
@@ -204,7 +214,7 @@ })) | ||
test('support mapper and options', function(t) { | ||
test('support mapper and options', function (t) { | ||
t.plan(6) | ||
var a = { a: '42' } | ||
, b = { b: '24' } | ||
var b = { b: '24' } | ||
var input = split(JSON.parse, { highWaterMark: 1024 }) | ||
@@ -217,4 +227,4 @@ | ||
input.pipe(objcb(function(err, list) { | ||
t.notOk(err, 'no errors') | ||
input.pipe(objcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, [a, b]) | ||
@@ -227,1 +237,67 @@ })) | ||
}) | ||
test('split utf8 chars', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['烫烫烫', '锟斤拷']) | ||
})) | ||
var buf = new Buffer('烫烫烫\r\n锟斤拷', 'utf8') | ||
for (var i = 0; i < buf.length; ++i) { | ||
input.write(buf.slice(i, i + 1)) | ||
} | ||
input.end() | ||
}) | ||
test('split utf8 chars 2by2', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['烫烫烫', '烫烫烫']) | ||
})) | ||
var str = '烫烫烫\r\n烫烫烫' | ||
var buf = new Buffer(str, 'utf8') | ||
for (var i = 0; i < buf.length; i += 2) { | ||
input.write(buf.slice(i, i + 2)) | ||
} | ||
input.end() | ||
}) | ||
test('split lines when the \n comes at the end of a chunk', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['hello', 'world']) | ||
})) | ||
input.write('hello\n') | ||
input.end('world') | ||
}) | ||
test('truncated utf-8 char', function (t) { | ||
t.plan(2) | ||
var input = split() | ||
input.pipe(strcb(function (err, list) { | ||
t.error(err) | ||
t.deepEqual(list, ['烫' + new Buffer('e7', 'hex').toString()]) | ||
})) | ||
var str = '烫烫' | ||
var buf = new Buffer(str, 'utf8') | ||
input.write(buf.slice(0, 3)) | ||
input.end(buf.slice(3, 4)) | ||
}) |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12846
8
302
5
1