strip-indent

Strip leading whitespace from every line of a string, with a dedent() helper for cleaning up multi-line template literals.

Library
npm
v4.1.1
150stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
41/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
63/100Good
Architecture65
Code Quality80
Innovation55
Learning Curve50

strip-indent is a tiny, dependency-free JavaScript utility for removing leading whitespace from every line of a string. It calculates the smallest common indentation across non-blank lines and strips exactly that much from each line, so relative indentation inside the string is preserved.

Alongside the core stripIndent() function, the package exports a dedent() helper purpose-built for multi-line template literals: it trims blank lines from the start and end of the string before stripping the shared indentation, giving clean, boundary-free output straight from a plain or tagged template literal.

What You Get

  • A default stripIndent() export that removes the smallest common leading whitespace from every line in a string
  • A named dedent() export that additionally trims leading/trailing blank lines, ideal for multi-line template literals
  • Hand-written TypeScript declarations (index.d.ts) with inline usage examples
  • Zero runtime dependencies and a single small ESM file

Common Use Cases

  • Cleaning up indented multi-line template literals before rendering or logging them
  • Normalizing indentation in code snippets pasted from editors before display or storage
  • Formatting heredoc-style embedded HTML/SQL/text blocks for output
  • Pre-processing test fixtures or CLI help text that’s indented for source readability

Under The Hood

Architecture strip-indent is a single-file ESM module (index.js) exposing two functions: a default export stripIndent() and a named export dedent(). The implementation is a straightforward regex-based transform — stripIndent() matches leading whitespace runs at the start of each line via /^[ \t]*(?=\S)/gm, tracks the minimum indent length seen across non-blank lines, then strips that many leading characters from every line with a dynamically-built RegExp. dedent() composes on top of stripIndent() rather than duplicating logic: it first trims leading/trailing whitespace-only lines with a boundary regex, then delegates the remaining string to stripIndent(). There is no internal layering, class hierarchy, or state — the whole module is two pure functions operating on strings, which is appropriate for its scope, though ‘architecture’ in the traditional sense barely applies beyond this one clean composition.

Tech Stack The package targets Node.js >=12 and ships as pure ESM (type: module), with a single exports entry pointing at index.js and no runtime dependencies at all. Type consumers get hand-written declarations in index.d.ts with JSDoc-style usage examples rather than generated-from-source types. Development tooling is minimal: ava for test running and xo (a zero-config, opinionated ESLint wrapper) for linting, both wired through a single test script (xo && ava). CI runs that script via GitHub Actions across a matrix of Node versions. There is no build or transpile step — the published index.js is the same ESM source used in development.

Code Quality test.js uses ava and is noticeably thorough for a 25-line implementation: beyond the basic stripIndent() case, it exercises numerous dedent() edge cases — CRLF line endings, mixed LF/CRLF, whitespace-only lines at both boundaries, preservation of internal blank lines and trailing spaces, and full round-trips through realistic template-literal use cases such as an HTML snippet and a JS function body. Linting is enforced via xo as a required step before tests run, and CI runs this same command across multiple Node versions on every push and pull request. There is no explicit error handling in the module itself — inputs are assumed to be strings with no validation or thrown errors for other types, a reasonable simplification for a utility this narrow in scope.

API Design The public API is minimal and ergonomic: a single default export for the common case (stripIndent) plus one named export (dedent) for the template-literal case, both taking a single string argument and returning a string synchronously, with no options object or configuration. Getting started requires nothing more than importing the function needed. Documentation is a single README with copy-pasteable examples for both functions, and the .d.ts file duplicates those examples inline as JSDoc so editors surface them via IntelliSense. The tradeoff for that simplicity is limited configurability — no options for tab width or custom indent characters — but for its narrow scope that keeps the surface area easy to learn instantly.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search