This module has a number of ways of converting things into CID instances - CID.parse for strings, CID.decode for Uint8Arrays, CID.asCID for things CID-shaped.
.parse and .decode will throw, .asCID will not. We generally want to validate what we've been passed as a CID, so a common pattern seems to be forming as:
function toCID (hash) {
if (typeof hash === 'string') {
return CID.parse(hash)
} else if (hash instanceof Uint8Array) {
return CID.decode(hash)
}
const cid = CID.asCID(hash)
if (!cid) {
throw new Error('Invalid CID')
}
return cid
}
If this seems familiar it may be because this is quite similar to how the constructor from the js-cids class behaves.
It is quite useful though, any objections to making this a utility method on the CID class?
This module has a number of ways of converting things into
CIDinstances -CID.parsefor strings,CID.decodefor Uint8Arrays,CID.asCIDfor things CID-shaped..parseand.decodewill throw,.asCIDwill not. We generally want to validate what we've been passed as a CID, so a common pattern seems to be forming as:If this seems familiar it may be because this is quite similar to how the constructor from the
js-cidsclass behaves.It is quite useful though, any objections to making this a utility method on the
CIDclass?