@padmaj/slugify
v1.0.2
Published
Convert strings to URL-friendly slugs. Handles unicode, configurable separator. Zero dependencies.
Maintainers
Readme
@padmaj/slugify
Convert strings to URL-friendly slugs. Handles unicode, configurable separator, optional max length. Zero dependencies. Works in Node and browser.
Install
npm install @padmaj/slugifyUsage
import { slugify } from '@padmaj/slugify'
slugify('Hello World!') // → 'hello-world'
slugify('Héllo Wörld') // → 'hello-world'
slugify('café au lait') // → 'cafe-au-lait'
slugify('Version 2.0 Release') // → 'version-2-0-release'
slugify(' multiple spaces ') // → 'multiple-spaces'Custom separator
slugify('Hello World', '_') // → 'hello_world'
slugify('Hello World', { separator: '.' }) // → 'hello.world'Preserve case
slugify('Hello World', { lowercase: false }) // → 'Hello-World'Limit length
slugify('hello world foo bar', { maxLength: 11 }) // → 'hello-world'Truncation never leaves a trailing separator.
API
slugify(input, separatorOrOptions?)
| Option | Type | Default | Description |
|---|---|---|---|
| separator | string | '-' | Character(s) used between words |
| lowercase | boolean | true | Convert to lowercase |
| maxLength | number | — | Truncate slug to this length |
Returns a string. Returns '' for empty or all-special-character input.
Notes
- Unicode accents are stripped via
NFDnormalization (é → e,ö → o). - Consecutive non-alphanumeric characters are collapsed into a single separator.
- Leading and trailing separators are removed.
- The
separatoris safely escaped before use in regex — passing+,.*, or other regex chars is safe. - For untrusted input, always set
maxLengthto avoid generating unexpectedly long slugs.
License
MIT
