liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var test = require('tape')
var collectAll = require('../')
 
test('collectAll', function (t) {
  var stream = collectAll()
  stream.on('readable', function () {
    var chunk = this.read()
    if (chunk) {
      t.strictEqual(chunk, 'onetwo')
      t.end()
    }
  })
  stream.setEncoding('utf8')
  stream.write('one')
  stream.write('two')
  stream.end()
})
 
test('.collectAll(through)', function (t) {
  var stream = collectAll(function (data) {
    return data + 'yeah?'
  })
 
  stream.on('readable', function () {
    var chunk = this.read()
    if (chunk) {
      t.strictEqual(chunk, 'cliveyeah?')
      t.end()
    }
  })
  stream.setEncoding('utf8')
  stream.end('clive')
})
 
test('.collectAll(through): object mode', function (t) {
  function through (collected) {
    collected.forEach(function (object) {
      object.received = true
    })
    return collected
  }
  var stream = collectAll(through, { objectMode: true })
 
  stream.on('readable', function () {
    var collected = this.read()
    if (collected) {
      t.deepEqual(collected, [ { received: true }, { received: true } ])
      t.end()
    }
  })
  stream.write({})
  stream.end({})
})
 
test('collect(through): readable object mode', function (t) {
  var stream = collectAll(
    function (data) {
      t.strictEqual(data.toString(), 'yeah')
      return { ok: true }
    },
    { readableObjectMode: true }
  )
  stream.end('yeah')
  t.end()
})