Skip to content

Commit

Permalink
readable documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
atiertant committed Apr 13, 2018
1 parent e25c6b7 commit e5925b2
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,99 @@ stream helper for NodeJs
[![NSP Status](https://nodesecurity.io/orgs/atlantis/projects/ed0ada30-0689-4121-b3b8-9d80f793d292/badge)](https://nodesecurity.io/orgs/atlantis/projects/ed0ada30-0689-4121-b3b8-9d80f793d292)
[![Dependencies Status](https://david-dm.org/Atlantis-Software/streamize.svg)](https://david-dm.org/Atlantis-Software/streamize)

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/).

Before installing, [download and install Node.js](https://nodejs.org/en/download/).
Node.js 6 or higher is required.

Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```bash
$ npm install streamize
```

## Documentation

### Readable

Description: create a readable stream

`streamize.Readable(read, [options])`
* read is a function that take a callback function as argument.
* options is an optional object argument passed to the node stream: [see node readable stream documentation](https://nodejs.org/api/stream.html#stream_new_stream_readable_options)

return a readable stream

#### sample
```javascript
var streamize = require('streamize');
var myArray = ['1', '2', '3', '4', '5'];
// create a readable stream from myArray
var readable = streamize.Readable(function(cb) {
cb(null, myArray.shift() || null);
});
```

the read function passed in argument is called each time a new chunk is required.
the callback takes 2 arguments:
* error

When error is defined and not null, the stream returned emit the error and chunk is ignored.
* chunk

When chunk is a Buffer, Uint8Array or string, the chunk of data will be added to the internal queue for users of the stream to consume.
Passing chunk as null signals the end of the stream (EOF), after which no more data can be written.

As read function is called in the stream context, every internal function could be called from `this`.

```javascript
var readable = streamize.Readable(function(cb) {
// push 'a' into the stream
this.push('a');
cb(null, myArray.shift() || null);
});
```

### Writeable
#### sample
```javascript
var streamize = require('streamize');
var newArray = [];
// create a writable stream that push chunk in newArray
var writable = streamize.Writable(function(chunk, cb) {
newArray.push(chunk);
 cb();
});
```

### Duplex
#### sample
```javascript
var streamize = require('streamize');
var myArray = ['1', '2', '3', '4', '5'];
var newArray = [];
// create a duplex stream that read myArray and write in newArray
var duplex = streamize.Duplex(function(cb) {
cb(null, myArray.shift() || null);
}, function(data, cb) {
 newArray.push(chunk);
 cb();
});
```

### Transform
#### sample
```javascript
var streamize = require('streamize');
// create a transform stream that duplicate all chunks
var transform = streamize.Transform(function(data, cb) {
this.push(data);
cb(null, data);
});

### obj.*

0 comments on commit e5925b2

Please sign in to comment.