Skip to content

getSlug

Converts a string into a URL-friendly slug. The input is trimmed, split into words on whitespace and any existing -/_ characters, and joined back together with the chosen separator. Letters are kept, while non-Latin letters, digits and special characters are gated by the options below.

The behavior is intentionally simple and predictable:

  • The result is lowercased by default (or uppercased when uppercase is set).
  • Only whitespace and -/_ act as word boundaries. Other dropped characters (such as @ or .) simply disappear, so the characters around them merge into a single word.
  • Non-Latin letters (for example Korean) are kept as-is by default because modern browsers can display them in a URL. Set includeNonLatin to false to keep only ASCII letters.
  • Digits are included by default; special characters are excluded by default. When includeSpecial is enabled, each special character is percent-encoded (for example & becomes %26).
  • When baseUrl is provided and the slug is not empty, the slug is appended to it to form a full URL. A trailing slash on the base URL is normalized away.
  • An empty input (or one that produces no slug) returns an empty string, even when a baseUrl is set.

Parameters

NameTypeRequiredDefault
textstring
optionsnamedobject

named parameters are passed as named parameters in Dart and keyword arguments in Python.

options
NameTypeRequiredDefault
separatorstring'-'
The word separator.
includeNumbersbooleantrue
Keep digits.
includeSpecialbooleanfalse
Percent-encode and keep special characters.
uppercasebooleanfalse
Output in uppercase instead of lowercase.
includeNonLatinbooleantrue
Keep non-Latin letters such as Korean.
baseUrlstring''
When set, prepend this base URL to build a full URL.

Returns

string

Examples

javascript
getSlug('Hello World'); // Returns 'hello-world'
getSlug('안녕 하세요 반갑습니다'); // Returns '안녕-하세요-반갑습니다'
getSlug('Product #123 (2024)'); // Returns 'product-123-2024'
getSlug('Hello World', { separator: '_', uppercase: true }); // Returns 'HELLO_WORLD'
getSlug('Café & Restaurant', { includeSpecial: true }); // Returns 'café-%26-restaurant'
getSlug('Hello 안녕', { includeNonLatin: false }); // Returns 'hello'
getSlug('Hello World', { baseUrl: 'https://example.com/blog' }); // Returns 'https://example.com/blog/hello-world'
dart
getSlug('Hello World'); // Returns 'hello-world'
getSlug('안녕 하세요 반갑습니다'); // Returns '안녕-하세요-반갑습니다'
getSlug('Product #123 (2024)'); // Returns 'product-123-2024'
getSlug('Hello World', separator: '_', uppercase: true); // Returns 'HELLO_WORLD'
getSlug('Café & Restaurant', includeSpecial: true); // Returns 'café-%26-restaurant'
getSlug('Hello 안녕', includeNonLatin: false); // Returns 'hello'
getSlug('Hello World', baseUrl: 'https://example.com/blog'); // Returns 'https://example.com/blog/hello-world'
python
getSlug('Hello World')  # Returns 'hello-world'
getSlug('안녕 하세요 반갑습니다')  # Returns '안녕-하세요-반갑습니다'
getSlug('Product #123 (2024)')  # Returns 'product-123-2024'
getSlug('Hello World', separator='_', uppercase=True)  # Returns 'HELLO_WORLD'
getSlug('Café & Restaurant', includeSpecial=True)  # Returns 'café-%26-restaurant'
getSlug('Hello 안녕', includeNonLatin=False)  # Returns 'hello'
getSlug('Hello World', baseUrl='https://example.com/blog')  # Returns 'https://example.com/blog/hello-world'

Released under the MIT License