Add utility files: spec-parser, file-writer, helpers, wizard
This commit is contained in:
65
src/utils/helpers.ts
Normal file
65
src/utils/helpers.ts
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
export function capitalize(str: string): string {
|
||||||
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function camelCase(str: string): string {
|
||||||
|
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function kebabCase(str: string): string {
|
||||||
|
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function snakeCase(str: string): string {
|
||||||
|
return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function quoteString(str: string, language: string): string {
|
||||||
|
const quotes: Record<string, { single: string; double: string }> = {
|
||||||
|
python: { single: "'", double: '"' },
|
||||||
|
go: { single: "'", double: '"' },
|
||||||
|
rust: { single: "'", double: '"' },
|
||||||
|
javascript: { single: "'", double: '"' },
|
||||||
|
typescript: { single: "'", double: '"' },
|
||||||
|
};
|
||||||
|
|
||||||
|
const langQuotes = quotes[language] || quotes.javascript;
|
||||||
|
|
||||||
|
if (str.includes(langQuotes.single) && str.includes(langQuotes.double)) {
|
||||||
|
return `${langQuotes.single}${str.replace(new RegExp(langQuotes.single, 'g'), `\\${langQuotes.single}`)}${langQuotes.single}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str.includes(langQuotes.single)) {
|
||||||
|
return `${langQuotes.double}${str}${langQuotes.double}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${langQuotes.single}${str}${langQuotes.single}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function escapeString(str: string, language: string): string {
|
||||||
|
const escapes: Record<string, Record<string, string>> = {
|
||||||
|
python: { '\\': '\\\\', "'": "\\'", '"': '\\"', '\n': '\\n', '\t': '\\t' },
|
||||||
|
go: { '\\': '\\\\', '"': '\\"', '\n': '\\n', '\t': '\\t' },
|
||||||
|
rust: { '\\': '\\\\', '"': '\\"', '\n': '\\n', '\t': '\\t' },
|
||||||
|
javascript: { '\\': '\\\\', "'": "\\'", '"': '\\"', '\n': '\\n', '\t': '\\t', '`': '\\`' },
|
||||||
|
typescript: { '\\': '\\\\', "'": "\\'", '"': '\\"', '\n': '\\n', '\t': '\\t', '`': '\\`' },
|
||||||
|
};
|
||||||
|
|
||||||
|
const langEscapes = escapes[language] || escapes.javascript;
|
||||||
|
|
||||||
|
let result = str;
|
||||||
|
for (const [char, escape] of Object.entries(langEscapes)) {
|
||||||
|
result = result.split(char).join(escape);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function indent(text: string, spaces: number): string {
|
||||||
|
const indentStr = ' '.repeat(spaces);
|
||||||
|
return text.split('\n').map(line => line ? indentStr + line : line).join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function trimEndNewlines(str: string): string {
|
||||||
|
return str.replace(/\n+$/g, '');
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user