Skip to content

Commit 0938d21

Browse files
committed
feat: 🎸 add "hex" codec
1 parent e0c20a8 commit 0938d21

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

‎src/codecs/hex.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type {CliCodec} from '../types';
2+
3+
export class CliCodecHex implements CliCodec<'hex'> {
4+
public readonly id = 'hex';
5+
public readonly description = 'Same as "raw", but encodes binary data as HEX octets';
6+
7+
encode(value: unknown): Uint8Array {
8+
const buf = value instanceof Uint8Array ? value : new TextEncoder().encode(String(value));
9+
const hex = Array.from(buf)
10+
.map((byte) => byte.toString(16).padStart(2, '0'))
11+
.join(' ');
12+
return new TextEncoder().encode(hex + '\n');
13+
}
14+
15+
decode(bytes: Uint8Array): unknown {
16+
throw new Error('Not available');
17+
}
18+
}

‎src/defaultCodecs.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {CliCodecRaw} from './codecs/raw';
99
import {CliCodecText} from './codecs/text';
1010
import {CliCodecTree} from './codecs/tree';
1111
import {CliCodecUbjson} from './codecs/ubjson';
12+
import {CliCodecHex} from './codecs/hex';
1213

1314
export const defaultCodecs = new CliCodecs();
1415

@@ -22,3 +23,4 @@ defaultCodecs.register(new CliCodecUbjson(writer));
2223
defaultCodecs.register(new CliCodecText());
2324
defaultCodecs.register(new CliCodecTree());
2425
defaultCodecs.register(new CliCodecRaw());
26+
defaultCodecs.register(new CliCodecHex());

0 commit comments

Comments
 (0)