Source: utils/getReadableStream.js

'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.getReadableStream = getReadableStream;
var Readable = require('stream').Readable;

/**
 * Returns a readable stream which outputs each one of the Harray elements.
 * @see {@link https://nodejs.org/api/stream.html| NodeJS Streams Documentation}
 * @name Harray#getReadableStream
 * @method
 * @param {Number} [startIndex] - The index to start from when outputting content. Defaults to 0.
 * @example
 * // Prints an odd number to the console every two seconds
 * let harr = new Harray(1, 3);
 * let positiveOddsStream = harr.getReadableStream();
 *
 * positiveOddsStream.on('data', function(chunk) {
 *      positiveOddsStream.pause();
 *      console.log(chunk.toString());
 *      setTimeout(function() {
 *          positiveOddsStream.resume();
 *      }, 2000);
 * });
 */

function getReadableStream(startIndex) {
    var _this = this;

    var rs = Readable();

    var i = typeof startIndex === 'number' ? startIndex : 0;

    rs._read = function () {
        rs.push(new Buffer('' + _this.get(i++)));
    };

    return rs;
}