Skip to content

Add maxLength option - #78

Open
AmineYagoub wants to merge 1 commit into
sindresorhus:mainfrom
AmineYagoub:feat/max-length
Open

AmineYagoub wants to merge 1 commit into
sindresorhus:mainfrom
AmineYagoub:feat/max-length

Conversation

@AmineYagoub

Copy link
Copy Markdown

Adds a maxLength option for callers feeding slugs into URL segments, filenames, and keys with hard length limits — today they truncate the returned slug by hand, which cuts words in half and leaves dangling separators.

Semantics:

  • No-op when the finished slug is already within the limit.
  • Otherwise cuts at the rightmost separator boundary at or before the limit (slugify('Foo Bar Baz Qux', {maxLength: 9}) → 'foo-bar'), hard-cutting a single unbroken run (slugify('Internationalization', {maxLength: 8}) → 'internat') and with separator: ''.
  • A truncated slug never ends with the separator.
  • slugifyWithCounter applies the limit to the base slug before the counter suffix, so deduplication is never silently broken by truncation.
  • Anything but a positive integer throws a TypeError naming the option.

Includes tests for each semantic (22 → 28 passing) and readme + index.d.ts docs in the existing style.

Full disclosure: this change was implemented end-to-end by Detent, an autonomous-engineering tool I'm building — every step was gated by this repo's own xo/ava suite and independently reviewed before commit. I'm sharing it as a hopefully useful contribution; happy to adjust anything, or to close this and open an issue first if that's your preferred flow.

🤖 Generated with Claude Code

Truncates the finished slug to at most `maxLength` characters, preferring
the rightmost separator boundary at or before the limit and hard-cutting
unbroken runs; a truncated slug never ends with the separator. Applies to
the base slug in `slugifyWithCounter` before the counter suffix. Rejects
anything but a positive integer with a TypeError.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant