Skip to content
Open
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
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class NewlineTransformer extends Transform {
_transform(chunk: any, encoding: string, callback: TransformCallback) {
let data: string = chunk.toString();
if (this._lastLineData) data = this._lastLineData + data;
const lines = data.split(newline);
const lines = data.split(/\r?\n/);
this._lastLineData = lines.pop();
lines.forEach(this.push.bind(this));
callback();
Expand Down
24 changes: 23 additions & 1 deletion test/test-python-shell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as should from 'should';
import { PythonShell } from '..';
import { PythonShell, NewlineTransformer } from '..';
import { sep, join } from 'path';
import { EOL as newline } from 'os';
import { chdir, cwd } from 'process';
Expand Down Expand Up @@ -642,3 +642,25 @@ describe('PythonShell', function () {
});
});
});

describe('NewlineTransformer', function () {
function collect(input: Buffer): Promise<string[]> {
return new Promise((resolve) => {
const t = new NewlineTransformer();
t.setEncoding('utf8');
const out: string[] = [];
t.on('data', (chunk) => out.push(chunk.toString()));
t.on('end', () => resolve(out));
t.write(input);
t.end();
});
}

it('splits on \\n regardless of the platform line ending', async function () {
(await collect(Buffer.from('hello\nworld\n'))).should.eql(['hello', 'world']);
});

it('splits on \\r\\n', async function () {
(await collect(Buffer.from('hello\r\nworld\r\n'))).should.eql(['hello', 'world']);
});
});