From a55f3bd8a7142473ddefc12761c3f8e4f0601ec8 Mon Sep 17 00:00:00 2001 From: hbc Date: Thu, 4 Jun 2015 12:09:53 +0800 Subject: [PATCH] Add boolean type support. By now, we can pack/unpack boolean value with the `?` invocation. --- README.md | 3 ++- bufferpack.js | 19 ++++++++++++++----- test/bool.test.js | 35 +++++++++++++++++++++++++++++++++++ test/cstring.test.js | 2 +- 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 test/bool.test.js diff --git a/README.md b/README.md index b5c4f01..2c89144 100644 --- a/README.md +++ b/README.md @@ -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) @@ -162,4 +163,4 @@ To run the test suite first invoke the following command within the repo, instal then run the tests: - $ npm test \ No newline at end of file + $ npm test diff --git a/bufferpack.js b/bufferpack.js index ff4ec7b..7f4e0f0 100644 --- a/bufferpack.js +++ b/bufferpack.js @@ -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]); @@ -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}, @@ -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)); @@ -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; } diff --git a/test/bool.test.js b/test/bool.test.js new file mode 100644 index 0000000..f621263 --- /dev/null +++ b/test/bool.test.js @@ -0,0 +1,35 @@ +require('should'); +var buffer = require('buffer'); + +var bufferpack = require('..'); + + +describe('Boolean', function() { + var values = [true, false, 42]; + var format = '