Skip to content

Commit

Permalink
Merge pull request #24 from multiformats/is-addr
Browse files Browse the repository at this point in the history
Add isMultiaddr
  • Loading branch information
daviddias committed Sep 7, 2016
2 parents 9c423dd + 2aa7abb commit 4222846
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@
},
"homepage": "https://github.com/jbenet/js-multiaddr",
"dependencies": {
"babel-runtime": "^6.6.1",
"babel-runtime": "^6.11.6",
"bs58": "^3.0.0",
"ip": "^1.0.2",
"lodash.filter": "^4.2.1",
"lodash.map": "^4.2.1",
"varint": "^4.0.0",
"ip": "^1.1.3",
"lodash.filter": "^4.6.0",
"lodash.map": "^4.6.0",
"varint": "^4.0.1",
"xtend": "^4.0.1"
},
"devDependencies": {
"aegir": "^3.0.4",
"aegir": "^8.0.0",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.2"
"pre-commit": "^1.1.3"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
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 4222846

Please sign in to comment.