-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.ts
48 lines (38 loc) · 1.24 KB
/
encode.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
// eslint-disable-next-line node/no-missing-import
import assert from "./helpers/assert";
const charset = "0░▒▓│┤┐└┴├├─┼┘┌█";
export default function encode(artwork: string) {
const width = artwork.indexOf("\n");
const lines = artwork.length / (width + 1);
assert(lines === Math.round(lines));
const cells = lines * width;
assert(cells % 2 === 0); // TODO: Implement artworks with odd number of cells
const bufLen = 1 + cells / 2;
let bufPos = 0;
const buf = Buffer.alloc(bufLen);
buf[bufPos++] = width;
const writeHalfByte = (() => {
let partial: number | undefined;
return (halfByte: number) => {
if (partial === undefined) {
partial = 16 * halfByte;
} else {
buf[bufPos++] = partial + halfByte;
partial = undefined;
}
};
})();
for (let i = 0; i < artwork.length; i++) {
if (artwork[i] === "\n") {
assert(i % (width + 1) === width);
continue;
}
const charsetIndex = charset.indexOf(artwork[i]);
assert(charsetIndex > 0); // Not -1 because 0 is also invalid
writeHalfByte(charsetIndex);
}
return buf;
}
export function encodeLines(...lines: string[]) {
return encode(lines.map((line) => line + "\n").join(""));
}