• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var tape = require('tape')
2var from = require('from2')
3var iterate = require('./')
4
5tape('merge sort', function (t) {
6  var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h'])
7  var b = from.obj(['b', 'c', 'f'])
8  var output = []
9
10  var readA = iterate(a)
11  var readB = iterate(b)
12
13  var loop = function () {
14    readA(function (err, dataA, nextA) {
15      if (err) throw err
16      readB(function (err, dataB, nextB) {
17        if (err) throw err
18
19        if (!dataA && !dataB) {
20          t.same(output, ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 'sorts list')
21          t.end()
22          return
23        }
24
25        if (!dataB || dataA < dataB) {
26          output.push(dataA)
27          nextA()
28          return loop()
29        }
30
31        if (!dataA || dataA > dataB) {
32          output.push(dataB)
33          nextB()
34          return loop()
35        }
36
37        output.push(dataA)
38        output.push(dataB)
39        nextA()
40        nextB()
41        loop()
42      })
43    })
44  }
45
46  loop()
47})
48
49tape('error handling', function (t) {
50  var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h'])
51  var read = iterate(a)
52
53  a.destroy(new Error('oh no'))
54
55  read(function (err) {
56    t.ok(err, 'had error')
57    t.same(err.message, 'oh no')
58    t.end()
59  })
60})
61