Skip to content

Commit b097af9

Browse files
fsdiogovmx
authored andcommitted
feat: use class-is module for type checks
1 parent 677e4ed commit b097af9

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"homepage": "https://github.com/multiformats/js-multiaddr",
3838
"dependencies": {
3939
"bs58": "^4.0.1",
40+
"class-is": "^0.5.0",
4041
"ip": "^1.1.5",
4142
"lodash.filter": "^4.6.0",
4243
"lodash.map": "^4.6.0",

src/index.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ const codec = require('./codec')
66
const protocols = require('./protocols-table')
77
const varint = require('varint')
88
const bs58 = require('bs58')
9+
const withIs = require('class-is')
910

1011
const NotImplemented = new Error('Sorry, Not Implemented Yet.')
1112

12-
exports = module.exports = Multiaddr
13-
1413
/**
1514
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
1615
* a Buffer, String or another Multiaddr instance
@@ -22,7 +21,7 @@ exports = module.exports = Multiaddr
2221
* Multiaddr('/ip4/127.0.0.1/tcp/4001')
2322
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
2423
*/
25-
function Multiaddr (addr) {
24+
const Multiaddr = withIs.proto(function (addr) {
2625
if (!(this instanceof Multiaddr)) {
2726
return new Multiaddr(addr)
2827
}
@@ -44,7 +43,7 @@ function Multiaddr (addr) {
4443
} else {
4544
throw new Error('addr must be a string, Buffer, or another Multiaddr')
4645
}
47-
}
46+
}, { className: 'Multiaddr', symbolName: '@multiformats/js-multiaddr/multiaddr' })
4847

4948
/**
5049
* Returns Multiaddr as a String
@@ -402,28 +401,6 @@ Multiaddr.prototype.fromStupidString = function fromStupidString (str) {
402401
*/
403402
Multiaddr.protocols = protocols
404403

405-
/**
406-
* Returns if something is a Multiaddr or not
407-
*
408-
* @param {Multiaddr} addr
409-
* @return {Bool} isMultiaddr
410-
* @example
411-
* Multiaddr.isMultiaddr(Multiaddr('/ip4/127.0.0.1/tcp/4001'))
412-
* // true
413-
* Multiaddr.isMultiaddr('/ip4/127.0.0.1/tcp/4001')
414-
* // false
415-
*/
416-
Multiaddr.isMultiaddr = function isMultiaddr (addr) {
417-
if (addr.constructor && addr.constructor.name) {
418-
return addr.constructor.name === 'Multiaddr'
419-
}
420-
421-
return Boolean(
422-
addr.fromStupidString &&
423-
addr.protos
424-
)
425-
}
426-
427404
/**
428405
* Returns if something is a Multiaddr that is a name
429406
*
@@ -459,3 +436,5 @@ Multiaddr.resolve = function resolve (addr, callback) {
459436
*/
460437
return callback(new Error('not implemented yet'))
461438
}
439+
440+
exports = module.exports = Multiaddr

0 commit comments

Comments
 (0)