From 8c49610dff2864fc132cdfd2312446386f8e9271 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Mar 2017 12:38:48 +0100 Subject: [PATCH 1/3] chore: update deps --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index bfdc489..2492e77 100644 --- a/package.json +++ b/package.json @@ -34,15 +34,15 @@ }, "homepage": "https://github.com/libp2p/js-peer-id", "devDependencies": { - "aegir": "^11.0.0", + "aegir": "^11.0.1", "chai": "^3.5.0", "dirty-chai": "^1.2.2", "pre-commit": "^1.2.2" }, "dependencies": { - "libp2p-crypto": "~0.8.6", - "multihashes": "~0.4.4", - "async": "^2.1.5" + "libp2p-crypto": "~0.8.7", + "multihashes": "~0.4.5", + "async": "^2.2.0" }, "repository": { "type": "git", From 0acc572fd32583f138c0c3dca638dd1accb7dca9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Mar 2017 13:23:18 +0100 Subject: [PATCH 2/3] feat: isPeerId --- src/index.js | 6 ++++++ test/index.spec.js | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/index.js b/src/index.js index 9cf6fb0..a4876e6 100644 --- a/src/index.js +++ b/src/index.js @@ -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') diff --git a/test/index.spec.js b/test/index.spec.js index 782747c..5cbc952 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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() From a3fe1a2f03c3e6d273f84057cd0b640c059be080 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Mar 2017 13:58:21 +0100 Subject: [PATCH 3/3] fix: avoid using constructor.name --- package.json | 7 ++++--- src/index.js | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 2492e77..f7c3ea4 100644 --- a/package.json +++ b/package.json @@ -40,9 +40,10 @@ "pre-commit": "^1.2.2" }, "dependencies": { + "async": "^2.2.0", "libp2p-crypto": "~0.8.7", - "multihashes": "~0.4.5", - "async": "^2.2.0" + "lodash": "^4.17.4", + "multihashes": "~0.4.5" }, "repository": { "type": "git", @@ -59,4 +60,4 @@ "nginnever ", "npmcdn-to-unpkg-bot " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index a4876e6..c1baba8 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,6 @@ class PeerId { } exports = module.exports = PeerId -exports.Buffer = Buffer // generation exports.create = function (opts, callback) { @@ -220,9 +219,9 @@ exports.createFromJSON = function (obj, callback) { } exports.isPeerId = function (peerId) { - if (peerId.constructor && peerId.constructor.name) { - return peerId.constructor.name === 'PeerId' - } + return Boolean(typeof peerId === 'object' && + peerId._id && + peerId._idB58String) } function toB64Opt (val) {