denoflate
例: PlantUML online serverにファイルを送ってsvgへのリンクを受け取る code:sh
code:index.ts
import { encode64 } from "./encoder.ts";
import { textToBuffer } from "./converter.ts";
const res = await fetch(Deno.args0); const text = await res.text();
console.log(http://www.plantuml.com/plantuml/svg/${encode64(deflate(textToBuffer(text), 9))});
code:converter.ts
export function textToBuffer(text: string) {
const ascii_string = unescape(encodeURIComponent(text)); // 間にこれを噛まさないと文字化けする
let buffer = new Uint8Array(ascii_string.length);
for (let i = 0; i < ascii_string.length; i++) {
bufferi = ascii_string.charCodeAt(i); }
return buffer;
}
code:sample.pu
Bob->Alice : hello
Alice->Bob : 日本語でおk
code:encoder.ts
export function encode64(data: Uint8Array) {
let r = '';
for (let i = 0; i < data.length; i += 3) {
if (i + 2 === data.length) {
r += append3bytes(datai, datai + 1, 0); } else if (i + 1 === data.length) {
r += append3bytes(datai, 0, 0); } else {
}
}
return r;
}
function encode6bit(b: number) {
if (b < 10) return String.fromCharCode(48 + b);
b -= 10;
if (b < 26) return String.fromCharCode(65 + b);
b -= 26;
if (b < 26) return String.fromCharCode(97 + b);
b -= 26;
if (b === 0) return '-';
if (b === 1) return '_';
return '?';
}
function append3bytes(b1: number, b2: number, b3: number) {
const c1 = b1 >> 2;
const c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
const c3 = ((b2 & 0xF) << 2) | (b3 >> 6);
const c4 = b3 & 0x3F;
return encode6bit(c1 & 0x3F)
+ encode6bit(c2 & 0x3F)
+ encode6bit(c3 & 0x3F)
+ encode6bit(c4 & 0x3F);
}