Skip to content

Commit

Permalink
feat: add isMultiaddr method
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 7, 2016
1 parent 093665d commit 2aa7abb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ const printerOverProxy = proxy.encapsulate(printer)
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
```

### Misc

#### `multiaddr.isMultiaddr(addr)`

Returns `true` if the passed in `addr` is a valid `multiaddr`.

## Installation

### npm
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,14 @@ Multiaddr.prototype.fromStupidString = function fromStupidString (str) {

// patch this in
Multiaddr.protocols = protocols

Multiaddr.isMultiaddr = function isMultiaddr (addr) {
if (addr.constructor && addr.constructor.name) {
return addr.constructor.name === 'Multiaddr'
}

return Boolean(
addr.fromStupidString &&
addr.protos
)
}
11 changes: 11 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

Expand Down Expand Up @@ -515,4 +516,14 @@ describe('helpers', () => {
)
})
})

describe('multiaddr.isMultiaddr', () => {
it('handles different inputs', () => {
expect(multiaddr.isMultiaddr(multiaddr('/'))).to.be.eql(true)
expect(multiaddr.isMultiaddr('/')).to.be.eql(false)
expect(multiaddr.isMultiaddr(123)).to.be.eql(false)

expect(multiaddr.isMultiaddr(Buffer('/hello'))).to.be.eql(false)
})
})
})

0 comments on commit 2aa7abb

Please sign in to comment.