-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (22 loc) · 979 Bytes
/
index.js
File metadata and controls
29 lines (22 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import BencodeParser from './bencoder.js';
console.log('Encoding 42:', BencodeParser.encode(42));
console.log('Encoding "spam":', BencodeParser.encode('spam'));
console.log('Encoding [1, 2, 3]:', BencodeParser.encode([1, 2, 3]));
console.log('Encoding {key: "value"}:', BencodeParser.encode({ key: 'value' }));
console.log('Encoding nested:', BencodeParser.encode({ info: { length: 1024 } }));
console.log('-----------');
const tests = [
'i42e', // integer: 42
'4:spam', // string: "spam"
'li1ei2ei3ee', // list: [1, 2, 3]
'd3:key5:valuee', // dict: {key: "value"}
'd4:infod6:lengthi1024eeee', // nested dictitonayr
];
tests.forEach(test => {
console.log(`Parsing: ${test}`);
const parser = new BencodeParser(test);
const result = parser.parse();
console.log('Result:', result);
console.log('-----------');
});
console.log(BencodeParser.decode(BencodeParser.encode({ cat: 1, dog: 2 })));