# Documentation ### Table of Contents - [Stream][1] - [wrap][2] - [promisify][3] - [readableFrom][4] - [pipeline][5] - [get][6] - [map][7] - [tap][8] - [filter][9] - [reject][10] - [batch][11] - [reduce][12] - [toPromise][13] - [take][14] ## Stream **Extends Events** Creates an instance of hl-stream. Take as argument and Array, ReadableStream, Generator function, Instance of generator function and Iterable. **Parameters** - `src` **(ReadableStream | [Array][15] | Generator | Iterable)** **Examples** ```javascript const _ = require('hl-stream') const R = require('ramda') const value = await _([1, 2]).map(R.add(1)).reduce(0, R.add).toPromise(Promise) // => 5 // or const value = await _.pipeline( _.map(R.add(1)), _.reduce(0, R.add), _.toPromise(Promise) )([1, 2]) // => 5 ``` ## wrap Return a new instance of WrappedStream. **Parameters** - `src` **ReadableStream** - `args` **([String][16] \| [Array][15])** **Examples** ```javascript const wrappedStream = _.wrap(readableStream) const result = wrappedStream.map(add(1)).reduce(0, add).toPromise(Promise) ``` Returns **WrappedStream** Wrapped Stream. ## promisify Promisify a node-style callback function. **Parameters** - `fn` **[Function][17]** - `PromiseConstructor` **[Function][17]** (optional, default `Promise`) **Examples** ```javascript const wrappedFn = _.promisify(nodeStyleFn) wrappedFn().then(console.log) // => wrappedFn return promise ``` Returns **[Function][17]** Promisified function ## readableFrom Create an new instance of ReadableStream. **Parameters** - `src` **([Array][15] | Iterator | ReadableStream | [Function][17])** **Examples** ```javascript const readableStreamFromArray = _.readableFrom([1, 2, 3, 4]) // => 1, 2, 3, 4 const readableStreamGeneratorFunction = _.readableFrom(function * () { yield 2 }) // => 2 const readableStreamFromIterator _.readableFrom(new Set(['value1', 'value2', 'value3'])) // => 'value1', 'value2', 'value3' ``` Returns **ReadableStream** It will emit all the data of src. ## pipeline Creates a function that when executed returns a stream composed by the steps of the pipeline. **Parameters** - `transforms` **...[Function][17]** **Examples** ```javascript const addTwoAnFilterPairs = _.pipeline( _.map(add(2)), _.filter(pairs) ) addTwoAnFilterPairs([1, 2, 3, 4]) // => 4, 6 ``` Returns **[Function][17]** When is executed it returns a Transform Stream composed by the steps of the pipeline. ## get Returns the stream composed of the function chain. The chaining of functions is lazy and is only done by calling the get method. **Examples** ```javascript const stream = _([1, 2, 3, 4]).map(double).filter(isPair) stream.get() // => 2, 4, 6, 8 ``` Returns **TransformStream** The stream composed ## map Creates a new Stream of transformed values by applying a function to each value from the source. **Parameters** - `fn` **[Function][17]** **Examples** ```javascript const double = (n) => n * 2 _([1, 2, 3, 4]).map(double) // => 2, 4, 6, 8 // or _.map(double, [1, 2, 3, 4]) // => 2, 4, 6, 8 // Use promises _([1, 2, 3, 4]).map((data) => Promise.resolve(data)) // => 1, 2, 3, 4 _.map((data) => Promise.resolve(data), [1, 2, 3, 4]) // => 1, 2, 3, 4 ``` Returns **TransformStream** ## tap Runs the given function with the supplied object, then returns the object. **Parameters** - `fn` **[Function][17]** **Examples** ```javascript _([1, 2, 3, 4]).tap(console.log) // => 1, 2, 3, 4, in console 1, 2, 3, 4 // or _.tap(console.log, [1, 2, 3, 4]) // => 1, 2, 3, 4, in console 1, 2, 3, 4 ``` ## filter Takes a predicate and create a new stream with the members of the given filterable which satisfy the given predicate. **Parameters** - `predicate` **[Function][17]** **Examples** ```javascript const isPair = (n) => n % 2 === 0 _([1, 2, 3, 4]).filter(isPair) // => 2, 4 // or _.filter(isPair, [1, 2, 3, 4]) // => 2, 4 ``` Returns **TransformStream** Stream with the filtered objects. ## reject The complement of filter. Removes every element in the stream that complies with the predicate **Parameters** - `predicate` **[Function][17]** **Examples** ```javascript const isPair = (n) => n % 2 === 0 _([1, 2, 3, 4]).reject(isPairs) // => 1, 3 // Or _.filter(isPairs, [1, 2, 3, 4]) // => 1, 3 ``` Returns **TransformStream** Stream with not rejected items ## batch Takes one Stream and batches incoming data into arrays of given length. **Parameters** - `size` **[Number][18]** **Examples** ```javascript _([1, 2, 3, 4, 5]).batch(2) // => [1, 2], [3, 4], [5] // or _.batch(2, [1, 2, 3, 4, 5]) // => [1, 2], [3, 4], [5] ``` Returns **TransformStream** ## reduce Boils down a Stream to a single value. **Parameters** - `initial` **any** - `fn` **[Function][17]** **Examples** ```javascript _([1, 2, 3, 4]).reduce(add) // => 10 // or _.reduce(add, [1, 2, 3, 4]) // => 10 ``` Returns **TransformStream** Stream with the reduced value. ## toPromise Converts the result of a stream to Promise. **Parameters** - `PromiseConstructor` **[Function][17]** **Examples** ```javascript _([1, 2, 3, 4]).reduce(0, add).toPromise(Promise).then(function (result) { // => 10 }) // or _.toPromise(Promise, [1, 2, 3, 5]).then(function (result) { // => [1, 2, 3, 5] }) ``` Returns **[Promise][19]** result ## take Take n items from readable stream and destroy the stream source. **Parameters** - `n` **[Number][18]** **Examples** ```javascript _([1, 2, 3, 4]).take(2) // => 1, 2 // or _.take(2, [1, 2, 3, 4]) ``` Returns **ReadableStream** [1]: #stream [2]: #wrap [3]: #promisify [4]: #readablefrom [5]: #pipeline [6]: #get [7]: #map [8]: #tap [9]: #filter [10]: #reject [11]: #batch [12]: #reduce [13]: #topromise [14]: #take [15]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array [16]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function [18]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number [19]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise