* Dedent a string. * * This removes any whitespace that is common to all lines in the string from * each line in the string.
(s: string)
| 463 | * each line in the string. |
| 464 | */ |
| 465 | function dedent(s: string): [string, number] { |
| 466 | const lines = s.split('\n') |
| 467 | const minIndent = lines.reduce(function (minIndent, line) { |
| 468 | if (line.trim() === '') { |
| 469 | return minIndent |
| 470 | } |
| 471 | const indent = line.match(/^\s*/)?.[0]?.length || 0 |
| 472 | return Math.min(indent, minIndent) |
| 473 | }, Infinity) |
| 474 | if (minIndent === 0) { |
| 475 | return [s, minIndent] |
| 476 | } |
| 477 | return [ |
| 478 | lines |
| 479 | .map(function (line) { |
| 480 | return line.slice(minIndent) |
| 481 | }) |
| 482 | .join('\n'), |
| 483 | minIndent, |
| 484 | ] |
| 485 | } |