Skip to content

Commit

Permalink
feat: use class-is module for type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
fsdiogo authored and vmx committed Mar 30, 2018
1 parent 677e4ed commit b097af9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"homepage": "https://github.com/multiformats/js-multiaddr",
"dependencies": {
"bs58": "^4.0.1",
"class-is": "^0.5.0",
"ip": "^1.1.5",
"lodash.filter": "^4.6.0",
"lodash.map": "^4.6.0",
Expand Down
31 changes: 5 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
const bs58 = require('bs58')
const withIs = require('class-is')

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

exports = module.exports = Multiaddr

/**
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
* a Buffer, String or another Multiaddr instance
Expand All @@ -22,7 +21,7 @@ exports = module.exports = Multiaddr
* Multiaddr('/ip4/127.0.0.1/tcp/4001')
* // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
*/
function Multiaddr (addr) {
const Multiaddr = withIs.proto(function (addr) {
if (!(this instanceof Multiaddr)) {
return new Multiaddr(addr)
}
Expand All @@ -44,7 +43,7 @@ function Multiaddr (addr) {
} else {
throw new Error('addr must be a string, Buffer, or another Multiaddr')
}
}
}, { className: 'Multiaddr', symbolName: '@multiformats/js-multiaddr/multiaddr' })

/**
* Returns Multiaddr as a String
Expand Down Expand Up @@ -402,28 +401,6 @@ Multiaddr.prototype.fromStupidString = function fromStupidString (str) {
*/
Multiaddr.protocols = protocols

/**
* Returns if something is a Multiaddr or not
*
* @param {Multiaddr} addr
* @return {Bool} isMultiaddr
* @example
* Multiaddr.isMultiaddr(Multiaddr('/ip4/127.0.0.1/tcp/4001'))
* // true
* Multiaddr.isMultiaddr('/ip4/127.0.0.1/tcp/4001')
* // false
*/
Multiaddr.isMultiaddr = function isMultiaddr (addr) {
if (addr.constructor && addr.constructor.name) {
return addr.constructor.name === 'Multiaddr'
}

return Boolean(
addr.fromStupidString &&
addr.protos
)
}

/**
* Returns if something is a Multiaddr that is a name
*
Expand Down Expand Up @@ -459,3 +436,5 @@ Multiaddr.resolve = function resolve (addr, callback) {
*/
return callback(new Error('not implemented yet'))
}

exports = module.exports = Multiaddr

0 comments on commit b097af9

Please sign in to comment.