(strings: TemplateStringsArray, ...values: any[])
| 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. |
no test coverage detected