From b519cd44eceda76c6b7282fed24c57f4c634710d Mon Sep 17 00:00:00 2001 From: =?utf8?q?To=CC=83nis=20Tiigi?= Date: Wed, 27 Feb 2013 21:20:20 +0200 Subject: [PATCH] Add missing binarySlice/binaryWrite methods --- index.js | 5 +++++ test/buffer.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/index.js b/index.js index 522dd93..5ff77ec 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,7 @@ SlowBuffer.byteLength = function (str, encoding) { return utf8ToBytes(str).length; case 'ascii': + case 'binary': return str.length; case 'base64': @@ -81,6 +82,8 @@ SlowBuffer.prototype.asciiWrite = function (string, offset, length) { return SlowBuffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length); }; +SlowBuffer.prototype.binaryWrite = SlowBuffer.prototype.asciiWrite; + SlowBuffer.prototype.base64Write = function (string, offset, length) { var bytes, pos; return SlowBuffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length); @@ -125,6 +128,8 @@ SlowBuffer.prototype.asciiSlice = function () { return ret; } +SlowBuffer.prototype.binarySlice = SlowBuffer.prototype.asciiSlice; + SlowBuffer.prototype.inspect = function() { var out = [], len = this.length; diff --git a/test/buffer.js b/test/buffer.js index f54c5d2..0f8c9dd 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -19,6 +19,15 @@ test('utf8 buffer to hex', function (t) { t.end(); }); +test('utf8 to utf8', function (t) { + t.plan(1); + t.equal( + new buffer.Buffer("öäüõÖÄÜÕ", "utf8").toString("utf8"), + new Buffer("öäüõÖÄÜÕ", "utf8").toString("utf8") + ); + t.end(); +}); + test('ascii buffer to base64', function (t) { t.plan(1); t.equal( @@ -72,6 +81,43 @@ test('hex buffer to ascii', function (t) { ); t.end(); }); +/* +test('utf8 to ascii', function (t) { + t.plan(1); + t.equal( + new buffer.Buffer("öäüõÖÄÜÕ", "utf8").toString("ascii"), + new Buffer("öäüõÖÄÜÕ", "utf8").toString("ascii") + ); + t.end(); +}); +*/ + +test('base64 buffer to binary', function (t) { + t.plan(1); + t.equal( + new buffer.Buffer("MTIzNDU2IUAjJCVe", "base64").toString("binary"), + new Buffer("MTIzNDU2IUAjJCVe", "base64").toString("binary") + ); + t.end(); +}); + +test('hex buffer to binary', function (t) { + t.plan(1); + t.equal( + new buffer.Buffer("31323334353621402324255e", "hex").toString("binary"), + new Buffer("31323334353621402324255e", "hex").toString("binary") + ); + t.end(); +}); + +test('utf8 to binary', function (t) { + t.plan(1); + t.equal( + new buffer.Buffer("öäüõÖÄÜÕ", "utf8").toString("binary"), + new Buffer("öäüõÖÄÜÕ", "utf8").toString("binary") + ); + t.end(); +}); test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function (t) { t.plan(2*(2*2*2+2)); -- 2.34.1