Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Stringer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ class Stringer extends Transform {
this._prev = '';
this._depth = 0;

if (this._makeArray) {
if (this._makeArray && this._makeObject) {
// todo: should it throw an error or not?
} else if (this._makeArray) {
this._transform = this._arrayTransform;
this._flush = this._arrayFlush;
} else if (this._makeObject) {
this._transform = this._objectTransform;
this._flush = this._objectFlush;
}
}

Expand All @@ -82,6 +87,21 @@ class Stringer extends Transform {
this._transform({name: 'endArray'}, null, callback);
}

_objectTransform(chunk, encoding, callback) {
// it runs once
delete this._transform;
this._transform({name: 'startObject'}, encoding, doNothing);
this._transform(chunk, encoding, callback);
}

_objectFlush(callback) {
if (this._transform === this._objectTransform) {
delete this._transform;
this._transform({name: 'startObject'}, null, doNothing);
}
this._transform({name: 'endObject'}, null, callback);
}

_transform(chunk, _, callback) {
if (this._values[chunk.name]) {
if (this._depth && noCommaAfter[this._prev] !== 1) this.push(',');
Expand Down
6 changes: 6 additions & 0 deletions tests/test_stringer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ unit.add(module, [

new ReadString('').pipe(parser);
},
function test_stringer_json_stream_objects_as_object(t) {
// todo
},
function test_stringer_no_input_as_object(t) {
// todo
},
function test_stringer_json_stream_primitives(t) {
const async = t.startAsync('test_stringer_json_stream_primitives');

Expand Down