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

replace set of multiaddrs feature #8

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,3 @@ peer-info JavaScript implementation
```javascript
const PeerInfo = require('peer-info')
```

### In the Browser through `<script>` tag

Make the [peer-info.min.js](/dist/peer-info.min.js) available through your server and load it using a normal `<script>` tag, this will export the `peerId` constructor on the `window` object, such that:

```JavaScript
const PeerInfo = window.PeerInfo
```
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ function Peer (peerId) {
})
}

this.multiaddr.replace = (existing, fresh) => {
if (!Array.isArray(existing)) {
existing = [existing]
}
if (!Array.isArray(fresh)) {
fresh = [fresh]
}
existing.forEach((m) => {
this.multiaddr.rm(m)
})
fresh.forEach((m) => {
this.multiaddr.add(m)
})
}

// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}
27 changes: 27 additions & 0 deletions tests/peer-info-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,31 @@ describe('peer-info', function (done) {
expect(pi.multiaddrs.length).to.equal(1)
done()
})

it('replace multiaddr', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
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')
const mh5 = Multiaddr('/ip4/127.0.0.1/tcp/5005')
const mh6 = Multiaddr('/ip4/127.0.0.1/tcp/5006')

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

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

const old = [mh2, mh4]
const fresh = [mh5, mh6]

pi.multiaddr.replace(old, fresh)

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

done()
})
})