Skip to content

slugify ​

Converts a string into a clean, URL-friendly slug. Removes diacritics/accents, replaces non-alphanumeric characters with separators, and normalizes casing.

Signature ​

typescript
function slugify(value: string | null | undefined, options?: SlugifyOptions): string;

Parameters ​

ParameterTypeDescription
valuestring | null | undefinedThe string to transform into a slug.
optionsSlugifyOptionsOptional configuration settings for slug.

Options ​

OptionTypeDefaultDescription
separatorstring"-"Character(s) used to separate words.
lowerbooleantrueWhether to convert the output to lowercase.

Returns ​

string — The slugified URL-safe string, or an empty string "" when value is empty, null, or undefined.

Examples ​

typescript
import { slugify } from "@rcpthongta/string-utils";

slugify("Hello World!");
// => "hello-world"

// Accents and diacritics normalization
slugify("Déjà vu & Crème brûlée");
// => "deja-vu-and-creme-brulee"

// Custom separator
slugify("Hello World", { separator: "_" });
// => "hello_world"

// Preserving uppercase
slugify("Hello World", { lower: false });
// => "Hello-World"

slugify("");
// => ""

slugify(null);
// => ""

slugify(undefined);
// => ""

Released under the MIT License.