bin-protocol
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": ["buffer"], |
@@ -66,3 +66,3 @@ # bin-protocol | ||
this.Int8('char'); | ||
return String.fromCharCode(this.context.char); | ||
return String.fromCharCode(this.context.char); // convert from char code to character | ||
} | ||
@@ -75,4 +75,4 @@ }); | ||
.Int8('length') | ||
.loop('items', this.char, this.context.length); | ||
return this.context.items; | ||
.loop('items', this.char, this.context.length); // read 'length' characters with above defined 'char' method | ||
return this.context.items; // return just items, without 'length' property | ||
} | ||
@@ -83,4 +83,3 @@ }); | ||
reader | ||
.array('chars'); | ||
reader.array('chars'); | ||
@@ -104,2 +103,3 @@ console.log(reader.result); // => { chars: [ 'a', 'b', 'c', 'd', 'e' ] } | ||
Define an array data type which first writes data array length as a single byte | ||
```javascript | ||
@@ -110,3 +110,3 @@ protocol.define('array', { | ||
.Int8(values.length) | ||
.loop(values, this.Int8); | ||
.loop(values, this.Int8); // write all values with Int8 method | ||
} | ||
@@ -150,2 +150,52 @@ }); | ||
### Loops (arrays) | ||
All next 3 examples are essentialy identical: | ||
Read data with your own code: | ||
```javascript | ||
protocol.define('customArray', { | ||
read: function () { | ||
var i = 0; | ||
this.Int32BE('length'); | ||
for(i = 0; i<this.context.length; i++){ | ||
this.Int32BE('items[' + i + ']'); // yes, this works | ||
} | ||
return this.context.items; | ||
} | ||
}); | ||
``` | ||
Read with `.loop()` method by providing the length (loop count): | ||
```javascript | ||
protocol.define('loopArray', { | ||
read: function () { | ||
this | ||
.Int32BE('length') | ||
.loop('items', this.Int32BE, this.context.length); | ||
return this.context.items; | ||
} | ||
}); | ||
``` | ||
Read with `.loop()` method until the `end()` is called: | ||
```javascript | ||
protocol.define('loopArrayEnd', { | ||
read: function () { | ||
var len; | ||
this.Int32BE('length'); | ||
len = this.context.length; | ||
this.loop('items', function (end) { | ||
this.Int32BE(); | ||
if((len -= 1) === 0){ | ||
end(); // call end() to break from loop | ||
} | ||
}); | ||
return this.context.items; | ||
} | ||
}); | ||
``` | ||
See [Kafka protocol](https://github.com/oleksiyk/kafka/tree/master/lib/protocol) for more examples. | ||
@@ -152,0 +202,0 @@ |
@@ -123,8 +123,8 @@ "use strict"; | ||
read: function () { | ||
var i = 0; | ||
this | ||
.Int32BE('length') | ||
.loop('items', function (end) { | ||
var len; | ||
this.Int32BE('length'); | ||
len = this.context.length; | ||
this.loop('items', function (end) { | ||
this.Int32BE(); | ||
if(i++ === 2){ | ||
if((len -= 1) === 0){ | ||
end(); | ||
@@ -131,0 +131,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68970
223