dedent
A tiny ES6 string tag that strips indentation from multi-line template strings.
Repository Health
Technical Analysis
dedent is a small, dependency-free TypeScript library that removes leading indentation from multi-line JavaScript template literals. It can be used either as a template tag (dedent...) or as a plain function call (dedent(str)), and finds the minimum common indentation across all lines before stripping it, so source code can stay nicely indented without that indentation leaking into the resulting string.
Beyond the default behavior, dedent exposes a .withOptions() API for controlling how multi-line interpolated values are re-aligned, whether special characters are escaped, and whether surrounding whitespace is trimmed. An optional dedent/macro entry point integrates with babel-plugin-macros to evaluate the tag entirely at build time, eliminating any runtime cost for projects using Babel.
What You Get
- A default export usable as both a template tag (dedent
text) and a plain function (dedent(text)) - A
.withOptions()method for creating reusable, preconfigured dedent variants - Options for
alignValues(re-indenting multi-line interpolated values),escapeSpecialCharacters, andtrimWhitespace - Escape-sequence unescaping (\n, \t, \xHH, \u{…}, etc.) applied after dedenting so literal escapes in source survive the transform
- An optional
dedent/macroentry point for babel-plugin-macros that evaluates the tag at build time with zero runtime cost - Dual CJS/ESM builds with full TypeScript type declarations for both module formats
Common Use Cases
- Writing readable multi-line error messages, CLI help text, or log output inside indented code blocks
- Generating SQL, GraphQL, or shell command strings from indented template literals without leaking whitespace
- Producing fixture or snapshot-test strings that need to match exactly, independent of surrounding source indentation
- Composing multi-line prompts or templates for LLM calls where extra leading whitespace would alter output
Under The Hood
Architecture dedent is implemented as a single ~140-line TypeScript module (src/dedent.ts) built around a createDedent(options) factory that closes over the current option set and attaches a .withOptions() method to the returned tag function, so calling .withOptions() produces a new independently configured tagger rather than mutating shared state. The core dedent() function normalizes both call styles (tagged template with a raw strings array, or a single plain string argument) into a common code path, then runs a sequential pipeline: interpolate values (optionally realigning multi-line interpolations via a separate alignValue helper that inspects the current line’s indentation), split on newlines and compute the minimum indentation across all indented lines, strip that indentation from every line, optionally trim leading/trailing whitespace, and finally unescape backslash sequences that were preserved through the escaping pass. A parallel macro.js file integrates with babel-plugin-macros, walking tagged-template and call-expression AST nodes and replacing them with a precomputed string literal at build time.
Tech Stack The library is written in strict TypeScript with no runtime dependencies. It ships dual builds — a legacy CJS bundle and a modern ESM bundle — produced via two separate Babel invocations (build:legacy / build:modern), with .d.ts/.d.mts type declarations generated separately by tsup. The package.json exports map routes require() and import consumers to the correct build and type file. babel-plugin-macros is an optional peer dependency used only by the opt-in macro entry point. Testing runs on Jest, with an additional Bun-specific test target (test:bun) that exercises a Unicode-handling workaround for a known Bun runtime issue.
Code Quality The test suite (src/dedent.test.ts, 363 lines, plus a 66-line macro test file) covers both call styles, all three options, escape-sequence round-tripping, and macro evaluation, backed by Jest snapshot fixtures. The project layers on an unusually strict lint pipeline: ESLint with --max-warnings 0 across typescript-eslint, jsdoc, regexp, and perfectionist plugins, plus markdownlint, cspell spell-checking, and npm-package-json-lint — all wired into pre-commit hooks via husky and lint-staged. Naming is short and consistent throughout (dedent, mindent, alignValue), and the few lint-suppression comments in the source are narrowly scoped and explained inline.
Used by 18 apps in this directory
Botpress
AI Assistants · AI Development · Customer Support
The open-source hub for building and deploying LLM-powered AI agents with TypeScript-first tooling, 40+ integrations, and a revolutionary code-execution agent framework.
Bun
Developer Tools
An all-in-one JavaScript and TypeScript toolkit — one Rust-and-JavaScriptCore binary that replaces Node.js, npm, a bundler, and a test runner with faster equivalents.
Fern
Developer Tools
Fern turns a single OpenAPI, AsyncAPI, or Protobuf definition into type-safe SDKs for nine languages and a hosted API documentation site, all from one CLI and one source of truth.
KeystoneJS
CMS · Developer Tools
The superpowered headless CMS for developers built with GraphQL and React
Kibana
Analytics · Monitoring
Your open source window into the Elastic Stack — query, visualize, and act on data stored in Elasticsearch with real-time dashboards, AI-assisted search, and automated alerting.
LibreChat
Developer Tools · AI Assistants
Unite every major AI model in one self-hosted chat platform with agents, code execution, MCP tools, and enterprise authentication.
Llama Coder
AI Development · Low Code Platforms
Generate multi-file web apps from a single prompt using open-source LLMs — an open-source alternative to Claude Artifacts.
open-pencil
AI Design Tools · Design Tools
An open-source design editor that reads native Figma files, ships a built-in AI assistant with 100+ design tools, and offers real-time serverless collaboration — all without giving up your files.
OpenPanel
Hosting Control Panel · Devops
Docker-powered web hosting control panel that gives every user a fully isolated environment with dedicated web server, database, and networking — VPS-grade security on shared hardware.