helpers.js 500 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. const {Readable, Writable} = require('stream');
  3. const streamFromArray = array =>
  4. new Readable({
  5. objectMode: true,
  6. read() {
  7. if (isNaN(this.index)) this.index = 0;
  8. this.push(this.index < array.length ? array[this.index++] : null);
  9. }
  10. });
  11. const streamToArray = array =>
  12. new Writable({
  13. objectMode: true,
  14. write(chunk, encoding, callback) {
  15. array.push(chunk);
  16. callback(null);
  17. }
  18. });
  19. module.exports = {streamFromArray, streamToArray};