From 3f4f670691944626f70e2aeff900485be3cf4ce5 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 30 Mar 2017 09:43:00 +0100 Subject: [PATCH] feat: isEqual --- README.md | 3 +++ src/index.js | 10 ++++++++++ test/index.spec.js | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/README.md b/README.md index cc47f52..0da2d07 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,9 @@ Returns an `obj` of the form Alias for `.toJSON()`. +### `isEqual(id)` + +- `id` can be a PeerId or a Buffer containing the id # License diff --git a/src/index.js b/src/index.js index c1baba8..0e78285 100644 --- a/src/index.js +++ b/src/index.js @@ -88,6 +88,16 @@ class PeerId { toB58String () { return this._idB58String } + + isEqual (id) { + if (Buffer.isBuffer(id)) { + return this.id.equals(id) + } else if (id.id) { + return this.id.equals(id.id) + } else { + throw new Error('not valid Id') + } + } } exports = module.exports = PeerId diff --git a/test/index.spec.js b/test/index.spec.js index 5cbc952..c822c87 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -129,6 +129,20 @@ describe('PeerId', () => { expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex')) }) + it('isEqual', (done) => { + parallel([ + (cb) => PeerId.create(cb), + (cb) => PeerId.create(cb) + ], (err, ids) => { + expect(err).to.not.exist() + expect(ids[0].isEqual(ids[0])).to.equal(true) + expect(ids[0].isEqual(ids[1])).to.equal(false) + expect(ids[0].isEqual(ids[0].id)).to.equal(true) + expect(ids[0].isEqual(ids[1].id)).to.equal(false) + done() + }) + }) + describe('fromJSON', () => { it('full node', (done) => { PeerId.create({ bits: 1024 }, (err, id) => {