(strings: TemplateStringsArray, ...values: any[])
| 12 | * Additionally, whitespace in empty lines is removed. |
| 13 | */ |
| 14 | export function dedent(strings: TemplateStringsArray, ...values: any[]) { |
| 15 | let joinedString = ''; |
| 16 | for (let i = 0; i < values.length; i++) { |
| 17 | joinedString += `${strings[i]}${values[i]}`; |
| 18 | } |
| 19 | joinedString += strings[strings.length - 1]; |
| 20 | |
| 21 | const matches = joinedString.match(/^[ \t]*(?=\S)/gm); |
| 22 | if (matches === null) { |
| 23 | return joinedString; |
| 24 | } |
| 25 | |
| 26 | const minLineIndent = Math.min(...matches.map((el) => el.length)); |
| 27 | const omitMinIndentRegex = new RegExp(`^[ \\t]{${minLineIndent}}`, 'gm'); |
| 28 | const omitEmptyLineWhitespaceRegex = /^[ \t]+$/gm; |
| 29 | const result = minLineIndent > 0 ? joinedString.replace(omitMinIndentRegex, '') : joinedString; |
| 30 | return result.replace(omitEmptyLineWhitespaceRegex, ''); |
| 31 | } |
no test coverage detected
searching dependent graphs…