redent
Strip a string's redundant common indentation, then re-indent it to a given depth.
Repository Health
Technical Analysis
redent is a tiny, zero-config npm utility from Sindre Sorhus that normalizes the indentation of multi-line strings. It first strips the smallest common leading whitespace shared by every non-empty line, then re-applies a configurable indent a given number of times — combining the behavior of two sibling packages, strip-indent and indent-string, behind a single function call.
It’s commonly reached for when working with template literals, code generators, or CLI output where a string’s indentation needs to be reset relative to its surrounding context rather than hardcoded. The package ships as native ESM with full TypeScript definitions and has no runtime dependencies beyond the two focused utilities it composes.
What You Get
- A single default-exported function,
redent(string, count?, options?), with no configuration boilerplate - Automatic stripping of the smallest common leading whitespace across all non-empty lines before re-indenting
- A configurable
indentstring (defaults to a single space) so tabs, multiple spaces, or any custom indent character can be used - An
includeEmptyLinesoption for the edge case where blank lines should also receive the new indentation - Full TypeScript type definitions shipped alongside the JavaScript source, verified with tsd
Common Use Cases
- Normalizing indentation of multi-line template literals before printing or storing them
- Formatting generated code snippets so they match the indentation of the file they’re inserted into
- Preparing CLI help text or error messages that were authored with source-level indentation
- Building documentation or codemod tooling that needs to re-indent extracted code blocks
Under The Hood
Architecture redent is a single-function composition module: it takes a string, pipes it through strip-indent (removing the smallest common leading whitespace across all non-empty lines) and then through indent-string (adding a configurable indent to each line), returning the final normalized string. There is no internal class hierarchy or configuration object beyond the function’s own parameters — all state lives in the caller’s arguments, and the module delegates the two non-trivial parsing steps entirely to its two dependencies. This keeps the module itself to a few lines while still exposing a coherent, purpose-built API.
Tech Stack The package ships as native ESM only (package.json sets “type”: “module” with a single “exports” entry point), targets Node.js 12+, and has zero runtime dependencies beyond two other sindresorhus micro-packages (indent-string, strip-indent) pulled in for their respective single responsibilities. There is no build or transpile step — the published files are the same ones used in development. Tooling is limited to xo (an opinionated ESLint config) for linting, ava for test execution, and tsd for validating the accompanying TypeScript declaration file.
Code Quality Test coverage lives in a single test.js file run through ava, exercising the default indent behavior, a custom indent character, and the includeEmptyLines option — covering the full parameter surface of the function. Type correctness is checked separately via tsd against index.test-d.ts. Linting is enforced through xo as part of the npm test script, and all three checks (xo, ava, tsd) must pass together. There is no error-handling logic in the module itself since the function is a pure, synchronous transformation with no external I/O or failure modes to guard against.
API Design The public API is a single default export with sensible defaults (count defaults to 0, indent defaults to a single space) and one escape hatch (includeEmptyLines) for a specific edge case. Naming is self-explanatory and consistent with the sibling packages it wraps. Full TypeScript definitions ship alongside the JavaScript source, and the README documents the entire surface in a few lines — there is effectively no ramp-up cost to adopting it.