stringifier.js
1.47 KB
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
/* eslint-env mocha */
var jsonlines = require('../')
var assert = require('assert')
var concat = require('concat-stream')
describe('Stringifier', function () {
it('should handle simple jsonlines', function (done) {
var stringifier = jsonlines.stringify()
var source = [
{ a: 453345, b: 23423 },
{ c: 843222, d: 19534 },
{ e: 656564, f: 76521 }
]
var expected = source.map(function (obj) {
return JSON.stringify(obj)
}).join('\n')
stringifier.pipe(concat({ encoding: 'buffer' }, function (result) {
assert.deepEqual(result.toString().trim(), expected)
done()
}))
for (var line of source) {
stringifier.write(line)
}
stringifier.end()
})
it('should handle plain values', function (done) {
var stringifier = jsonlines.stringify()
var source = [ true, false, true, true, false ]
var expected = 'true\nfalse\ntrue\ntrue\nfalse'
stringifier.pipe(concat({ encoding: 'buffer' }, function (result) {
assert.deepEqual(result.toString().trim(), expected)
done()
}))
for (var line of source) {
stringifier.write(line)
}
stringifier.end()
})
it('should emit an error on bad values', function (done) {
var stringifier = jsonlines.stringify()
var broken = { a: 1 }
// Make the value bad
broken.self = broken
stringifier.on('error', function (err) {
assert.equal(err.source, broken)
done()
})
stringifier.end(broken)
})
})