From 0369808b2e903bac1abb5db25575ad1bd79e54ff Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 19 Jul 2024 18:07:10 -0400 Subject: [PATCH] small improvements --- src/string/dedent.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/string/dedent.ts b/src/string/dedent.ts index ab9e3099..eb527fac 100644 --- a/src/string/dedent.ts +++ b/src/string/dedent.ts @@ -71,7 +71,12 @@ 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 @@ -79,8 +84,3 @@ export function dedent( // 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] -}