Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

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 28, 2018
1 parent 55a87a7 commit 621c12c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"bs58": "^4.0.1",
"buffer-loader": "0.0.1",
"cids": "~0.5.2",
"class-is": "^0.4.0",
"ipfs-block": "~0.6.1",
"is-ipfs": "~0.3.2",
"multihashes": "~0.4.12",
Expand Down
3 changes: 2 additions & 1 deletion src/dag-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const mh = require('multihashes')
const assert = require('assert')
const withIs = require('class-is')

// Link represents an IPFS Merkle DAG Link between Nodes.
class DAGLink {
Expand Down Expand Up @@ -59,6 +60,6 @@ class DAGLink {
}
}

exports = module.exports = DAGLink
exports = module.exports = withIs(DAGLink, { className: 'DAGLink', symbolName: '@ipld/js-ipld-dag-pb/daglink' })
exports.create = require('./create')
exports.util = require('./util')
2 changes: 1 addition & 1 deletion src/dag-link/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const DAGLink = require('./index')

function isDagLink (link) {
return link && link.constructor && link.constructor.name === 'DAGLink'
return link && link.constructor && DAGLink.isDAGLink(link)
}

function createDagLinkFromB58EncodedHash (link) {
Expand Down
5 changes: 3 additions & 2 deletions src/dag-node/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const cloneLinks = dagNodeUtil.cloneLinks
const cloneData = dagNodeUtil.cloneData
const toDAGLink = dagNodeUtil.toDAGLink
const DAGLink = require('./../dag-link')
const DAGNode = require('./index')
const create = require('./create')

function addLink (node, link, callback) {
const links = cloneLinks(node)
const data = cloneData(node)

if ((link.constructor && link.constructor.name === 'DAGLink')) {
if ((link.constructor && DAGLink.isDAGLink(link))) {
// It's a DAGLink instance
// no need to do anything
} else if (link.constructor && link.constructor.name === 'DAGNode') {
} else if (link.constructor && DAGNode.isDAGNode(link)) {
// It's a DAGNode instance
// convert to link
link = toDAGLink(link)
Expand Down
3 changes: 2 additions & 1 deletion src/dag-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const mh = require('multihashes')
const assert = require('assert')
const withIs = require('class-is')

class DAGNode {
constructor (data, links, serialized, multihash) {
Expand Down Expand Up @@ -77,7 +78,7 @@ class DAGNode {
}
}

exports = module.exports = DAGNode
exports = module.exports = withIs(DAGNode, { className: 'DAGNode', symbolName: '@ipld/js-ipld-dag-pb/dagnode' })
exports.create = require('./create')
exports.clone = require('./clone')
exports.addLink = require('./addLink')
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function serialize (node, callback) {
let serialized

// If the node is not an instance of a DAGNode, the link.hash might be a Base58 encoded string; decode it
if (node.constructor.name !== 'DAGNode' && node.links) {
if (!DAGNode.isDAGNode(node) && node.links) {
node.links = node.links.map((link) => {
return DAGLink.util.isDagLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
Expand Down

0 comments on commit 621c12c

Please sign in to comment.