Skip to content

Commit

Permalink
BREAKING: remove print, use getName instead (#72)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias authored Dec 11, 2020
1 parent 4d81f9f commit 23f3e93
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function getCodec (prefixedData) {
}

/**
* Get the name of the codec.
* Get the name of the codec (human friendly).
*
* @param {CodecNumber} codec
* @returns {CodecName|undefined}
Expand Down Expand Up @@ -129,9 +129,6 @@ function getVarint (code) {
// Make the constants top-level constants
const constants = require('./constants')

// Human friendly names for printing, e.g. in error messages
const print = require('./print')

module.exports = {
addPrefix,
rmPrefix,
Expand All @@ -141,6 +138,5 @@ module.exports = {
getCode,
getCodeVarint,
getVarint,
print,
...constants
}
4 changes: 3 additions & 1 deletion src/int-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const nameTable = new Map()

for (const encodingName in baseTable) {
const code = baseTable[encodingName]
nameTable.set(code, /** @type {CodecName} */(encodingName))
if (!nameTable.has(code)) {
nameTable.set(code, /** @type {CodecName} */(encodingName))
}
}

module.exports = Object.freeze(nameTable)
16 changes: 0 additions & 16 deletions src/print.js

This file was deleted.

20 changes: 10 additions & 10 deletions test/multicodec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ describe('multicodec', () => {
})

it('returns the name from codec number', () => {
expect(multicodec.print[144]).to.eql('eth-block')
expect(multicodec.print[112]).to.eql('dag-pb')
expect(multicodec.print[0x0111]).to.eql('udp')
expect(multicodec.print[0xb201]).to.eql('blake2b-8')

expect(multicodec.print[multicodec.ETH_BLOCK]).to.eql('eth-block')
expect(multicodec.print[multicodec.DAG_PB]).to.eql('dag-pb')
expect(multicodec.print[multicodec.UDP]).to.eql('udp')
expect(multicodec.print[multicodec.BLAKE2B_8]).to.eql('blake2b-8')
expect(multicodec.getName(144)).to.eql('eth-block')
expect(multicodec.getName(112)).to.eql('dag-pb')
expect(multicodec.getName(0x0111)).to.eql('udp')
expect(multicodec.getName(0xb201)).to.eql('blake2b-8')

expect(multicodec.getName(multicodec.ETH_BLOCK)).to.eql('eth-block')
expect(multicodec.getName(multicodec.DAG_PB)).to.eql('dag-pb')
expect(multicodec.getName(multicodec.UDP)).to.eql('udp')
expect(multicodec.getName(multicodec.BLAKE2B_8)).to.eql('blake2b-8')
})

it('returns p2p when 0x01a5 is used', () => {
// `ipfs` and `p2p` are assigned to `0x01a5`, `ipfs` is deprecated
expect(multicodec.print[0x01a5]).to.eql('p2p')
expect(multicodec.getName(0x01a5)).to.eql('p2p')
})

it('throws error on unknown codec name when getting the code', () => {
Expand Down

0 comments on commit 23f3e93

Please sign in to comment.