connect.asynct.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var es = require('../')
  2. , it = require('it-is').style('colour')
  3. , d = require('ubelt')
  4. function makeExamplePipe() {
  5. return es.connect(
  6. es.map(function (data, callback) {
  7. callback(null, data * 2)
  8. }),
  9. es.map(function (data, callback) {
  10. d.delay(callback)(null, data)
  11. }),
  12. es.map(function (data, callback) {
  13. callback(null, data + 2)
  14. }))
  15. }
  16. exports['simple pipe'] = function (test) {
  17. var pipe = makeExamplePipe()
  18. pipe.on('data', function (data) {
  19. it(data).equal(18)
  20. test.done()
  21. })
  22. pipe.write(8)
  23. }
  24. exports['read array then map'] = function (test) {
  25. var readThis = d.map(3, 6, 100, d.id) //array of multiples of 3 < 100
  26. , first = es.readArray(readThis)
  27. , read = []
  28. , pipe =
  29. es.connect(
  30. first,
  31. es.map(function (data, callback) {
  32. callback(null, {data: data})
  33. }),
  34. es.map(function (data, callback) {
  35. callback(null, {data: data})
  36. }),
  37. es.writeArray(function (err, array) {
  38. it(array).deepEqual(d.map(readThis, function (data) {
  39. return {data: {data: data}}
  40. }))
  41. test.done()
  42. })
  43. )
  44. }
  45. exports ['connect returns a stream'] = function (test) {
  46. var rw =
  47. es.connect(
  48. es.map(function (data, callback) {
  49. callback(null, data * 2)
  50. }),
  51. es.map(function (data, callback) {
  52. callback(null, data * 5)
  53. })
  54. )
  55. it(rw).has({readable: true, writable: true})
  56. var array = [190, 24, 6, 7, 40, 57, 4, 6]
  57. , _array = []
  58. , c =
  59. es.connect(
  60. es.readArray(array),
  61. rw,
  62. es.log('after rw:'),
  63. es.writeArray(function (err, _array) {
  64. it(_array).deepEqual(array.map(function (e) { return e * 10 }))
  65. test.done()
  66. })
  67. )
  68. }
  69. require('./helper')(module)