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

add filter functionality for multiaddr #63

Merged
merged 4 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
"pre-commit": "^1.2.2"
},
"dependencies": {
"multiaddr": "^3.0.2",
"js-mafmt": "^1.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zcstarr the correct module name is mafmt

"lodash.uniqby": "^4.7.0",
"multiaddr": "^3.0.2",
"peer-id": "~0.10.5"
},
"contributors": [
Expand Down
9 changes: 9 additions & 0 deletions src/multiaddr-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class MultiaddrSet {
return this._multiaddrs.forEach(fn)
}

filterBy (maFmt) {
if (typeof maFmt !== 'object' ||
typeof maFmt.matches !== 'function' ||
typeof maFmt.partialMatch !== 'function' ||
typeof maFmt.toString !== 'function') return []

return this._multiaddrs.filter((ma) => maFmt.matches(ma))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


has (ma) {
ma = ensureMultiaddr(ma)
return this._multiaddrs.some((m) => m.equals(ma))
Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ chai.use(dirtyChai)
const expect = chai.expect
const Id = require('peer-id')
const Multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const Info = require('../src')
const peerIdJSON = require('./peer-test.json')

Expand Down Expand Up @@ -260,6 +261,16 @@ describe('peer-info', () => {
})
})

it('multiaddrs.filterBy', () => {
const ma1 = Multiaddr('/ip4/127.0.0.1/tcp/7000')
const ma2 = Multiaddr('/ip4/127.0.0.1/udp/5001')
pi.multiaddrs.add(ma1)
pi.multiaddrs.add(ma2)
const maddrs = pi.multiaddrs.filterBy(mafmt.TCP)
expect(maddrs.length).to.eq(1)
expect(maddrs[0].equals(ma1)).to.eq(true)
})

it('multiaddrs.toArray', () => {
pi.multiaddrs.add('/ip4/127.0.0.1/tcp/5001')
pi.multiaddrs.toArray().forEach((ma) => {
Expand Down