| 11 | * literal. The smallest common indentation will be omitted. |
| 12 | */ |
| 13 | export function dedent(strings: TemplateStringsArray, ...values: any[]) { |
| 14 | let joinedString = ''; |
| 15 | for (let i = 0; i < values.length; i++) { |
| 16 | joinedString += `${strings[i]}${values[i]}`; |
| 17 | } |
| 18 | joinedString += strings[strings.length - 1]; |
| 19 | |
| 20 | const matches = joinedString.match(/^[ \t]*(?=\S)/gm); |
| 21 | if (matches === null) { |
| 22 | return joinedString; |
| 23 | } |
| 24 | |
| 25 | const minLineIndent = Math.min(...matches.map((el) => el.length)); |
| 26 | const omitMinIndentRegex = new RegExp(`^[ \\t]{${minLineIndent}}`, 'gm'); |
| 27 | return minLineIndent > 0 ? joinedString.replace(omitMinIndentRegex, '') : joinedString; |
| 28 | } |