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
55 lines (52 loc) • 1.57 kB
JavaScript
import {
decodeMO,
encodeMO
} from "./chunk-EKQOSSOR.js";
import {
getWeissmanScore
} from "./chunk-MRISBIOS.js";
// src/algorithms/mo.ts
function compressWithMiddleOut(input, config) {
const preserveWhitespace = config?.preserveWhitespace ?? true;
const cleanedInput = preserveWhitespace ? input : input.replace(/\s+/g, "");
const len = cleanedInput.length;
const third = Math.floor(len / 3);
const start = cleanedInput.slice(0, third);
const end = cleanedInput.slice(len - third);
const middleOutData = `${start}...${end}`;
const targetWeissman = config?.targetWeissman || 10;
const weissmanScore = getWeissmanScore(
"middle-out",
input.length,
middleOutData.length,
targetWeissman
);
return {
original: input,
compressed: middleOutData,
originalSize: input.length,
compressedSize: middleOutData.length,
algorithm: "middle-out",
weissmanScore,
encoded: encodeMO("middle-out", middleOutData, weissmanScore)
};
}
function decompressWithMiddleOut(encoded, raw) {
if (raw) {
const reversed = encoded.replace(/μ/g, "").split("").reverse().join("");
return `[RAW_RECOVERY_MODE] ${reversed}`;
}
try {
const { compressedData } = decodeMO(encoded);
const [start, end] = compressedData.split("...");
return `${start}[...MISSING_MIDDLE...]${end}`;
} catch (e) {
const fallback = encoded.replace(/μ/g, "").split("").reverse().join("");
return `[DECODE_FAIL_FALLBACK] ${fallback}`;
}
}
export {
compressWithMiddleOut,
decompressWithMiddleOut
};