split2
Advanced tools
Comparing version 3.2.2 to 4.0.0
10
bench.js
'use strict' | ||
var split = require('./') | ||
var bench = require('fastbench') | ||
var binarySplit = require('binary-split') | ||
var fs = require('fs') | ||
const split = require('./') | ||
const bench = require('fastbench') | ||
const binarySplit = require('binary-split') | ||
const fs = require('fs') | ||
@@ -22,3 +22,3 @@ function benchSplit (cb) { | ||
var run = bench([ | ||
const run = bench([ | ||
benchSplit, | ||
@@ -25,0 +25,0 @@ benchBinarySplit |
23
index.js
/* | ||
Copyright (c) 2014-2018, Matteo Collina <hello@matteocollina.com> | ||
Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com> | ||
@@ -19,3 +19,3 @@ Permission to use, copy, modify, and/or distribute this software for any | ||
const { Transform } = require('readable-stream') | ||
const { Transform } = require('stream') | ||
const { StringDecoder } = require('string_decoder') | ||
@@ -26,5 +26,5 @@ const kLast = Symbol('last') | ||
function transform (chunk, enc, cb) { | ||
var list | ||
let list | ||
if (this.overflow) { // Line buffer is full. Skip to start of next line. | ||
var buf = this[kDecoder].write(chunk) | ||
const buf = this[kDecoder].write(chunk) | ||
list = buf.split(this.matcher) | ||
@@ -44,3 +44,3 @@ | ||
for (var i = 0; i < list.length; i++) { | ||
for (let i = 0; i < list.length; i++) { | ||
try { | ||
@@ -54,3 +54,6 @@ push(this, this.mapper(list[i])) | ||
this.overflow = this[kLast].length > this.maxLength | ||
if (this.overflow && !this.skipOverflow) return cb(new Error('maximum buffer reached')) | ||
if (this.overflow && !this.skipOverflow) { | ||
cb(new Error('maximum buffer reached')) | ||
return | ||
} | ||
@@ -119,2 +122,3 @@ cb() | ||
options = Object.assign({}, options) | ||
options.autoDestroy = true | ||
options.transform = transform | ||
@@ -131,4 +135,9 @@ options.flush = flush | ||
stream.maxLength = options.maxLength | ||
stream.skipOverflow = options.skipOverflow | ||
stream.skipOverflow = options.skipOverflow || false | ||
stream.overflow = false | ||
stream._destroy = function (err, cb) { | ||
// Weird Node v12 bug that we need to work around | ||
this._writableState.errorEmitted = false | ||
cb(err) | ||
} | ||
@@ -135,0 +144,0 @@ return stream |
{ | ||
"name": "split2", | ||
"version": "3.2.2", | ||
"version": "4.0.0", | ||
"description": "split a Text Stream into a Line Stream, using Stream 3", | ||
@@ -33,9 +33,5 @@ "main": "index.js", | ||
"pre-commit": "^1.1.2", | ||
"safe-buffer": "^5.1.1", | ||
"standard": "^14.0.0", | ||
"standard": "^16.0.1", | ||
"tape": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"readable-stream": "^3.0.0" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # Split2(matcher, mapper, options) | ||
and it is totally API compatible with it. | ||
However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) via [`readable-stream`](https://github.com/nodejs/readable-stream) | ||
However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options). | ||
@@ -72,17 +72,5 @@ `matcher` may be a `String`, or a `RegExp`. Example, read every line in a file ... | ||
# Benchmark | ||
```bash | ||
$ node bench.js | ||
benchSplit*10000: 1484.983ms | ||
benchBinarySplit*10000: 1484.080ms | ||
benchSplit*10000: 1407.334ms | ||
benchBinarySplit*10000: 1500.281ms | ||
``` | ||
Benchmark taken on Node 8.11.3, on a Macbook i5 2018. | ||
# License | ||
Copyright (c) 2014-2018, Matteo Collina <hello@matteocollina.com> | ||
Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com> | ||
@@ -89,0 +77,0 @@ Permission to use, copy, modify, and/or distribute this software for any |
104
test.js
'use strict' | ||
var test = require('tape') | ||
var split = require('./') | ||
var callback = require('callback-stream') | ||
var Buffer = require('safe-buffer').Buffer | ||
var strcb = callback.bind(null, { decodeStrings: false }) | ||
var objcb = callback.bind(null, { objectMode: true }) | ||
const test = require('tape') | ||
const split = require('./') | ||
const callback = require('callback-stream') | ||
const strcb = callback.bind(null, { decodeStrings: false }) | ||
const objcb = callback.bind(null, { objectMode: true }) | ||
@@ -13,3 +12,3 @@ test('split two lines on end', function (t) { | ||
var input = split() | ||
const input = split() | ||
@@ -27,3 +26,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -43,3 +42,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -60,3 +59,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -76,3 +75,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split('~') | ||
const input = split('~') | ||
@@ -90,3 +89,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split(/~/) | ||
const input = split(/~/) | ||
@@ -104,3 +103,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split({ highWaterMark: 2 }) | ||
const input = split({ highWaterMark: 2 }) | ||
@@ -118,6 +117,6 @@ input.pipe(strcb(function (err, list) { | ||
var a = { a: '42' } | ||
var b = { b: '24' } | ||
const a = { a: '42' } | ||
const b = { b: '24' } | ||
var input = split(JSON.parse) | ||
const input = split(JSON.parse) | ||
@@ -137,3 +136,3 @@ input.pipe(objcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -151,3 +150,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -165,3 +164,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split(function (line) { }) | ||
const input = split(function (line) { }) | ||
@@ -179,3 +178,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split(function (line) { }) | ||
const input = split(function (line) { }) | ||
@@ -193,5 +192,5 @@ input.on('close', function () { | ||
var a = { a: '42' } | ||
var b = { b: '24' } | ||
var input = split('~', JSON.parse) | ||
const a = { a: '42' } | ||
const b = { b: '24' } | ||
const input = split('~', JSON.parse) | ||
@@ -214,3 +213,3 @@ t.equal(input.matcher, '~') | ||
var input = split('~', { highWaterMark: 1024 }) | ||
const input = split('~', { highWaterMark: 1024 }) | ||
@@ -233,5 +232,5 @@ t.equal(input.matcher, '~') | ||
var a = { a: '42' } | ||
var b = { b: '24' } | ||
var input = split(JSON.parse, { highWaterMark: 1024 }) | ||
const a = { a: '42' } | ||
const b = { b: '24' } | ||
const input = split(JSON.parse, { highWaterMark: 1024 }) | ||
@@ -256,3 +255,3 @@ t.ok(input.matcher instanceof RegExp, 'matcher is RegExp') | ||
var input = split() | ||
const input = split() | ||
@@ -264,4 +263,4 @@ input.pipe(strcb(function (err, list) { | ||
var buf = Buffer.from('烫烫烫\r\n锟斤拷', 'utf8') | ||
for (var i = 0; i < buf.length; ++i) { | ||
const buf = Buffer.from('烫烫烫\r\n锟斤拷', 'utf8') | ||
for (let i = 0; i < buf.length; ++i) { | ||
input.write(buf.slice(i, i + 1)) | ||
@@ -275,3 +274,3 @@ } | ||
var input = split() | ||
const input = split() | ||
@@ -283,5 +282,5 @@ input.pipe(strcb(function (err, list) { | ||
var str = '烫烫烫\r\n烫烫烫' | ||
var buf = Buffer.from(str, 'utf8') | ||
for (var i = 0; i < buf.length; i += 2) { | ||
const str = '烫烫烫\r\n烫烫烫' | ||
const buf = Buffer.from(str, 'utf8') | ||
for (let i = 0; i < buf.length; i += 2) { | ||
input.write(buf.slice(i, i + 2)) | ||
@@ -295,3 +294,3 @@ } | ||
var input = split() | ||
const input = split() | ||
@@ -310,3 +309,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split() | ||
const input = split() | ||
@@ -318,4 +317,4 @@ input.pipe(strcb(function (err, list) { | ||
var str = '烫烫' | ||
var buf = Buffer.from(str, 'utf8') | ||
const str = '烫烫' | ||
const buf = Buffer.from(str, 'utf8') | ||
@@ -329,8 +328,9 @@ input.write(buf.slice(0, 3)) | ||
var input = split({ maxLength: 2 }) | ||
input.pipe(strcb(function (err, list) { | ||
const input = split({ maxLength: 2 }) | ||
input.on('error', function (err) { | ||
t.ok(err) | ||
})) | ||
}) | ||
input.resume() | ||
input.write('hey') | ||
@@ -340,3 +340,3 @@ }) | ||
test('readable highWaterMark', function (t) { | ||
var input = split() | ||
const input = split() | ||
t.equal(input._readableState.highWaterMark, 16) | ||
@@ -349,3 +349,3 @@ t.end() | ||
var input = split({ maxLength: 2 }) | ||
const input = split({ maxLength: 2 }) | ||
@@ -363,3 +363,3 @@ input.pipe(strcb(function (err, list) { | ||
var input = split({ maxLength: 2, skipOverflow: true }) | ||
const input = split({ maxLength: 2, skipOverflow: true }) | ||
@@ -380,4 +380,4 @@ input.pipe(strcb(function (err, list) { | ||
var options = {} | ||
var input = split(options) | ||
const options = {} | ||
const input = split(options) | ||
@@ -394,4 +394,4 @@ input.pipe(strcb(function (err, list) { | ||
t.plan(1) | ||
var error = new Error() | ||
var input = split(function () { | ||
const error = new Error() | ||
const input = split(function () { | ||
throw error | ||
@@ -407,6 +407,6 @@ }) | ||
test('mapper throws on transform', function (t) { | ||
t.plan(2) | ||
t.plan(1) | ||
var error = new Error() | ||
var input = split(function (l) { | ||
const error = new Error() | ||
const input = split(function (l) { | ||
throw error | ||
@@ -413,0 +413,0 @@ }) |
0
7
427
17066
86
- Removedreadable-stream@^3.0.0
- Removedinherits@2.0.4(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)