From dc01d61139dcab32d556bb898c15fdfad1c69547 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 11 Dec 2020 18:25:02 +0100 Subject: [PATCH] BREAKING: remove print, use getName insted License: MIT Signed-off-by: Henrique Dias --- src/index.js | 6 +----- src/int-table.js | 4 +++- src/print.js | 16 ---------------- test/multicodec.spec.js | 20 ++++++++++---------- 4 files changed, 14 insertions(+), 32 deletions(-) delete mode 100644 src/print.js diff --git a/src/index.js b/src/index.js index 602354c..210521d 100644 --- a/src/index.js +++ b/src/index.js @@ -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} @@ -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, @@ -141,6 +138,5 @@ module.exports = { getCode, getCodeVarint, getVarint, - print, ...constants } diff --git a/src/int-table.js b/src/int-table.js index f28b04b..10f4cec 100644 --- a/src/int-table.js +++ b/src/int-table.js @@ -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) diff --git a/src/print.js b/src/print.js deleted file mode 100644 index fc4ec4d..0000000 --- a/src/print.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -/** @typedef {import('./generated-types').CodecName} CodecName */ -/** @typedef {import('./generated-types').NumberNameMap} NumberNameMap */ - -const { baseTable } = require('./base-table') - -const tableByCode = /** @type {NumberNameMap} */({}) - -for (const [name, code] of Object.entries(baseTable)) { - if (tableByCode[code] === undefined) { - tableByCode[code] = /** @type {CodecName} **/(name) - } -} - -module.exports = /** @type {NumberNameMap} */(Object.freeze(tableByCode)) diff --git a/test/multicodec.spec.js b/test/multicodec.spec.js index 91923bf..3eb4c78 100644 --- a/test/multicodec.spec.js +++ b/test/multicodec.spec.js @@ -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', () => {