From 566ee111661ba625d2995eb4ce2aca5d3e7e9cd0 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 30 Jan 2026 07:09:14 +0000 Subject: [PATCH] Add utility files: spec-parser, file-writer, helpers, wizard --- src/utils/helpers.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/utils/helpers.ts diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts new file mode 100644 index 0000000..ed63d2e --- /dev/null +++ b/src/utils/helpers.ts @@ -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 = { + 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> = { + 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, ''); +}