header_footer.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var fs = require ('fs')
  2. , join = require('path').join
  3. , file = join(__dirname, 'fixtures','header_footer.json')
  4. , JSONStream = require('../')
  5. , it = require('it-is')
  6. var expected = JSON.parse(fs.readFileSync(file))
  7. , parser = JSONStream.parse(['rows', /\d+/ /*, 'value'*/])
  8. , called = 0
  9. , headerCalled = 0
  10. , footerCalled = 0
  11. , ended = false
  12. , parsed = []
  13. fs.createReadStream(file).pipe(parser)
  14. parser.on('header', function (data) {
  15. headerCalled ++
  16. it(data).deepEqual({
  17. total_rows: 129,
  18. offset: 0
  19. })
  20. })
  21. parser.on('footer', function (data) {
  22. footerCalled ++
  23. it(data).deepEqual({
  24. foo: { bar: 'baz' }
  25. })
  26. })
  27. parser.on('data', function (data) {
  28. called ++
  29. it.has({
  30. id: it.typeof('string'),
  31. value: {rev: it.typeof('string')},
  32. key:it.typeof('string')
  33. })
  34. it(headerCalled).equal(1)
  35. parsed.push(data)
  36. })
  37. parser.on('end', function () {
  38. ended = true
  39. })
  40. process.on('exit', function () {
  41. it(called).equal(expected.rows.length)
  42. it(headerCalled).equal(1)
  43. it(footerCalled).equal(1)
  44. it(parsed).deepEqual(expected.rows)
  45. console.error('PASSED')
  46. })