| 1 | export function removeQuotesAndEscapes(input: string): string { |
| 2 | let output = input.trim(); |
| 3 | |
| 4 | // Replace smart quotes |
| 5 | output = output.replaceAll("“", '"'); |
| 6 | output = output.replaceAll("”", '"'); |
| 7 | output = output.replaceAll("‘", "'"); |
| 8 | output = output.replaceAll("’", "'"); |
| 9 | |
| 10 | // Remove escapes |
| 11 | output = output.replaceAll('\\"', '"'); |
| 12 | output = output.replaceAll("\\'", "'"); |
| 13 | output = output.replaceAll("\\n", "\n"); |
| 14 | output = output.replaceAll("\\t", "\t"); |
| 15 | output = output.replaceAll("\\\\", "\\"); |
| 16 | while ( |
| 17 | (output.startsWith('"') && output.endsWith('"')) || |
| 18 | (output.startsWith("'") && output.endsWith("'")) |
| 19 | ) { |
| 20 | output = output.slice(1, -1); |
| 21 | } |
| 22 | |
| 23 | while (output.startsWith("`") && output.endsWith("`")) { |
| 24 | output = output.slice(1, -1); |
| 25 | } |
| 26 | |
| 27 | return output; |
| 28 | } |
| 29 | |
| 30 | export function dedentAndGetCommonWhitespace(s: string): [string, string] { |
| 31 | const lines = s.split("\n"); |
| 32 | if (lines.length === 0 || (lines[0].trim() === "" && lines.length === 1)) { |
| 33 | return ["", ""]; |
| 34 | } |
| 35 | |
| 36 | // Longest common whitespace prefix |
| 37 | let lcp = lines[0].split(lines[0].trim())[0]; |
| 38 | // Iterate through the lines |
| 39 | for (let i = 1; i < lines.length; i++) { |
| 40 | // Empty lines are wildcards |
| 41 | if (lines[i].trim() === "") { |
| 42 | continue; // hey that's us! |
| 43 | } |
| 44 | |
| 45 | if (lcp === undefined) { |
| 46 | lcp = lines[i].split(lines[i].trim())[0]; |
| 47 | } |
| 48 | |
| 49 | // Iterate through the leading whitespace characters of the current line |
| 50 | for (let j = 0; j < lcp.length; j++) { |
| 51 | // If it doesn't have the same whitespace as lcp, then update lcp |
| 52 | if (j >= lines[i].length || lcp[j] !== lines[i][j]) { |
| 53 | lcp = lcp.slice(0, j); |
| 54 | if (lcp === "") { |
| 55 | return [s, ""]; |
| 56 | } |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (lcp === undefined) { |
| 63 | return [s, ""]; |
| 64 | } |
| 65 | |
| 66 | return [lines.map((x) => x.replace(lcp, "")).join("\n"), lcp]; |
| 67 | } |
| 68 | |
| 69 | export function getMarkdownLanguageTagForFile(filepath: string): string { |
| 70 | const extToLangMap: { [key: string]: string } = { |
| 71 | py: "python", |
| 72 | js: "javascript", |
| 73 | jsx: "jsx", |
| 74 | tsx: "tsx", |
| 75 | ts: "typescript", |
| 76 | java: "java", |
| 77 | class: "java", //.class files decompile to Java |
| 78 | go: "go", |
| 79 | rb: "ruby", |
| 80 | rs: "rust", |
| 81 | c: "c", |
| 82 | cpp: "cpp", |
| 83 | cs: "csharp", |
| 84 | php: "php", |
| 85 | scala: "scala", |
| 86 | swift: "swift", |
| 87 | kt: "kotlin", |
| 88 | md: "markdown", |
| 89 | json: "json", |
| 90 | html: "html", |
| 91 | css: "css", |
| 92 | sh: "shell", |
| 93 | yaml: "yaml", |
| 94 | toml: "toml", |
| 95 | tex: "latex", |
| 96 | sql: "sql", |
| 97 | ps1: "powershell", |
| 98 | }; |
| 99 | |
| 100 | const ext = sanitizeExtension(filepath.split(".").pop()); |
| 101 | return ext ? (extToLangMap[ext] ?? ext) : ""; |
| 102 | } |
| 103 | |
| 104 | function sanitizeExtension(ext?: string): string | undefined { |
| 105 | if (ext) { |
| 106 | //ignore ranges in extension eg. "java (11-23)" |
| 107 | const match = ext.match(/^(\S+)\s*(\(.*\))?$/); |
| 108 | if (match) { |
| 109 | ext = match[1]; |
| 110 | } |
| 111 | } |
| 112 | return ext; |
| 113 | } |
| 114 | |
| 115 | export function copyOf(obj: any): any { |
| 116 | if (obj === null || obj === undefined) { |
| 117 | return obj; |
| 118 | } |
| 119 | return JSON.parse(JSON.stringify(obj)); |
| 120 | } |
| 121 | |
| 122 | export function deduplicateArray<T>( |
| 123 | array: T[], |
| 124 | equal: (a: T, b: T) => boolean, |
| 125 | ): T[] { |
| 126 | const result: T[] = []; |
| 127 | |
| 128 | for (const item of array) { |
| 129 | if (!result.some((existingItem) => equal(existingItem, item))) { |
| 130 | result.push(item); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | export type TODO = any; |
| 138 | |
| 139 | export function dedent(strings: TemplateStringsArray, ...values: any[]) { |
| 140 | let raw = ""; |
| 141 | for (let i = 0; i < strings.length; i++) { |
| 142 | raw += strings[i]; |
| 143 | |
| 144 | // Handle the value if it exists |
| 145 | if (i < values.length) { |
| 146 | let value = String(values[i]); |
| 147 | // If the value contains newlines, we need to adjust the indentation |
| 148 | if (value.includes("\n")) { |
| 149 | // Find the indentation level of the last line in strings[i] |
| 150 | let lines = strings[i].split("\n"); |
| 151 | let lastLine = lines[lines.length - 1]; |
| 152 | let match = lastLine.match(/(^|\n)([^\S\n]*)$/); |
| 153 | let indent = match ? match[2] : ""; |
| 154 | // Add indentation to all lines except the first line of value |
| 155 | let valueLines = value.split("\n"); |
| 156 | valueLines = valueLines.map((line, index) => |
| 157 | index === 0 ? line : indent + line, |
| 158 | ); |
| 159 | value = valueLines.join("\n"); |
| 160 | } |
| 161 | raw += value; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Now dedent the full string |
| 166 | let result = raw.replace(/^\n/, "").replace(/\n\s*$/, ""); |
| 167 | let lines = result.split("\n"); |
| 168 | |
| 169 | // Remove leading/trailing blank lines |
| 170 | while (lines.length > 0 && lines[0].trim() === "") { |
| 171 | lines.shift(); |
| 172 | } |
| 173 | while (lines.length > 0 && lines[lines.length - 1].trim() === "") { |
| 174 | lines.pop(); |
| 175 | } |
| 176 | |
| 177 | // Calculate minimum indentation (excluding empty lines) |
| 178 | let minIndent = lines.reduce((min: any, line: any) => { |
| 179 | if (line.trim() === "") return min; |
| 180 | let match = line.match(/^(\s*)/); |
| 181 | let indent = match ? match[1].length : 0; |
| 182 | return min === null ? indent : Math.min(min, indent); |
| 183 | }, null); |
| 184 | |
| 185 | if (minIndent !== null && minIndent > 0) { |
| 186 | // Remove the minimum indentation from each line |
| 187 | lines = lines.map((line) => line.slice(minIndent)); |
| 188 | } |
| 189 | |
| 190 | return lines.join("\n"); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Removes code blocks from a message. |
| 195 | * |
| 196 | * Return modified message text. |
| 197 | */ |
| 198 | export function removeCodeBlocksAndTrim(text: string): string { |
| 199 | const codeBlockRegex = /```[\s\S]*?```/g; |
| 200 | |
| 201 | // Remove code blocks from the message text |
| 202 | const textWithoutCodeBlocks = text.replace(codeBlockRegex, ""); |
| 203 | |
| 204 | return textWithoutCodeBlocks.trim(); |
| 205 | } |
| 206 | |
| 207 | export function splitCamelCaseAndNonAlphaNumeric(value: string) { |
| 208 | return value |
| 209 | .split(/(?<=[a-z0-9])(?=[A-Z])|[^a-zA-Z0-9]/) |
| 210 | .filter((t) => t.length > 0) |
| 211 | .map((t) => t.toLowerCase()); |
| 212 | } |