Merge pull request #58 from libp2p/feat/isEqual

feat: isEqual
This commit is contained in:
David Dias 2017-03-30 10:02:36 +01:00 committed by GitHub
commit bc213dd818
3 changed files with 27 additions and 0 deletions

View File

@ -203,6 +203,9 @@ Returns an `obj` of the form
Alias for `.toJSON()`. Alias for `.toJSON()`.
### `isEqual(id)`
- `id` can be a PeerId or a Buffer containing the id
# License # License

View File

@ -88,6 +88,16 @@ class PeerId {
toB58String () { toB58String () {
return this._idB58String 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 exports = module.exports = PeerId

View File

@ -129,6 +129,20 @@ describe('PeerId', () => {
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex')) 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', () => { describe('fromJSON', () => {
it('full node', (done) => { it('full node', (done) => {
PeerId.create({ bits: 1024 }, (err, id) => { PeerId.create({ bits: 1024 }, (err, id) => {