multiformats / js-cid

CID implementation in JavaScript
MIT License
98 stars 39 forks source link

feat: add nodejs.util.inspect.custom #105

Closed bluelovers closed 4 years ago

vmx commented 4 years ago

I don't use enough node.js, but as @rvagg seems to be excited about it (thanks for the review), let's do it.

rvagg commented 4 years ago

Here's the difference.

Before:

> CID = require('./'); new CID('QmV88khHDJEXi7wo6o972MZWY661R9PhrZW6dvpFP6jnMn')
CID {
  version: 0,
  codec: 'dag-pb',
  multihash: <Buffer 12 20 64 cc f7 d7 8e 74 b5 50 54 e3 0d fc ab c1 28 d0 f4 9c 21 54 88 b6 b9 83 32 37 15 6e e0 b7 99 a9>,
  multibaseName: 'base58btc'
}

After:

> CID = require('./'); new CID('QmV88khHDJEXi7wo6o972MZWY661R9PhrZW6dvpFP6jnMn')
CID(QmV88khHDJEXi7wo6o972MZWY661R9PhrZW6dvpFP6jnMn)

You lose information in the process, but the verbosity of CID inspection has been a major hurdle and if you want that information to be printed then go ahead and print it manually--the cases where you want codec and multibase are going to be far outweighed by the cases where you just want to see that there is a CID here and I could copy it and stick it paste it somewhere else.

I wrote a JSON stringify replacer function just 1 week ago to deal with this verbosity that does almost this exact thing in this PR. I could probably just have done a util.inspect() now instead for the same data with this change. https://github.com/rvagg/js-example-dag-generate/blob/289720dad5aea1df1bf3f9ce050aa88fde96018a/example-dag-generate.js#L101-L109