gajus / gajus/table

Suggestion: node Writable stream wrapper around table.createStream

Open
#86 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
973
Forks
81
PR merge metrics
No merged PRs in 30d

Description

Opening as issue because I am not sure where the code should land.

Shipping a writable wrapper is easy as defining `_write` and `_final` methods.

```javascript
const {Writable} = require('stream'); // or "readable-stream" if you prefer

function WriteTableStream(options) {
if (!(this instanceof WriteTableStream)) {
return new WriteTableStream(options);
}

options = Object.assign({objectMode: true}, options);
Writable.call(this, options);
this._tableStream = createStream(options);
}

// or require('util').inherits if you prefer. Honestly, I hate that Object.setPrototypeOf is used there
WriteTableStream.super_ = Writable;
WriteTableStream.prototype = Object.create(Writable.prototype, {
constructor: {
configurable: true,
enumerable: false,
value: WriteTableStream,
writable: true
}
});

WriteTableStream.prototype._write = function(chunk, encoding, next) {
let err = null;

try {
this._tableStream.write(chunk);
} catch (e) {
err = e;
}

next(err);
};

WriteTableStream.prototype._final = function(done) {
process.stdout.write('\n', done);
};
```

This allows users to create a pipeline that transforms their data into row arrays. For example maybe read a csv file in a stream.

```javascript
fs.createReadStream('data.csv')
// https://github.com/mcollina/split2
.pipe(split(line => line.split(','), {trailing: false}))
.pipe(new WriteTableStream(options));
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.