(s: string)
| 449 | } |
| 450 | |
| 451 | export function unindentLines(s: string): string[] { |
| 452 | let seenNonEmpty = false; |
| 453 | const lines = s |
| 454 | .split("\n") |
| 455 | .map((line) => { |
| 456 | const trimmedLine = line.trimEnd(); |
| 457 | if (seenNonEmpty) { |
| 458 | return trimmedLine; |
| 459 | } |
| 460 | if (trimmedLine.length > 0) { |
| 461 | seenNonEmpty = true; |
| 462 | return trimmedLine; |
| 463 | } |
| 464 | return null; |
| 465 | }) |
| 466 | .filter((line) => line != null); |
| 467 | while (lines.length > 0 && lines[lines.length - 1].length === 0) { |
| 468 | lines.pop(); |
| 469 | } |
| 470 | if (lines.length === 0) { |
| 471 | return []; |
| 472 | } |
| 473 | |
| 474 | const indent = lines[0].match(/^\s*/)?.[0]; |
| 475 | if (!indent) { |
| 476 | return lines; // No indent, return as is |
| 477 | } |
| 478 | // Remove indent from the beginning of each line |
| 479 | const regex = new RegExp(`^${indent}`); |
| 480 | return lines.map((line) => line.replace(regex, "")); |
| 481 | } |
| 482 | |
| 483 | export function unindent(s: string): string { |
| 484 | return unindentLines(s).join("\n"); |
no outgoing calls
no test coverage detected