Skip to content

slugify function ​

Turns a text into a URL- and id-friendly slug: accents removed, lower-case words joined with -.

ts
import { slugify } from 'utils';

Signature ​

ts
export function slugify(input: string): string

Parameters ​

NameDescription
inputThe text, such as a title or a label.

Returns ​

The slug; '' when the text has no letter or digit.

Example ​

ts
slugify('Salle des machines — Été 2026'); // 'salle-des-machines-ete-2026'
slugify('  Hello, World!  '); // 'hello-world'

Tests ​

Line coverage: 100 %.

  • ✅ slugifies "Salle des machines — Été 2026" as "salle-des-machines-ete-2026"
  • ✅ slugifies " Hello, World! " as "hello-world"
  • ✅ slugifies "Crème brûlée" as "creme-brulee"
  • ✅ slugifies "engineRoom" as "engine-room"
  • ✅ slugifies "!!!" as ""

Source ​

src/string/slugify.ts (on GitHub)

Uses ​

  • kebabCase: Converts a string to kebab-case: lowercase words joined with -, as in file names, CSS classes and URLs.
  • removeDiacritics: Removes the accents of a text (Été → Ete), to search or sort without them.

Files it needs ​

src/string/slugify.ts imports, directly or not:

Source code
ts
import { kebabCase } from './kebab-case';
import { removeDiacritics } from './remove-diacritics';

/**
 * Turns a text into a URL- and id-friendly slug: accents removed, lower-case words joined with `-`.
 *
 * @param input - The text, such as a title or a label.
 * @returns The slug; `''` when the text has no letter or digit.
 * @example
 * slugify('Salle des machines — Été 2026'); // 'salle-des-machines-ete-2026'
 * slugify('  Hello, World!  '); // 'hello-world'
 */
export function slugify(input: string): string {
  return kebabCase(removeDiacritics(input));
}
Test file
ts
import { slugify } from './slugify';

describe(slugify, () => {
  it.for([
    ['Salle des machines — Été 2026', 'salle-des-machines-ete-2026'],
    ['  Hello, World!  ', 'hello-world'],
    ['Crème brûlée', 'creme-brulee'],
    ['engineRoom', 'engine-room'],
    ['!!!', ''],
  ] as const)('slugifies %j as %j', ([input, expected]) => {
    expect(slugify(input)).toBe(expected);
  });
});

Keywords: slugify, strings, function

Generated from the JSDoc of the sources.