Skip to content

Commit

Permalink
Fix broken transform stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ullenius committed Jul 9, 2022
1 parent 47bd0b8 commit 97790ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $ node rle-decode.js < rle-encoded.txt
## Running tests
Run `run-tests.sh` bash script to invoke the unit tests.

They are written using my port of `jstinytest` which is bundled
They are written using my fork of `jstinytest` which is bundled
inside `tests/tinytest.js`.

## Licence
Expand Down
5 changes: 3 additions & 2 deletions rle-decode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var transformStream= require("./transform-stream.js");
var transformStream = require("./transform-stream.js");

/*
* run length decoding JS
Expand All @@ -18,4 +18,5 @@ function decode(data) {
return result;
}

process.stdin.pipe(transformStream).pipe(process.stdout);
var transform = transformStream.create( decode );
process.stdin.pipe(transform).pipe(process.stdout);
3 changes: 2 additions & 1 deletion rle-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ function prettyPrint(rleData) {
return [...rleData].join("");
}

process.stdin.pipe(transformStream).pipe(process.stdout);
var transform = transformStream.create( encode );
process.stdin.pipe(transform).pipe(process.stdout);
17 changes: 11 additions & 6 deletions transform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

var stream = require("stream");

var transformStream = new stream.Transform();
transformStream._transform = function transform(chunk, encoding, callback) {
transformStream.push( encode(chunk.toString()) );
callback();
};
// encode/decode-function passed in
// strategy pattern, GoF
function create( func ) {
var transformStream = new stream.Transform();
transformStream._transform = function transform(chunk, encoding, callback) {
transformStream.push( func(chunk.toString()) );
callback();
};
return transformStream;
}

module.exports = transformStream;
module.exports = { create };

0 comments on commit 97790ee

Please sign in to comment.