feat: reorganize builder
This commit is contained in:
parent
ff7bb041ef
commit
362b7aa15e
18 changed files with 1094 additions and 310 deletions
59
lib/utils/StringUtils.js
Normal file
59
lib/utils/StringUtils.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Utilities for string operations used throughout the Blueprint compiler.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Escapes special HTML characters in a string to prevent XSS attacks.
|
||||
* @param {string} text - The text to escape
|
||||
* @returns {string} - The escaped text
|
||||
*/
|
||||
const escapeHTML = (text) => {
|
||||
return text
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a camelCase string to kebab-case (lowercase with hyphens).
|
||||
* @param {string} str - The string to convert
|
||||
* @returns {string} - The converted string
|
||||
*/
|
||||
const toKebabCase = (str) => {
|
||||
return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
||||
};
|
||||
|
||||
/**
|
||||
* Stringify an object while handling circular references.
|
||||
* @param {Object} obj - The object to stringify
|
||||
* @returns {string} - JSON string representation
|
||||
*/
|
||||
const safeStringify = (obj) => {
|
||||
const getCircularReplacer = () => {
|
||||
const seen = new WeakSet();
|
||||
return (key, value) => {
|
||||
if (key === "parent") return "[Circular:Parent]";
|
||||
if (typeof value === "object" && value !== null) {
|
||||
if (seen.has(value)) {
|
||||
return "[Circular]";
|
||||
}
|
||||
seen.add(value);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
return JSON.stringify(obj, getCircularReplacer(), 2);
|
||||
} catch (err) {
|
||||
return `[Unable to stringify: ${err.message}]`;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
escapeHTML,
|
||||
toKebabCase,
|
||||
safeStringify
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue