Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 19, 2024
1 parent f43a735 commit 0369808
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/string/dedent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ export function dedent(
text = text[0]
}

const indent = values[0] ?? detectIndent(text)
// If no indent is provided, use the indentation of the first
// non-empty line.
const indent = values[0] ?? text.match(/^[ \t]*(?=\S)/m)?.[0]

// Note: Lines with an indent less than the removed indent will not
// be changed.
const output = indent
? text.replace(new RegExp(`^${indent}`, 'gm'), '')
: text

// Remove the first and last lines (if empty).
return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '')
}

// Find the indentation of the first non-empty line.
function detectIndent(text: string) {
return text.match(/^[ \t]*(?=\S)/m)?.[0]
}

0 comments on commit 0369808

Please sign in to comment.