Skip to content

Commit e93b439

Browse files
committed
BREAKING: rename functions
License: MIT Signed-off-by: Henrique Dias <[email protected]>
1 parent be1a57f commit e93b439

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

src/index.js

+24-25
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
}
@@ -121,23 +122,21 @@ function getCodeVarint (codecName) {
121122
* @param {CodecCode} code
122123
* @returns {Array.<number>}
123124
*/
124-
function getVarint (code) {
125+
function getVarintFromCode (code) {
125126
return varint.encode(code)
126127
}
127128

128129
module.exports = {
129130
addPrefix,
130131
rmPrefix,
131-
getCodec,
132-
getName,
133-
getNumber,
134-
getCode,
135-
getCodeVarint,
136-
getVarint,
137-
132+
getNameFromData,
133+
getNameFromCode,
134+
getCodeFromName,
135+
getCodeFromData,
136+
getVarintFromName,
137+
getVarintFromCode,
138138
// Make the constants top-level constants
139139
...constantToCode,
140-
141140
// Export the maps
142141
nameToVarint,
143142
constantToCode,

test/multicodec.spec.js

+27-27
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)
47+
const code = multicodec.getVarintFromCode(multicodec.KECCAK_256)
4848
expect(code).to.eql([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,28 +73,28 @@ 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

9292
it('throws error on unknown codec name when getting the code', () => {
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

@@ -104,9 +104,9 @@ describe('multicodec', () => {
104104
const buf = uint8ArrayFromString('hey')
105105
const prefixedBuf = multicodec.addPrefix(code, buf)
106106
expect(() => {
107-
multicodec.getCodec(prefixedBuf)
107+
multicodec.getNameFromData(prefixedBuf)
108108
}).to.throw(
109-
'Code 65518 not found'
109+
'Code "65518" not found'
110110
)
111111
})
112112
})

0 commit comments

Comments
 (0)