middleout.js
Version:
A spoof compression library that pretends to revolutionize data compression using made-up algorithms — inspired by the legendary middle-out compression from Silicon Valley
58 lines (55 loc) • 1.37 kB
JavaScript
import {
decodeMO,
encodeMO
} from "./chunk-EKQOSSOR.js";
import {
getWeissmanScore
} from "./chunk-MRISBIOS.js";
// src/algorithms/zph.ts
function compressWithZPH(input, config) {
const preserveWhitespace = config?.preserveWhitespace ?? true;
const cleanedInput = preserveWhitespace ? input : input.replace(/\s+/g, "");
let compressed = "";
let i = 0;
while (i < cleanedInput.length) {
const char = cleanedInput[i];
let count = 1;
while (i + 1 < cleanedInput.length && cleanedInput[i + 1] === char) {
count++;
i++;
}
if (count >= 3) {
compressed += `{${char}:${count}}`;
} else {
compressed += char.repeat(count);
}
i++;
}
const targetWeissman = config?.targetWeissman || 10;
const weissmanScore = getWeissmanScore(
"zph",
input.length,
compressed.length,
targetWeissman
);
return {
original: input,
compressed,
originalSize: input.length,
compressedSize: compressed.length,
algorithm: "zph",
weissmanScore,
encoded: encodeMO("zph", compressed, weissmanScore)
};
}
function decompressWithZPH(encoded) {
const { compressedData } = decodeMO(encoded);
return compressedData.replace(/\{(.)\:(\d+)\}/g, (_, char, count) => {
return char.repeat(Number(count));
});
}
export {
compressWithZPH,
decompressWithZPH
};