diff --git a/README.md b/README.md index f93c3ad..73c292d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ ## Table of Contents +- [peer-id](#peer-id) + - [Lead Maintainer](#lead-maintainer) + - [Table of Contents](#table-of-contents) - [Description](#description) - [Example](#example) - [Installation](#installation) @@ -37,13 +40,18 @@ - [`createFromPubKey(pubKey)`](#createfrompubkeypubkey) - [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey) - [`createFromJSON(obj)`](#createfromjsonobj) + - [`createFromProtobuf(buf)`](#createfromprotobufbuf) - [Export](#export) + - [`toHexString()`](#tohexstring) - [`toBytes()`](#tobytes) - [`toString()`](#tostring) - [`toB58String()`](#tob58string) - - [`toHexString()`](#tohexstring) - [`toJSON()`](#tojson) + - [`marshal(excludePrivateKey)`](#marshalexcludeprivatekey) + - [`marshalPubKey()`](#marshalpubkey) - [`toPrint()`](#toprint) + - [`equals(id)`](#equalsid) + - [`isEqual(id)`](#isequalid) - [License](#license) # Description @@ -256,7 +264,14 @@ Returns the Peer ID as a printable string without the `Qm` prefix. Example: `` +### `equals(id)` + +Returns `true` if the given PeerId is equal to the current instance. + +- `id` can be a PeerId or a Buffer containing the id + ### `isEqual(id)` +**Deprecation Notice**: Use [`equals`](#equalsid), `isEqual` will be removed in 0.14.0. - `id` can be a PeerId or a Buffer containing the id diff --git a/src/index.js b/src/index.js index 0081dd4..2f790c1 100644 --- a/src/index.js +++ b/src/index.js @@ -133,7 +133,12 @@ class PeerId { return this._idCIDString } - isEqual (id) { + /** + * Checks the equality of `this` peer against a given PeerId. + * @param {Buffer|PeerId} id + * @returns {boolean} + */ + equals (id) { if (Buffer.isBuffer(id)) { return this.id.equals(id) } else if (id.id) { @@ -143,6 +148,16 @@ class PeerId { } } + /** + * Checks the equality of `this` peer against a given PeerId. + * @deprecated Use `.equals` + * @param {Buffer|PeerId} id + * @returns {boolean} + */ + isEqual (id) { + return this.equals(id) + } + /* * Check if this PeerId instance is valid (privKey -> pubKey -> Id) */ diff --git a/test/peer-id.spec.js b/test/peer-id.spec.js index 2e85a5f..8b48938 100644 --- a/test/peer-id.spec.js +++ b/test/peer-id.spec.js @@ -231,6 +231,18 @@ describe('PeerId', () => { expect(ids[0].isEqual(ids[1].id)).to.equal(false) }) + it('equals', async () => { + const ids = await Promise.all([ + PeerId.create(testOpts), + PeerId.create(testOpts) + ]) + + expect(ids[0].equals(ids[0])).to.equal(true) + expect(ids[0].equals(ids[1])).to.equal(false) + expect(ids[0].equals(ids[0].id)).to.equal(true) + expect(ids[0].equals(ids[1].id)).to.equal(false) + }) + describe('fromJSON', () => { it('full node', async () => { const id = await PeerId.create(testOpts)