slugify turns a Persian string into a URL-safe slug. Companion helpers (createSlug, slugifyWithNumbers, slugifySimple) are thin wrappers around the same engine.
Functions
slugify(text, options?)
With options
Convenience exports
What slugify does internally
- Validate input is a non-empty string (otherwise throws).
- Normalize Arabic-script characters → Persian via
toPersianChars. - Apply
SLUG_REPLACEMENTS(e.g.آ → ا,ة → ه, strip Arabic diacritics). - Apply
PUNCTUATION_REPLACEMENTS(strip؟ ، « »etc.; convert Arabic-Indic digits to English). - Apply any
customReplacements. - Collapse whitespace to
separator, optionally dedupe consecutive separators, optionally truncate tomaxLength.
Pitfalls
- Throws on empty string. Pre-check
text.trim().length > 0. lowercase: trueaffects only Latin — Persian has no case.preserveNumbers: truekeeps Persian digits, which browsers handle but some legacy servers may reject. For pure-ASCII URLs, setpreserveNumbers: falseANDdigitsFaToEnupstream.maxLengthtruncates after slug building, possibly leaving a trailing separator — trim it yourself if it matters.- Custom replacements run after defaults and can override punctuation handling.
Source
src/modules/slugify/index.ts · Tests: test/slugify.spec.ts