Skip to content

Commit 4222846

Browse files
authored
Merge pull request #24 from multiformats/is-addr
Add isMultiaddr
2 parents 9c423dd + 2aa7abb commit 4222846

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ const printerOverProxy = proxy.encapsulate(printer)
191191
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
192192
```
193193

194+
### Misc
195+
196+
#### `multiaddr.isMultiaddr(addr)`
197+
198+
Returns `true` if the passed in `addr` is a valid `multiaddr`.
199+
194200
## Installation
195201

196202
### npm

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@
3636
},
3737
"homepage": "https://github.com/jbenet/js-multiaddr",
3838
"dependencies": {
39-
"babel-runtime": "^6.6.1",
39+
"babel-runtime": "^6.11.6",
4040
"bs58": "^3.0.0",
41-
"ip": "^1.0.2",
42-
"lodash.filter": "^4.2.1",
43-
"lodash.map": "^4.2.1",
44-
"varint": "^4.0.0",
41+
"ip": "^1.1.3",
42+
"lodash.filter": "^4.6.0",
43+
"lodash.map": "^4.6.0",
44+
"varint": "^4.0.1",
4545
"xtend": "^4.0.1"
4646
},
4747
"devDependencies": {
48-
"aegir": "^3.0.4",
48+
"aegir": "^8.0.0",
4949
"buffer-loader": "0.0.1",
5050
"chai": "^3.5.0",
51-
"pre-commit": "^1.1.2"
51+
"pre-commit": "^1.1.3"
5252
},
5353
"contributors": [
5454
"David Dias <[email protected]>",

src/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ Multiaddr.prototype.fromStupidString = function fromStupidString (str) {
166166

167167
// patch this in
168168
Multiaddr.protocols = protocols
169+
170+
Multiaddr.isMultiaddr = function isMultiaddr (addr) {
171+
if (addr.constructor && addr.constructor.name) {
172+
return addr.constructor.name === 'Multiaddr'
173+
}
174+
175+
return Boolean(
176+
addr.fromStupidString &&
177+
addr.protos
178+
)
179+
}

test/index.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
12
/* eslint-env mocha */
23
'use strict'
34

@@ -515,4 +516,14 @@ describe('helpers', () => {
515516
)
516517
})
517518
})
519+
520+
describe('multiaddr.isMultiaddr', () => {
521+
it('handles different inputs', () => {
522+
expect(multiaddr.isMultiaddr(multiaddr('/'))).to.be.eql(true)
523+
expect(multiaddr.isMultiaddr('/')).to.be.eql(false)
524+
expect(multiaddr.isMultiaddr(123)).to.be.eql(false)
525+
526+
expect(multiaddr.isMultiaddr(Buffer('/hello'))).to.be.eql(false)
527+
})
528+
})
518529
})

0 commit comments

Comments
 (0)