UNPKG

825 BJavaScriptView Raw
1const HexStream = require('../util/HexStream');
2
3class BaseCoder {
4 static from(component) {} // eslint-disable-line no-unused-vars
5
6 constructor({ type, name }) {
7 this.type = type;
8 this.name = name;
9 this.dynamic = false;
10 }
11
12 /**
13 * @return {Buffer}
14 */
15 encode(value) { // eslint-disable-line no-unused-vars
16 throw new Error(`${this.constructor.name}.encode not implemented`);
17 }
18
19 /**
20 * @param stream {HexStream}
21 * @return {*}
22 */
23 decode(stream) { // eslint-disable-line no-unused-vars
24 throw new Error(`${this.constructor.name}.decode not implemented`);
25 }
26
27 encodeIndex(value) {
28 return this.encode(value);
29 }
30
31 decodeIndex(hex) {
32 const stream = new HexStream(hex);
33 return this.decode(stream);
34 }
35}
36
37module.exports = BaseCoder;