sicua
Version:
A tool for analyzing project structure and dependencies
143 lines (142 loc) • 5.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentProcessor = void 0;
class ContentProcessor {
constructor() {
this.dictionary = {
keywords: {
import: "1e",
from: "1f",
const: "1c",
function: "1n",
return: "r",
export: "x",
default: "d",
interface: "i",
type: "t",
class: "cl",
},
patterns: {
useState: "1",
useEffect: "2",
useCallback: "3",
useMemo: "4",
"try\\s*{": "t{", // Escaped
"catch\\s*\\(": "c(", // Escaped with parenthesis
"await\\s+": "w", // Added word boundary
"async\\s+": "a", // Added word boundary
props: "p",
state: "s",
},
attributes: {
className: "c",
onClick: "o",
onChange: "h",
value: "v",
type: "t",
disabled: "d",
placeholder: "p",
id: "i",
name: "n",
role: "r",
"aria-label": "al",
},
styles: {
flex: "f",
"items-center": "ic",
"justify-between": "jb",
gap: "g",
grid: "gr",
"font-bold": "fb",
"text-": "t",
"space-": "s",
"bg-": "b",
rounded: "rd",
border: "br",
},
};
}
extractImports(content) {
const importRegex = /import[\s\S]*?from\s+['"].*?['"]/g;
const matches = content.match(importRegex) || [];
return matches.map((imp) => this.compressContent(imp));
}
extractLogic(content) {
// Remove imports
let logic = content.replace(/import[\s\S]*?from\s+['"].*?['"]\s*;/g, "");
// Remove exports and declarations
logic = logic.replace(/export\s+(?:default\s+)?(?:function|const|class)\s+\w+/g, "");
// Extract function bodies and class methods
const functionRegex = /(?:function|const|class)\s+\w+\s*(?:=|\(|\{)[\s\S]*?(?:\}|\));?/g;
const matches = logic.match(functionRegex) || [];
return this.compressContent(matches.join(";"));
}
extractJSX(content) {
const jsxRegex = /return\s*\(([\s\S]*?)\);/;
const match = content.match(jsxRegex);
return match ? this.compressContent(match[1].trim()) : "";
}
processComponent(component) {
if (!component.content)
return component;
const processed = {
d: this.dictionary,
i: this.extractImports(component.content),
l: this.extractLogic(component.content),
j: this.extractJSX(component.content),
};
return {
...component,
content: JSON.stringify(processed),
};
}
compressContent(content) {
let compressed = content;
Object.entries(this.dictionary).forEach(([categoryKey, mappings]) => {
const category = categoryKey;
Object.entries(mappings).forEach(([original, shortened]) => {
let regex;
switch (category) {
case "keywords":
regex = new RegExp(`\\b${original}\\b`, "g");
break;
case "patterns":
regex = new RegExp(original, "g");
break;
case "attributes":
regex = new RegExp(`${original}=`, "g");
break;
case "styles":
regex = new RegExp(`"${original}"`, "g");
break;
default:
return;
}
const replacement = category === "attributes" ? `${shortened}=` : shortened;
compressed = compressed.replace(regex, () => replacement);
});
});
return compressed;
}
decompressComponent(compressed) {
const processed = JSON.parse(compressed);
const reverseMappings = new Map();
Object.values(processed.d).forEach((mappings) => {
Object.entries(mappings).forEach(([original, shortened]) => {
reverseMappings.set(shortened, original);
});
});
let logic = processed.l;
let jsx = processed.j;
reverseMappings.forEach((original, shortened) => {
const regex = new RegExp(shortened, "g");
logic = logic.replace(regex, () => original);
jsx = jsx.replace(regex, () => original);
});
return [processed.i.join(";\n"), logic, jsx].join("\n\n");
}
processComponents(components) {
return components.map((comp) => this.processComponent(comp));
}
}
exports.ContentProcessor = ContentProcessor;