Skip to content

Commit b764341

Browse files
committed
refactor: rename functions to explicit names
Breaking refactorings: - Renamed getCodec to getNameFromData - Renamed getName to getNameFromCode - Renamed getNumber to getCodeFromName - Renamed getCode to getCodeFromData - Renamed getCodeVarint to getVarintFromName - Renamed getVarint to getVarintFromCode - getVarintFromCode returns Uint8Array instead of Array For backwards compatibility, all old names were kept as alias, and getVarint returns a regular Array as it did before. This does not break compatibility. They are all marked as @deprecated. License: MIT Signed-off-by: Henrique Dias <[email protected]>
1 parent d26166b commit b764341

File tree

3 files changed

+131
-50
lines changed

3 files changed

+131
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ console.log(multicodec.DAG_CBOR)
4545
// 113
4646

4747
// To get the string representation of a codec, e.g. for error messages:
48-
console.log(multicodec.print[113])
48+
console.log(multicodec.getNameFromCode(113))
4949
// dag-cbor
5050
```
5151

src/index.js

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function addPrefix (multicodecStrOrCode, data) {
3838
throw new Error('multicodec not recognized')
3939
}
4040
}
41+
4142
return uint8ArrayConcat([prefix, data], prefix.length + data.length)
4243
}
4344

@@ -53,27 +54,27 @@ function rmPrefix (data) {
5354
}
5455

5556
/**
56-
* Get the codec of the prefixed data.
57+
* Get the codec name of the prefixed data.
5758
*
5859
* @param {Uint8Array} prefixedData
5960
* @returns {CodecName}
6061
*/
61-
function getCodec (prefixedData) {
62+
function getNameFromData (prefixedData) {
6263
const code = varint.decode(prefixedData)
63-
const codecName = codeToName[code]
64-
if (codecName === undefined) {
65-
throw new Error(`Code ${code} not found`)
64+
const name = codeToName[code]
65+
if (name === undefined) {
66+
throw new Error(`Code "${code}" not found`)
6667
}
67-
return codecName
68+
return name
6869
}
6970

7071
/**
71-
* Get the name of the codec (human friendly).
72+
* Get the codec name from a code.
7273
*
7374
* @param {CodecCode} codec
74-
* @returns {CodecName|undefined}
75+
* @returns {CodecName}
7576
*/
76-
function getName (codec) {
77+
function getNameFromCode (codec) {
7778
return codeToName[codec]
7879
}
7980

@@ -83,10 +84,10 @@ function getName (codec) {
8384
* @param {CodecName} name
8485
* @returns {CodecCode}
8586
*/
86-
function getNumber (name) {
87+
function getCodeFromName (name) {
8788
const code = nameToCode[name]
8889
if (code === undefined) {
89-
throw new Error('Codec `' + name + '` not found')
90+
throw new Error(`Codec "${name}" not found`)
9091
}
9192
return code
9293
}
@@ -97,20 +98,20 @@ function getNumber (name) {
9798
* @param {Uint8Array} prefixedData
9899
* @returns {CodecCode}
99100
*/
100-
function getCode (prefixedData) {
101+
function getCodeFromData (prefixedData) {
101102
return varint.decode(prefixedData)
102103
}
103104

104105
/**
105106
* Get the code as varint of a codec name.
106107
*
107-
* @param {CodecName} codecName
108+
* @param {CodecName} name
108109
* @returns {Uint8Array}
109110
*/
110-
function getCodeVarint (codecName) {
111-
const code = nameToVarint[codecName]
111+
function getVarintFromName (name) {
112+
const code = nameToVarint[name]
112113
if (code === undefined) {
113-
throw new Error('Codec `' + codecName + '` not found')
114+
throw new Error(`Codec "${name}" not found`)
114115
}
115116
return code
116117
}
@@ -119,25 +120,96 @@ function getCodeVarint (codecName) {
119120
* Get the varint of a code.
120121
*
121122
* @param {CodecCode} code
123+
* @returns {Uint8Array}
124+
*/
125+
function getVarintFromCode (code) {
126+
return util.varintEncode(code)
127+
}
128+
129+
/**
130+
* Get the codec name of the prefixed data.
131+
*
132+
* @deprecated use getNameFromData instead.
133+
* @param {Uint8Array} prefixedData
134+
* @returns {CodecName}
135+
*/
136+
function getCodec (prefixedData) {
137+
return getNameFromData(prefixedData)
138+
}
139+
140+
/**
141+
* Get the codec name from a code.
142+
*
143+
* @deprecated use getNameFromCode instead.
144+
* @param {CodecCode} codec
145+
* @returns {CodecName}
146+
*/
147+
function getName (codec) {
148+
return getNameFromCode(codec)
149+
}
150+
151+
/**
152+
* Get the code of the codec
153+
*
154+
* @deprecated use getCodeFromName instead.
155+
* @param {CodecName} name
156+
* @returns {CodecCode}
157+
*/
158+
function getNumber (name) {
159+
return getCodeFromName(name)
160+
}
161+
162+
/**
163+
* Get the code of the prefixed data.
164+
*
165+
* @deprecated use getCodeFromData instead.
166+
* @param {Uint8Array} prefixedData
167+
* @returns {CodecCode}
168+
*/
169+
function getCode (prefixedData) {
170+
return getCodeFromData(prefixedData)
171+
}
172+
173+
/**
174+
* Get the code as varint of a codec name.
175+
*
176+
* @deprecated use getVarintFromName instead.
177+
* @param {CodecName} name
178+
* @returns {Uint8Array}
179+
*/
180+
function getCodeVarint (name) {
181+
return getVarintFromName(name)
182+
}
183+
184+
/**
185+
* Get the varint of a code.
186+
*
187+
* @deprecated use getVarintFromCode instead.
188+
* @param {CodecCode} code
122189
* @returns {Array.<number>}
123190
*/
124191
function getVarint (code) {
125-
return varint.encode(code)
192+
return Array.from(getVarintFromCode(code))
126193
}
127194

128195
module.exports = {
129196
addPrefix,
130197
rmPrefix,
198+
getNameFromData,
199+
getNameFromCode,
200+
getCodeFromName,
201+
getCodeFromData,
202+
getVarintFromName,
203+
getVarintFromCode,
204+
// Deprecated
131205
getCodec,
132206
getName,
133207
getNumber,
134208
getCode,
135209
getCodeVarint,
136210
getVarint,
137-
138211
// Make the constants top-level constants
139212
...constantToCode,
140-
141213
// Export the maps
142214
nameToVarint,
143215
nameToCode,

test/multicodec.spec.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,56 @@ describe('multicodec', () => {
1313
it('add prefix through multicodec (string)', () => {
1414
const buf = uint8ArrayFromString('hey')
1515
const prefixedBuf = multicodec.addPrefix('protobuf', buf)
16-
expect(multicodec.getCodec(prefixedBuf)).to.equal('protobuf')
16+
expect(multicodec.getNameFromData(prefixedBuf)).to.equal('protobuf')
1717
expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf))
1818
})
1919

2020
it('add prefix through code (code)', () => {
2121
const buf = uint8ArrayFromString('hey')
2222
const prefixedBuf = multicodec.addPrefix(uint8ArrayFromString('70', 'base16'), buf)
23-
expect(multicodec.getCodec(prefixedBuf)).to.equal('dag-pb')
23+
expect(multicodec.getNameFromData(prefixedBuf)).to.equal('dag-pb')
2424
expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf))
2525
})
2626

2727
it('add multibyte varint prefix (eth-block) through multicodec (string)', () => {
2828
const buf = uint8ArrayFromString('hey')
2929
const prefixedBuf = multicodec.addPrefix('eth-block', buf)
30-
expect(multicodec.getCodec(prefixedBuf)).to.equal('eth-block')
30+
expect(multicodec.getNameFromData(prefixedBuf)).to.equal('eth-block')
3131
expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf))
3232
})
3333

3434
it('returns code via codec name', () => {
35-
const code = multicodec.getCodeVarint('keccak-256')
35+
const code = multicodec.getVarintFromName('keccak-256')
3636
expect(code).to.eql(uint8ArrayFromString('1b', 'base16'))
3737
})
3838

3939
it('returns code from prefixed data', () => {
4040
const buf = uint8ArrayFromString('hey')
4141
const prefixedBuf = multicodec.addPrefix('dag-cbor', buf)
42-
const code = multicodec.getCode(prefixedBuf)
42+
const code = multicodec.getCodeFromData(prefixedBuf)
4343
expect(code).to.eql(multicodec.DAG_CBOR)
4444
})
4545

4646
it('returns varint from code', () => {
47-
const code = multicodec.getVarint(multicodec.KECCAK_256)
48-
expect(code).to.eql([0x1b])
47+
const code = multicodec.getVarintFromCode(multicodec.KECCAK_256)
48+
expect(code).to.eql(Uint8Array.from([0x1b]))
4949
})
5050

5151
it('returns the codec name from code', () => {
52-
expect(multicodec.getName(144)).to.eql('eth-block')
53-
expect(multicodec.getName(112)).to.eql('dag-pb')
54-
expect(multicodec.getName(0xb201)).to.eql('blake2b-8')
52+
expect(multicodec.getNameFromCode(144)).to.eql('eth-block')
53+
expect(multicodec.getNameFromCode(112)).to.eql('dag-pb')
54+
expect(multicodec.getNameFromCode(0xb201)).to.eql('blake2b-8')
5555
})
5656

5757
it('returns the codec number from name', () => {
58-
expect(multicodec.getNumber('eth-block')).to.eql(144)
59-
expect(multicodec.getNumber('dag-pb')).to.eql(112)
60-
expect(multicodec.getNumber('blake2b-8')).to.eql(0xb201)
58+
expect(multicodec.getCodeFromName('eth-block')).to.eql(144)
59+
expect(multicodec.getCodeFromName('dag-pb')).to.eql(112)
60+
expect(multicodec.getCodeFromName('blake2b-8')).to.eql(0xb201)
6161
})
6262

6363
it('returns all codec numbers from names', () => {
6464
for (const name in nameToCode) {
65-
expect(multicodec.getNumber(/** @type {CodecName} */(name))).to.eql(nameToCode[name])
65+
expect(multicodec.getCodeFromName(/** @type {CodecName} */(name))).to.eql(nameToCode[name])
6666
}
6767
})
6868

@@ -73,40 +73,49 @@ describe('multicodec', () => {
7373
})
7474

7575
it('returns the name from codec number', () => {
76-
expect(multicodec.getName(144)).to.eql('eth-block')
77-
expect(multicodec.getName(112)).to.eql('dag-pb')
78-
expect(multicodec.getName(0x0111)).to.eql('udp')
79-
expect(multicodec.getName(0xb201)).to.eql('blake2b-8')
80-
81-
expect(multicodec.getName(multicodec.ETH_BLOCK)).to.eql('eth-block')
82-
expect(multicodec.getName(multicodec.DAG_PB)).to.eql('dag-pb')
83-
expect(multicodec.getName(multicodec.UDP)).to.eql('udp')
84-
expect(multicodec.getName(multicodec.BLAKE2B_8)).to.eql('blake2b-8')
76+
expect(multicodec.getNameFromCode(144)).to.eql('eth-block')
77+
expect(multicodec.getNameFromCode(112)).to.eql('dag-pb')
78+
expect(multicodec.getNameFromCode(0x0111)).to.eql('udp')
79+
expect(multicodec.getNameFromCode(0xb201)).to.eql('blake2b-8')
80+
81+
expect(multicodec.getNameFromCode(multicodec.ETH_BLOCK)).to.eql('eth-block')
82+
expect(multicodec.getNameFromCode(multicodec.DAG_PB)).to.eql('dag-pb')
83+
expect(multicodec.getNameFromCode(multicodec.UDP)).to.eql('udp')
84+
expect(multicodec.getNameFromCode(multicodec.BLAKE2B_8)).to.eql('blake2b-8')
8585
})
8686

8787
it('returns p2p when 0x01a5 is used', () => {
8888
// `ipfs` and `p2p` are assigned to `0x01a5`, `ipfs` is deprecated
89-
expect(multicodec.getName(0x01a5)).to.eql('p2p')
89+
expect(multicodec.getNameFromCode(0x01a5)).to.eql('p2p')
9090
})
9191

92-
it('throws error on unknown codec name when getting the code', () => {
92+
it('throws error on unknown codec name when getting the varint from name', () => {
9393
expect(() => {
9494
// @ts-expect-error
95-
multicodec.getCodeVarint('this-codec-doesnt-exist')
95+
multicodec.getVarintFromName('this-codec-doesnt-exist')
9696
}).to.throw(
97-
'Codec `this-codec-doesnt-exist` not found'
97+
'Codec "this-codec-doesnt-exist" not found'
9898
)
9999
})
100100

101-
it('throws error on unknown codec name when getting the codec', () => {
101+
it('throws error on unknown codec name when getting the code from name', () => {
102+
expect(() => {
103+
// @ts-expect-error
104+
multicodec.getCodeFromName('this-codec-doesnt-exist')
105+
}).to.throw(
106+
'Codec "this-codec-doesnt-exist" not found'
107+
)
108+
})
109+
110+
it('throws error on unknown codec name when getting the name from the code', () => {
102111
const code = uint8ArrayFromString('ffee', 'base16')
103112

104113
const buf = uint8ArrayFromString('hey')
105114
const prefixedBuf = multicodec.addPrefix(code, buf)
106115
expect(() => {
107-
multicodec.getCodec(prefixedBuf)
116+
multicodec.getNameFromData(prefixedBuf)
108117
}).to.throw(
109-
'Code 65518 not found'
118+
'Code "65518" not found'
110119
)
111120
})
112121
})

0 commit comments

Comments
 (0)