Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Added distinctMultiaddr method (#29)
Browse files Browse the repository at this point in the history
* Added distinctMultiaddr method

* made distinctAddr filter by port and transport

* adding multi addr tests

* changing lodash require
  • Loading branch information
SidHarder authored and daviddias committed Nov 22, 2016
1 parent 64c1dc0 commit 8a6cc98
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Id = require('peer-id')
const multiaddr = require('multiaddr')
const uniqBy = require('lodash').uniqBy

exports = module.exports = PeerInfo

Expand Down Expand Up @@ -88,6 +89,13 @@ function PeerInfo (peerId) {
})
}

this.distinctMultiaddr = () => {
var result = uniqBy(this.multiaddrs, function (item) {
return [item.toOptions().port, item.toOptions().transport].join()
})
return result
}

// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}
Expand Down
77 changes: 77 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,81 @@ describe('peer-info', () => {

expect(pi.multiaddrs.length).to.equal(4)
})

it('get distinct multiaddr same transport multiple different ports', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002')
const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003')
const mh4 = Multiaddr('/ip4/127.0.0.1/tcp/5004')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)
pi.multiaddr.add(mh3)
pi.multiaddr.add(mh4)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(4)
})

it('get distinct multiaddr same transport different port', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(2)
})

it('get distinct multiaddr same transport same port', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5001')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(1)
})

it('get distinct multiaddr different transport same port', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip4/127.0.0.1/udp/5001')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(2)
})

it('get distinct multiaddr different family same port same transport', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip6/::/tcp/5001')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(1)
})

it('get distinct multiaddr different family same port multiple transports', () => {
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
const mh2 = Multiaddr('/ip6/::/tcp/5001')
const mh3 = Multiaddr('/ip6/::/udp/5002')
const mh4 = Multiaddr('/ip4/127.0.0.1/udp/5002')

pi.multiaddr.add(mh1)
pi.multiaddr.add(mh2)
pi.multiaddr.add(mh3)
pi.multiaddr.add(mh4)

var distinctMultiaddr = pi.distinctMultiaddr()
expect(distinctMultiaddr.length).to.equal(2)

expect(distinctMultiaddr[0].toOptions().family).to.equal('ipv4')
expect(distinctMultiaddr[1].toOptions().family).to.equal('ipv6')
})
})

0 comments on commit 8a6cc98

Please sign in to comment.