Skip to content

Commit 6d54479

Browse files
leonardodinolathropd
authored andcommitted
Fix array stream to account for empty arrays (#337)
* Fix array stream to account for empty arrays * Add strem.array testing, fix edge-case
1 parent d6fba8a commit 6d54479

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

lib/stream.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ module.exports = {
55
* Streaming array helper
66
*
77
* @param {Stream} data (optional)
8+
* @return {Function}
89
*/
910
array: function stream_array (stream) {
1011
if (!stream) return function () {}
1112
var first = true
1213

1314
return function _stream_array (data, end) {
14-
var json = JSON.stringify(data, true, 2)
15+
var string = JSON.stringify(data, true, 2)
16+
var json = isArray(data) ? string.slice(1, -1) : string
17+
var empty = json.trim() === ''
1518

16-
if (first) {
17-
stream.write('[\n')
18-
first = false
19-
}
20-
21-
if (isArray(data)) {
22-
json = json.slice(1, -1)
23-
}
19+
if (first && empty && !end) return
20+
if (first) { stream.write('[\n') }
21+
if (!first && !empty) { stream.write(',') }
2422

2523
if (end) {
2624
stream.end(json + ']')
2725
} else {
28-
stream.write(json + ',')
26+
stream.write(json)
2927
}
28+
29+
first = false
3030
}
3131
},
3232

test/stream_spec.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* global it describe */
2+
3+
/**
4+
* Module Dependencies
5+
*/
6+
7+
var assert = require('assert')
8+
var EventEmitter = require('events')
9+
var streamHelper = require('../lib/stream')
10+
11+
function createStream () {
12+
var instance = new EventEmitter()
13+
instance._data = ''
14+
instance._open = true
15+
instance.on('write', function (chunk) { instance._data += chunk })
16+
instance.once('end', function () { instance._open = false })
17+
18+
instance.write = function write (chunk) { instance.emit('write', String(chunk) || '') }
19+
instance.error = function error (err) { instance.emit('error', err) }
20+
instance.end = function end (chunk) {
21+
if (!instance._open) return
22+
instance.emit('write', chunk)
23+
instance.emit('end')
24+
}
25+
26+
return instance
27+
}
28+
29+
function getSessionResult () {
30+
var events = Array.prototype.slice.call(arguments)
31+
var stream = createStream()
32+
var helper = streamHelper.array(stream)
33+
events.forEach(function (data, index) { helper(data, index === events.length - 1) })
34+
while (stream._open) { /* wait for stream to close */ }
35+
return JSON.stringify(JSON.parse(stream._data))
36+
}
37+
38+
/**
39+
* Tests
40+
*/
41+
42+
describe('stream.array helper', function () {
43+
it('accepts non-empty arrays', function () {
44+
var result = getSessionResult([1, 2], [3])
45+
assert.equal(result, '[1,2,3]')
46+
})
47+
it('accepts one non-empty array', function () {
48+
var result = getSessionResult([1])
49+
assert.equal(result, '[1]')
50+
})
51+
it('accepts one empty array', function () {
52+
var result = getSessionResult([])
53+
assert.equal(result, '[]')
54+
})
55+
it('accepts one single value', function () {
56+
var result = getSessionResult(1)
57+
assert.equal(result, '[1]')
58+
})
59+
it('accepts multiple values', function () {
60+
var result = getSessionResult(1, 2, 3)
61+
assert.equal(result, '[1,2,3]')
62+
})
63+
it('accepts one empty array at the end', function () {
64+
var result = getSessionResult([1, 2], [3], [])
65+
assert.equal(result, '[1,2,3]')
66+
})
67+
it('accepts multiple empty arrays', function () {
68+
var result = getSessionResult([], [], [], [])
69+
assert.equal(result, '[]')
70+
})
71+
it('accepts arrays', function () {
72+
var result = getSessionResult([1], [], [], [2], [])
73+
assert.equal(result, '[1,2]')
74+
})
75+
it('accepts all weird things', function () {
76+
var result = getSessionResult([], [1], [2], [], [], 3, 4, [])
77+
var result2 = getSessionResult([], [1], [2], [], [], 3, 4, [], [])
78+
var result3 = getSessionResult([], [], [1], [2], [], [], 3, 4, [], [])
79+
var result4 = getSessionResult([1], [2], [], [], 3, 4, [], [])
80+
var result5 = getSessionResult([1, 2, 3, 4])
81+
assert.equal(result, '[1,2,3,4]')
82+
assert.equal(result2, '[1,2,3,4]')
83+
assert.equal(result3, '[1,2,3,4]')
84+
assert.equal(result4, '[1,2,3,4]')
85+
assert.equal(result5, '[1,2,3,4]')
86+
})
87+
})

0 commit comments

Comments
 (0)