Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit addf54b

Browse files
fix: Specify types instead of extensions
1 parent 7c9b917 commit addf54b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/block.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const util = require('./util')
33

44
// Immutable block of data
5-
function Block (data, extension) {
5+
function Block (data, type) {
66
if (!data) {
77
throw new Error('Block must be constructed with data')
88
}
@@ -18,7 +18,20 @@ function Block (data, extension) {
1818
}
1919

2020
this.key = util.hash(this.data)
21-
this.extension = extension || 'data'
21+
this.type = type || 'protobuf'
2222
}
2323

24+
Object.defineProperty(Block.prototype, 'extension', {
25+
get () {
26+
switch (this.type) {
27+
case 'protobuf':
28+
return 'data'
29+
case 'ipld':
30+
return 'ipld'
31+
default:
32+
return this.type
33+
}
34+
}
35+
})
36+
2437
module.exports = Block

test/block.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ describe('block', () => {
3131

3232
expect(key.equals(block.key)).to.equal(false)
3333
})
34+
35+
it('has the right extension to type mapping', () => {
36+
const b1 = new Block('hello', 'protobuf')
37+
const b2 = new Block('hello')
38+
const b3 = new Block('hello', 'ipld')
39+
const b4 = new Block('hello', 'woot')
40+
41+
expect(b1.type).to.be.eql('protobuf')
42+
expect(b1.extension).to.be.eql('data')
43+
44+
expect(b2.type).to.be.eql('protobuf')
45+
expect(b2.extension).to.be.eql('data')
46+
47+
expect(b3.type).to.be.eql('ipld')
48+
expect(b3.extension).to.be.eql('ipld')
49+
50+
expect(b4.type).to.be.eql('woot')
51+
expect(b4.extension).to.be.eql('woot')
52+
})
3453
})

0 commit comments

Comments
 (0)