Skip to content

Commit 3877aa2

Browse files
committed
feat(json-pack): 🎸 add array Bencode encoding
1 parent b5f6984 commit 3877aa2

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/json-pack/bencode/BencodeEncoder.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ export class BencodeEncoder implements BinaryJsonEncoder {
5555
}
5656

5757
public writeNull(): void {
58-
throw new Error('Method not implemented.');
58+
throw new Error('NULL_NOT_SUPPORTED');
5959
}
6060

6161
public writeUndef(): void {
62-
throw new Error('Method not implemented.');
62+
throw new Error('UNDEF_NOT_SUPPORTED');
6363
}
6464

6565
public writeBoolean(bool: boolean): void {
66-
throw new Error('Method not implemented.');
66+
throw new Error('BOOL_NOT_SUPPORTED');
6767
}
6868

6969
public writeNumber(num: number): void {
@@ -120,7 +120,11 @@ export class BencodeEncoder implements BinaryJsonEncoder {
120120
}
121121

122122
public writeArr(arr: unknown[]): void {
123-
throw new Error('Method not implemented.');
123+
const writer = this.writer;
124+
writer.u8(0x6c); // 'l'
125+
const length = arr.length;
126+
for (let i = 0; i < length; i++) this.writeAny(arr[i]);
127+
writer.u8(0x65); // 'e'
124128
}
125129

126130
public writeObj(obj: Record<string, unknown>): void {

src/json-pack/bencode/__tests__/BencodeEncoder.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,27 @@ describe('binary', () => {
112112
});
113113
});
114114

115-
// describe('array', () => {
116-
// test('empty array', () => {
117-
// assertEncoder([]);
118-
// });
115+
describe('array', () => {
116+
test('empty array', () => {
117+
assertEncoder([], utf8`le`);
118+
});
119119

120-
// test('array with one element', () => {
121-
// assertEncoder([1]);
122-
// });
120+
test('array with one integer element', () => {
121+
assertEncoder([1], utf8`li1ee`);
122+
});
123123

124-
// test('array with two elements', () => {
125-
// assertEncoder([1, 2]);
126-
// });
124+
test('array with two integer elements', () => {
125+
assertEncoder([1, 2], utf8`li1ei2ee`);
126+
});
127127

128-
// test('array of array', () => {
129-
// assertEncoder([[123]]);
130-
// });
128+
test('array of array', () => {
129+
assertEncoder([[123]], utf8`lli123eee`);
130+
});
131131

132-
// test('array of various types', () => {
133-
// assertEncoder([0, 1.32, 'str', true, false, null, [1, 2, 3]]);
134-
// });
135-
// });
132+
test('array of various types', () => {
133+
assertEncoder([0, 1.32, 'str', [1, 2, 3]], utf8`li0ei1e3:strli1ei2ei3eee`);
134+
});
135+
});
136136

137137
// describe('object', () => {
138138
// test('empty object', () => {

0 commit comments

Comments
 (0)