Skip to content

Add boolean type support. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ JavaScript values should be obvious given their types:
-------------------------------------------------------------------
A | char[] | Array | Length | (1)
x | pad byte | N/A | 1 |
? | boolean | boolean | 1 |
c | char | string (length 1) | 1 | (2)
b | signed char | number | 1 | (3)
B | unsigned char | number | 1 | (3)
Expand Down Expand Up @@ -162,4 +163,4 @@ To run the test suite first invoke the following command within the repo, instal

then run the tests:

$ npm test
$ npm test
19 changes: 14 additions & 5 deletions bufferpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function BufferPack() {
for (var i = 0; i < l; a[p+i] = v[i]?v[i]:0, i++);
};

// Boolean
m._DeBool = function (a, p) {
return !!a[p];
};
m._EnBool = function (a, p, v) {
a[p] = !!v ? 1 : 0;
};

// ASCII characters
m._DeChar = function (a, p) {
return String.fromCharCode(a[p]);
Expand Down Expand Up @@ -127,10 +135,11 @@ function BufferPack() {
};

// Class data
m._sPattern = '(\\d+)?([AxcbBhHsSfdiIlL])(\\(([a-zA-Z0-9]+)\\))?';
m._lenLut = {'A': 1, 'x': 1, 'c': 1, 'b': 1, 'B': 1, 'h': 2, 'H': 2, 's': 1,
'S': 1, 'f': 4, 'd': 8, 'i': 4, 'I': 4, 'l': 4, 'L': 4};
m._sPattern = '(\\d+)?([A\?xcbBhHsSfdiIlL])(\\(([a-zA-Z0-9]+)\\))?';
m._lenLut = {'A': 1, '?': 1, 'x': 1, 'c': 1, 'b': 1, 'B': 1, 'h': 2, 'H': 2,
's': 1, 'S': 1, 'f': 4, 'd': 8, 'i': 4, 'I': 4, 'l': 4, 'L': 4};
m._elLut = {'A': {en: m._EnArray, de: m._DeArray},
'?': {en: m._EnBool, de: m._DeBool},
's': {en: m._EnString, de: m._DeString},
'S': {en: m._EnString, de: m._DeNullString},
'c': {en: m._EnChar, de: m._DeChar},
Expand Down Expand Up @@ -200,7 +209,7 @@ function BufferPack() {
case 'A': case 's': case 'S':
rv.push(this._elLut[m[2]].de(a, p, n));
break;
case 'c': case 'b': case 'B': case 'h': case 'H':
case '?': case 'c': case 'b': case 'B': case 'h': case 'H':
case 'i': case 'I': case 'l': case 'L': case 'f': case 'd':
el = this._elLut[m[2]];
rv.push(this._UnpackSeries(n, s, a, p));
Expand Down Expand Up @@ -253,7 +262,7 @@ function BufferPack() {
this._elLut[m[2]].en(a, p, n, values[i]);
i += 1;
break;
case 'c': case 'b': case 'B': case 'h': case 'H':
case'?': case 'c': case 'b': case 'B': case 'h': case 'H':
case 'i': case 'I': case 'l': case 'L': case 'f': case 'd':
el = this._elLut[m[2]];
if ((i + n) > values.length) { return false; }
Expand Down
35 changes: 35 additions & 0 deletions test/bool.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require('should');
var buffer = require('buffer');

var bufferpack = require('..');


describe('Boolean', function() {
var values = [true, false, 42];
var format = '<?(first)?(second)b(third)';

var packed = bufferpack.pack(format, values);

describe('#pack()', function() {
it('should have packed size of 3', function() {
packed.length.should.equal(3);
});

it('should pack fine', function() {
(!!packed[0]).should.equal(values[0]);
(!!packed[1]).should.equal(values[1]);
packed[2].should.equal(values[2]);
});
});

describe('#unpack()', function() {
var unpacked = bufferpack.unpack(format, packed);

it('should return an object with correct values', function() {
unpacked.should.be.an.instanceOf(Object);
unpacked.first.should.equal(values[0]);
unpacked.second.should.equal(values[1]);
unpacked.third.should.equal(values[2]);
});
});
});
2 changes: 1 addition & 1 deletion test/cstring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('C String', function() {
packed.length.should.equal(12);
});

it('should back fine', function() {
it('should pack fine', function() {
packed[0].should.equal(values[0]);
packed[1].should.equal(values[1]);
packed[2].should.equal(values[2]);
Expand Down