From 6d54479b0e7f26ff61c6f2e6646f3c3efd0ffe3d Mon Sep 17 00:00:00 2001 From: Leonardo Dino Date: Sun, 7 Jul 2019 00:29:51 -0300 Subject: [PATCH] Fix array stream to account for empty arrays (#337) * Fix array stream to account for empty arrays * Add strem.array testing, fix edge-case --- lib/stream.js | 20 +++++------ test/stream_spec.js | 87 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 test/stream_spec.js diff --git a/lib/stream.js b/lib/stream.js index 1eb3c56..313e468 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -5,28 +5,28 @@ module.exports = { * Streaming array helper * * @param {Stream} data (optional) + * @return {Function} */ array: function stream_array (stream) { if (!stream) return function () {} var first = true return function _stream_array (data, end) { - var json = JSON.stringify(data, true, 2) + var string = JSON.stringify(data, true, 2) + var json = isArray(data) ? string.slice(1, -1) : string + var empty = json.trim() === '' - if (first) { - stream.write('[\n') - first = false - } - - if (isArray(data)) { - json = json.slice(1, -1) - } + if (first && empty && !end) return + if (first) { stream.write('[\n') } + if (!first && !empty) { stream.write(',') } if (end) { stream.end(json + ']') } else { - stream.write(json + ',') + stream.write(json) } + + first = false } }, diff --git a/test/stream_spec.js b/test/stream_spec.js new file mode 100644 index 0000000..b669b72 --- /dev/null +++ b/test/stream_spec.js @@ -0,0 +1,87 @@ +/* global it describe */ + +/** + * Module Dependencies + */ + +var assert = require('assert') +var EventEmitter = require('events') +var streamHelper = require('../lib/stream') + +function createStream () { + var instance = new EventEmitter() + instance._data = '' + instance._open = true + instance.on('write', function (chunk) { instance._data += chunk }) + instance.once('end', function () { instance._open = false }) + + instance.write = function write (chunk) { instance.emit('write', String(chunk) || '') } + instance.error = function error (err) { instance.emit('error', err) } + instance.end = function end (chunk) { + if (!instance._open) return + instance.emit('write', chunk) + instance.emit('end') + } + + return instance +} + +function getSessionResult () { + var events = Array.prototype.slice.call(arguments) + var stream = createStream() + var helper = streamHelper.array(stream) + events.forEach(function (data, index) { helper(data, index === events.length - 1) }) + while (stream._open) { /* wait for stream to close */ } + return JSON.stringify(JSON.parse(stream._data)) +} + +/** + * Tests + */ + +describe('stream.array helper', function () { + it('accepts non-empty arrays', function () { + var result = getSessionResult([1, 2], [3]) + assert.equal(result, '[1,2,3]') + }) + it('accepts one non-empty array', function () { + var result = getSessionResult([1]) + assert.equal(result, '[1]') + }) + it('accepts one empty array', function () { + var result = getSessionResult([]) + assert.equal(result, '[]') + }) + it('accepts one single value', function () { + var result = getSessionResult(1) + assert.equal(result, '[1]') + }) + it('accepts multiple values', function () { + var result = getSessionResult(1, 2, 3) + assert.equal(result, '[1,2,3]') + }) + it('accepts one empty array at the end', function () { + var result = getSessionResult([1, 2], [3], []) + assert.equal(result, '[1,2,3]') + }) + it('accepts multiple empty arrays', function () { + var result = getSessionResult([], [], [], []) + assert.equal(result, '[]') + }) + it('accepts arrays', function () { + var result = getSessionResult([1], [], [], [2], []) + assert.equal(result, '[1,2]') + }) + it('accepts all weird things', function () { + var result = getSessionResult([], [1], [2], [], [], 3, 4, []) + var result2 = getSessionResult([], [1], [2], [], [], 3, 4, [], []) + var result3 = getSessionResult([], [], [1], [2], [], [], 3, 4, [], []) + var result4 = getSessionResult([1], [2], [], [], 3, 4, [], []) + var result5 = getSessionResult([1, 2, 3, 4]) + assert.equal(result, '[1,2,3,4]') + assert.equal(result2, '[1,2,3,4]') + assert.equal(result3, '[1,2,3,4]') + assert.equal(result4, '[1,2,3,4]') + assert.equal(result5, '[1,2,3,4]') + }) +})