index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict'
  2. var Readable = require('readable-stream').Readable
  3. ;
  4. /**
  5. * Create a new instance of StreamArray
  6. *
  7. * @access private
  8. * @param {Array} list
  9. */
  10. function StreamArray(list) {
  11. if (!Array.isArray(list))
  12. throw new TypeError('First argument must be an Array');
  13. Readable.call(this, {objectMode:true});
  14. this._i = 0;
  15. this._l = list.length;
  16. this._list = list;
  17. }
  18. StreamArray.prototype = Object.create(Readable.prototype, {constructor: {value: StreamArray}});
  19. /**
  20. * Read the next item from the source Array and push into NodeJS stream
  21. * @access protected
  22. * @desc Read the next item from the source Array and push into NodeJS stream
  23. * @param {number} size The amount of data to read (ignored)
  24. */
  25. StreamArray.prototype._read = function(size) {
  26. this.push(this._i < this._l ? this._list[this._i++] : null);
  27. };
  28. /**
  29. * Create a new instance of StreamArray
  30. *
  31. * @module stream-array
  32. * @desc Push Array elements through a NodeJS stream
  33. * @type {function}
  34. * @param {Array} list An Array of objects, strings, numbers, etc
  35. */
  36. module.exports = function(list) {
  37. return new StreamArray(list);
  38. };