Skip to content

stripIndent

Edit

Removes shared indentation from multiline text.

stripIndent.ts
/** 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");
};
Terminal
wget -O src/lib/stripIndent.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/stripIndent.ts
stripIndent.example.ts
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.