-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
56 lines (51 loc) · 1.7 KB
/
index.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { uint8ArrayToHex } from "uint8array-extras";
import { compress } from "./compress";
import { decompress } from "./decompress";
import { stdin, stdout } from "process";
import arg from "arg";
import fs from "fs";
import { prefixTreeCommand } from "./commands/prefix-tree";
import "./helpers/Uint1ArrayHelper";
const args = arg({});
if (args._[0] === "prefix-tree") {
const example = "basic";
const input = fs.readFileSync(`./examples/${example}/input.txt`, "ascii");
await prefixTreeCommand(input, example);
process.exit(0);
}
if (args._.length === 0) {
const example = "a-z";
const input = fs.readFileSync(`./examples/${example}/input.txt`, "ascii");
const { compressed, compressedSize } = compress(input);
const decompressed = decompress(compressed);
const inputSize = Buffer.byteLength(input);
if (input !== decompressed) {
console.error(`It didn't decompress properly`);
}
console.table({
input: input.slice(0, 50),
decompressed: decompressed.slice(0, 50),
inputSize,
compressed: uint8ArrayToHex(compressed).toUpperCase().slice(0, 50),
compressedSize,
compressionRatio:
((inputSize - compressedSize) / compressedSize) * 100 + "%",
});
} else {
// echo 'abbcccc' | pnpm exec vite-node index.ts compress | pnpm exec vite-node index.ts decompress
let chunks: Buffer[] = [];
stdin.on("data", (chunk) => {
chunks.push(chunk);
});
stdin.on("end", () => {
const data = Buffer.concat(chunks);
if (args._[0] === "compress") {
const { compressed } = compress(data.toString());
stdout.write(compressed);
}
if (args._[0] === "decompress") {
const decompressed = decompress(data);
stdout.write(decompressed);
}
});
}