Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
siarheidudko committed Aug 20, 2019
1 parent e93a514 commit ec06694
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 79 deletions.
49 changes: 3 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,8 @@ Creates a stream to convert json from string or convert json to drain. The strea

```
let ObjectStream = require('@sergdudko/objectstream');
let objectStream = new ObjectStream();
objectStream.Stringifer.on('data', function(data){
..//data event return string (based on JSON.stringify method)
});
objectStream.Stringifer.on('error', function(err){
..//error event return err (instanceof Error)
});
objectStream.Stringifer.on('end', function(){
..//end event
});
objectStream.Stringifer.on('finish', function(){
..//finish event
});
objectStream.Parser.on('data', function(data){
..//data event return object (based on JSON.parse method)
});
objectStream.Parser.on('error', function(errors){
..//error event return Array of error (instanceof Error)
errors.forEach(function(err){
..//err (instanceof Error)
});
});
objectStream.Parser.on('end', function(){
..//end event
});
objectStream.Parser.on('finish', function(){
..//finish event
});
objectstream.Stringifer.write(1); //will cause an error event
objectstream.Stringifer.write({w:1}); //will cause an data event {"w":1}
objectstream.Stringifer.end({w:1}); //will cause an data event {"w":1}, end event, finish event
objectstream.Parser.write('@'); //will cause an error event
objectstream.Parser.write('{"w":1}\r{"b":2, "a": false}, [{"f":3}, {"c":]10 }{"g":1}{}{u:0}'); //will cause an data event {w:1} {b:2, a: false} {f:3} {g:1} {}, error event
objectstream.Parser.end('{"w":1}'); //will cause an data event {"w":1}, end event, finish event
```

## Alternative
```
let objectStream = require('@sergdudko/objectstream')();
let Stringifer = new objectStream.Stringifer();
let Parser = new objectStream.Parser();
let Stringifer = new ObjectStream.Stringifer();
let Parser = new ObjectStream.Parser();
Stringifer.on('data', function(data){
..//data event return string (based on JSON.stringify method)
Expand Down Expand Up @@ -108,7 +65,7 @@ Creates a stream to convert json from string or convert json to drain. The strea

## TEST
```
let ObjectStream = require('@sergdudko/objectstream')();
let ObjectStream = require('@sergdudko/objectstream');
const start = '[',
sep = ',',
Expand Down
36 changes: 12 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ let Stringifer = function(_start = '', _separator = '', _end = ''){
highWaterMark: 64*1024,
objectMode: true
});
self.Transform.setEncoding('utf8');
return self.Transform;
}

Expand All @@ -78,26 +77,28 @@ let Parser = function(_start = '', _separator = '', _end = ''){
self.LeftBrace = 0;
self.RightBrace = 0;
self.OpenQuotes = false;
//self.StringDecoder.end();
return;
}
self.clear();
self.Transform = new STREAM.Transform({
transform(string, encoding = self.encoding, callback = function(){}) {
let _string;
if(string instanceof Buffer){
string = self.StringDecoder.write(string);
_string = self.StringDecoder.write(string);
} else {
_string = string;
}
if(string === ''){
if(_string === ''){
callback();
}else if(typeof(string) === 'undefined'){
}else if(typeof(_string) === 'undefined'){
callback();
} else if(typeof(string) !== 'string'){
callback([new Error('Incoming data type is '+typeof(string)+', require data type is String or Buffer!')]);
} else if(typeof(_string) !== 'string'){
callback([new Error('Incoming data type is '+typeof(_string)+', require data type is String!')]);
} else {
let charArr = new Array();
let error = new Array();
charArr = string.split('');
self.bytesRead += Buffer.byteLength(string, self.encoding);
charArr = _string.split('');
self.bytesRead += Buffer.byteLength(_string, self.encoding);
for(let s = 0; s < charArr.length; s++){
if(charArr[s] !== null){
switch(charArr[s]){
Expand Down Expand Up @@ -175,18 +176,5 @@ let Parser = function(_start = '', _separator = '', _end = ''){
return self.Transform;
}

let ObjectStream = function(){
let self = this;
if(this){
self.Stringifer = new Stringifer();
self.Parser = new Parser();
return self;
} else {
return {
Stringifer: Stringifer,
Parser: Parser
};
}
}

module.exports = ObjectStream;
module.exports.Stringifer = Stringifer;
module.exports.Parser = Parser;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sergdudko/objectstream",
"version": "1.1.4",
"version": "1.2.0",
"description": "Creates a stream to convert json from string or convert json to drain. The stream is based on the incoming object stream.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
let ObjectStream = require('./index.js');

let objectstream = {
Stringifer: new (ObjectStream().Stringifer)(),
Parser: new (ObjectStream().Parser)()
Stringifer: new ObjectStream.Stringifer(),
Parser: new ObjectStream.Parser()
};

objectstream.Stringifer.on('data', function(data){
Expand Down
4 changes: 2 additions & 2 deletions test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const start = '[',
end = ']';

let objectstream = {
Stringifer: new (ObjectStream().Stringifer)(start, sep, end),
Parser: new (ObjectStream().Parser)(start, sep, end)
Stringifer: new ObjectStream.Stringifer(start, sep, end),
Parser: new ObjectStream.Parser(start, sep, end)
};

const object = {
Expand Down
8 changes: 4 additions & 4 deletions test3.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const start = '[',
end = ']';

let objectstream = {
Stringifer: new (ObjectStream().Stringifer)(start, sep, end),
Parser: new (ObjectStream().Parser)(start, sep, end),
Stringifer2: new (ObjectStream().Stringifer)(start, sep, end),
Parser2: new (ObjectStream().Parser)(start, sep, end)
Stringifer: new ObjectStream.Stringifer(start, sep, end),
Parser: new ObjectStream.Parser(start, sep, end),
Stringifer2: new ObjectStream.Stringifer(start, sep, end),
Parser2: new ObjectStream.Parser(start, sep, end)
};

const object = {
Expand Down

0 comments on commit ec06694

Please sign in to comment.