stripIndent
EditRemoves shared indentation from multiline text.
/** Removes the smallest shared indentation from non-empty lines. */export const stripIndent = (value: string): string => { const normalized = value.replace(/\r\n?/g, "\n").replace(/^\n/, "").replace(/\n[\t ]*$/, ""); const lines = normalized.split("\n"); const indents = lines .filter((line) => line.trim().length > 0) .map((line) => line.match(/^[\t ]*/)?.[0].length ?? 0); const minimum = indents.length > 0 ? Math.min(...indents) : 0; return lines.map((line) => line.slice(Math.min(minimum, line.length))).join("\n");};Download
Section titled “Download”wget -O src/lib/stripIndent.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/stripIndent.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/functions "$temp_dir" && mv "$temp_dir/stripIndent.ts" src/lib/stripIndent.ts && rm -r "$temp_dir"Examples
Section titled “Examples”import { stripIndent } from "./stripIndent";
const query = stripIndent(` SELECT * FROM users WHERE active = true`);value(string) — Multiline text.- Leading and trailing template-literal blank lines are removed.
- Line endings are normalized to
\n.