Skip to content

feat(dedent): add dedent function to remove common indentation#1577

Open
KimKyuHoi wants to merge 5 commits intotoss:mainfrom
KimKyuHoi:feat/string-indent
Open

feat(dedent): add dedent function to remove common indentation#1577
KimKyuHoi wants to merge 5 commits intotoss:mainfrom
KimKyuHoi:feat/string-indent

Conversation

@KimKyuHoi
Copy link

Summary

image

This PR adds a dedent function that removes the common leading indentation from multi-line strings.

Closes #842

TC39 String.dedent Proposal (Stage 2)

This implementation follows the TC39 String.dedent proposal specification:

TC39 Spec Supported
Tagged template literal
Tag composition (dedent(otherTag)...``)
Remove opening/closing empty lines
Calculate common indentation
Preserve relative indentation

Additional Feature (Beyond TC39)

In addition to the TC39 spec, this implementation also supports regular function calls:

// TC39 spec only defines tagged template usage
dedent`
  hello
  world
`;
// We also support regular function call for convenience
dedent("  hello\n  world");

Usage

import { dedent } from 'es-toolkit/string';

// 1. Tagged template literal (TC39)
const code = dedent`
  function hello() {
    console.log('world');
  }
`;
// → "function hello() {\n  console.log('world');\n}"

// 2. Regular function (additional)
dedent("  line 1\n    line 2");
// → "line 1\n  line 2"

// 3. Tag composition (TC39)
const html = (strings, ...values) => strings.join('');
dedent(html)`
  <div>
    <span>Hello</span>
  </div>
`;

References

@vercel
Copy link

vercel bot commented Jan 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
es-toolkit Error Error Jan 23, 2026 7:09am

Request Review

@KimKyuHoi KimKyuHoi changed the title Feat/string indent feat(dedent): add dedent function to remove common indentation Jan 14, 2026
@KimKyuHoi
Copy link
Author

@raon0211 @dayongkr
Hello guys!
if you have time, would you be able to do a code review? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: dedent

2 participants