StringAPI ReferenceSlugification API

Slugification API

Complete API reference for slugify and transliterate functions in @visulima/string.

Slugification API

Detailed API reference for the slugify and transliterate functions.

slugify

Converts a string into a URL-friendly slug. It transliterates non-ASCII characters, optionally converts case, removes disallowed characters (replacing with separator), and collapses separators.

function slugify(input: string, options?: SlugifyOptions): string;

Import:

import slugify from "@visulima/string/slugify";
// or from the barrel export:
// import { slugify } from "@visulima/string";

Parameters:

ParameterTypeDescription
inputstringThe string to convert into a slug.
optionsSlugifyOptionsOptional configuration options.

Returns: string -- The generated URL-friendly slug.

Examples:

slugify("Hello World!");          // 'hello-world'
slugify("Creme Brulee");          // 'creme-brulee'
slugify("foo & bar * baz");       // 'foo-bar-baz'

// Chinese support
slugify("你好 World!");            // 'ni-hao-world'

// Custom separator
slugify("foo bar baz", { separator: "_" });  // 'foo_bar_baz'

// Uppercase
slugify("foo bar", { lowercase: false, uppercase: true });  // 'FOO-BAR'

// Preserve case
slugify("Foo Bar", { lowercase: false });  // 'Foo-Bar'

Edge cases:

  • If both lowercase and uppercase are true, a console warning is emitted and the function defaults to lowercase.
  • Leading and trailing separator characters are always trimmed from the result.
  • Consecutive separators are collapsed into a single separator.

SlugifyOptions

interface SlugifyOptions extends OptionsTransliterate {
    /**
     * Allowed characters. Other characters will be converted to `separator`.
     * Uses regex character class syntax (e.g. "a-zA-Z0-9").
     * @default "a-zA-Z0-9-_.~"
     */
    allowedChars?: string;

    /**
     * Fix Chinese spacing passed to transliterate.
     * If you don't need to transliterate Chinese characters, set to false to improve performance.
     * @default true
     */
    fixChineseSpacing?: boolean;

    /**
     * Characters/strings to ignore during transliteration.
     * @default []
     */
    ignore?: string[];

    /**
     * Whether the result should be converted into lowercase.
     * Cannot be true if `uppercase` is true.
     * @default true
     */
    lowercase?: boolean;

    /**
     * Search/replace pairs applied after transliteration.
     * Can be an object `{ search: replacement }` or an array of `[search, replacement]` tuples.
     * Searches can be strings or RegExp.
     * @default []
     */
    replaceAfter?: OptionReplaceCombined;

    /**
     * Search/replace pairs applied before transliteration.
     * @default []
     */
    replaceBefore?: OptionReplaceCombined;

    /**
     * Custom separator string used to join words in the slug.
     * @default "-"
     */
    separator?: string;

    /**
     * Whether to transliterate the input string.
     * @default true
     */
    transliterate?: boolean;

    /**
     * Trim leading/trailing whitespace before processing.
     * @default false
     */
    trim?: boolean;

    /**
     * Character to use for unknown characters.
     * @default ""
     */
    unknown?: string;

    /**
     * Whether the result should be converted into uppercase.
     * Cannot be true if `lowercase` is true.
     * @default false
     */
    uppercase?: boolean;
}

transliterate

Transliterates Unicode characters to ASCII equivalents using a comprehensive character map. Supports Chinese Pinyin, Thai romanization, Cyrillic, Greek, Arabic, Hebrew, Japanese, Korean, and many other scripts.

function transliterate(source: string, options?: OptionsTransliterate): string;

Import:

import { transliterate } from "@visulima/string";

Parameters:

ParameterTypeDescription
sourcestringThe string to transliterate.
optionsOptionsTransliterateOptional configuration.

Returns: string -- The transliterated ASCII string.

Examples:

// European
transliterate("Creme brulee");    // 'Creme brulee'
transliterate("Dusseldorf");      // 'Dusseldorf'

// Cyrillic
transliterate("Москва");          // 'Moskva'

// Chinese (with spacing)
transliterate("你好世界");         // 'Ni Hao Shi Jie'

// Chinese (without spacing)
transliterate("你好世界", { fixChineseSpacing: false });
// 'NiHaoShiJie'

// Custom replacements
transliterate("Replace C++ before map", {
    replaceBefore: { "C++": "cpp" },
}); // 'Replace cpp before map'

// Ignore segments
transliterate("Don't change THIS", {
    ignore: ["THIS"],
}); // "Dont change THIS"

// Unknown characters
transliterate("a🚀b", { unknown: "[?]" });  // 'a[?]b'

OptionsTransliterate

interface OptionsTransliterate {
    /**
     * Add space between Chinese Pinyin syllables.
     * @default true
     */
    fixChineseSpacing?: boolean;

    /**
     * Characters/strings to ignore during transliteration.
     * These are preserved as-is in the output.
     * @default []
     */
    ignore?: string[];

    /**
     * Search/replace pairs applied after the character map.
     * Can be an object or array of `[RegExp | string, string]` tuples.
     * @default []
     */
    replaceAfter?: OptionReplaceCombined;

    /**
     * Search/replace pairs applied before the character map.
     * @default []
     */
    replaceBefore?: OptionReplaceCombined;

    /**
     * Trim leading/trailing whitespace from the result.
     * @default false
     */
    trim?: boolean;

    /**
     * Character to use for unknown (untransliterable) characters.
     * @default ""
     */
    unknown?: string;
}

OptionReplaceCombined

The replacement configuration can be either an object or an array:

/** Object form: keys are search strings, values are replacements */
type OptionReplaceObject = Record<string, string>;

/** Array form: tuples of [search, replacement] where search can be a RegExp */
type OptionReplaceArray = [RegExp | string, string | undefined][];

/** Combined type */
type OptionReplaceCombined = OptionReplaceArray | OptionReplaceObject;

Object form:

slugify("C# and C++", {
    replaceBefore: {
        "C#": "csharp",
        "C++": "cpp",
    },
}); // 'csharp-and-cpp'

Array form (with RegExp):

transliterate("test123", {
    replaceAfter: [[/\d+/g, "NUM"]],
}); // 'testNUM'

Notes

  • The slugify function internally calls transliterate when the transliterate option is true (the default).
  • The character map covers thousands of Unicode code points across many writing systems.
  • When transliterate is set to false, the input is normalized with NFC but non-ASCII characters that are not in allowedChars will be removed.
  • The fixChineseSpacing option inserts spaces between Chinese Pinyin syllables for more readable output. Set to false for compact slugs.
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues