Skip to content

Commit

Permalink
Merge pull request #4 from haensl/3
Browse files Browse the repository at this point in the history
#3: Fix empty source transformation.
  • Loading branch information
haensl authored Jan 13, 2021
2 parents 8387b99 + c7cce77 commit 7765f85
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 121 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## 1.0.1
* [#3: Fix empty source transformation.](https://github.com/haensl/json-transform-stream/issues/3)
* Update dependencies.

## 1.0.0
* [#1: Bootstrap project.](https://github.com/haensl/json-transform-stream/issues/1)
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = ({
transform: (chunk, encoding, callback) => {
if (first) {
first = false;

return callback(
null,
Buffer.concat([Buffer.from(pre), chunk])
Expand All @@ -23,13 +24,13 @@ module.exports = ({
Buffer.concat([Buffer.from(separator), chunk])
);
},
final: function(callback) {
flush: function(callback) {
if (first) {
this.push(Buffer.from(pre));
} else {
this.push(Buffer.from(post));
}

this.push(Buffer.from(post));

process.nextTick(callback);
}
});
Expand Down
28 changes: 26 additions & 2 deletions json-transform-stream.package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('json-transform-stream', () => {
{ index: 3 }
];

const iterator = function* () {
for (const d of data) {
const iterator = function* (_data = data) {
for (const d of _data) {
yield JSON.stringify(d);
}
};
Expand Down Expand Up @@ -56,6 +56,30 @@ describe('json-transform-stream', () => {
})
);
});

describe('empty source', () => {
beforeEach((done) => {
streamed = '';
const stream = Readable.from(iterator([]))
.pipe(JSONTransform({
pre: '{"data":[',
post: ']}'
}));
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});

it('Allows to setup custom wrapping', () => {
expect(streamed)
.toEqual(
JSON.stringify({
data: []
})
);
});
});
});

describe('separator', () => {
Expand Down
28 changes: 26 additions & 2 deletions json-transform-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('json-transform-stream', () => {
{ index: 3 }
];

const iterator = function* () {
for (const d of data) {
const iterator = function* (_data = data) {
for (const d of _data) {
yield JSON.stringify(d);
}
};
Expand Down Expand Up @@ -56,6 +56,30 @@ describe('json-transform-stream', () => {
})
);
});

describe('empty source', () => {
beforeEach((done) => {
streamed = '';
const stream = Readable.from(iterator([]))
.pipe(JSONTransform({
pre: '{"data":[',
post: ']}'
}));
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});

it('Allows to setup custom wrapping', () => {
expect(streamed)
.toEqual(
JSON.stringify({
data: []
})
);
});
});
});

describe('separator', () => {
Expand Down
Loading

0 comments on commit 7765f85

Please sign in to comment.