Skip to content

Commit

Permalink
feat: isPeerId
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Mar 27, 2017
1 parent 8c49610 commit 0acc572
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ exports.createFromJSON = function (obj, callback) {
}
}

exports.isPeerId = function (peerId) {
if (peerId.constructor && peerId.constructor.name) {
return peerId.constructor.name === 'PeerId'
}
}

function toB64Opt (val) {
if (val) {
return val.toString('base64')
Expand Down
10 changes: 10 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ describe('PeerId', () => {
})
})

it('isPeerId', (done) => {
PeerId.create((err, id) => {
expect(err).to.not.exist()
expect(PeerId.isPeerId(id)).to.equal(true)
expect(PeerId.isPeerId('aaa')).to.equal(false)
expect(PeerId.isPeerId(new Buffer('batatas'))).to.equal(false)
done()
})
})

it('throws on changing the id', (done) => {
PeerId.create((err, id) => {
expect(err).to.not.exist()
Expand Down

0 comments on commit 0acc572

Please sign in to comment.