Skip to content

Commit f088660

Browse files
authored
Merge pull request #57 from libp2p/feat/isPeerId
isPeerId
2 parents e1ffe9b + a3fe1a2 commit f088660

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@
3434
},
3535
"homepage": "https://github.com/libp2p/js-peer-id",
3636
"devDependencies": {
37-
"aegir": "^11.0.0",
37+
"aegir": "^11.0.1",
3838
"chai": "^3.5.0",
3939
"dirty-chai": "^1.2.2",
4040
"pre-commit": "^1.2.2"
4141
},
4242
"dependencies": {
43-
"libp2p-crypto": "~0.8.6",
44-
"multihashes": "~0.4.4",
45-
"async": "^2.1.5"
43+
"async": "^2.2.0",
44+
"libp2p-crypto": "~0.8.7",
45+
"lodash": "^4.17.4",
46+
"multihashes": "~0.4.5"
4647
},
4748
"repository": {
4849
"type": "git",
@@ -59,4 +60,4 @@
5960
"nginnever <[email protected]>",
6061
"npmcdn-to-unpkg-bot <[email protected]>"
6162
]
62-
}
63+
}

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ class PeerId {
9191
}
9292

9393
exports = module.exports = PeerId
94-
exports.Buffer = Buffer
9594

9695
// generation
9796
exports.create = function (opts, callback) {
@@ -219,6 +218,12 @@ exports.createFromJSON = function (obj, callback) {
219218
}
220219
}
221220

221+
exports.isPeerId = function (peerId) {
222+
return Boolean(typeof peerId === 'object' &&
223+
peerId._id &&
224+
peerId._idB58String)
225+
}
226+
222227
function toB64Opt (val) {
223228
if (val) {
224229
return val.toString('base64')

test/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ describe('PeerId', () => {
3232
})
3333
})
3434

35+
it('isPeerId', (done) => {
36+
PeerId.create((err, id) => {
37+
expect(err).to.not.exist()
38+
expect(PeerId.isPeerId(id)).to.equal(true)
39+
expect(PeerId.isPeerId('aaa')).to.equal(false)
40+
expect(PeerId.isPeerId(new Buffer('batatas'))).to.equal(false)
41+
done()
42+
})
43+
})
44+
3545
it('throws on changing the id', (done) => {
3646
PeerId.create((err, id) => {
3747
expect(err).to.not.exist()

0 commit comments

Comments
 (0)