From 5beee200388fd9bbdfcb7fc24f25f4178f77a65d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 2 Mar 2015 16:47:26 +1100 Subject: [PATCH] bufferutils: add equal --- src/bufferutils.js | 11 +++++++++++ test/bufferutils.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/bufferutils.js b/src/bufferutils.js index 02c9594..afe03e6 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -165,6 +165,16 @@ function varIntBuffer (i) { return buffer } +function equal (a, b) { + if (a.length !== b.length) return false + + for (var i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false + } + + return true +} + function reverse (buffer) { var buffer2 = new Buffer(buffer) Array.prototype.reverse.call(buffer2) @@ -172,6 +182,7 @@ function reverse (buffer) { } module.exports = { + equal: equal, pushDataSize: pushDataSize, readPushDataInt: readPushDataInt, readUInt64LE: readUInt64LE, diff --git a/test/bufferutils.js b/test/bufferutils.js index a41e2a7..485447b 100644 --- a/test/bufferutils.js +++ b/test/bufferutils.js @@ -77,6 +77,22 @@ describe('bufferutils', function () { }) }) + describe('equal', function () { + fixtures.valid.forEach(function (f) { + describe('for ' + f.hexVI, function () { + fixtures.valid.forEach(function (f2) { + it('equates the string comparison: ' + f.hexVI + ' === ' + f2.hexVI, function () { + var a = new Buffer(f.hexVI, 'hex') + var b = new Buffer(f2.hexVI, 'hex') + var expected = f.hexVI === f2.hexVI + + assert.equal(bufferutils.equal(a, b), expected) + }) + }) + }) + }) + }) + describe('reverse', function () { fixtures.valid.forEach(function (f) { it('reverses ' + f.hex64 + ' correctly', function () {