UtilAdded in v16.9Optional

wrSlug

A text as a slug for an anchor, an id, a URL segment or a file name. Letters of every script are kept.

Installation

Import the class. This entry point has no stylesheet.

import { wrSlug } from 'ngwr/utils';

Usage

Words are lower-cased and joined by one -. Punctuation and whitespace at either end are dropped. null and undefined never throw.

import { wrSlug } from 'ngwr/utils';

wrSlug('What it costs to adopt');  // 'what-it-costs-to-adopt'
wrSlug('  Data & charts!  ');      // 'data-charts'
wrSlug('v16.9.0');                 // 'v16-9-0'
wrSlug('...');                     // ''
wrSlug(null);                      // ''

Options

separator joins the words with another string, such as _ for a file name. maxLength caps the slug at a word boundary, for a URL segment or a column with a limit.

wrSlug('Data & charts', { separator: '_' });          // 'data_charts'
wrSlug('Signal Forms', { separator: '' });            // 'signalforms'
wrSlug('What it costs to adopt', { maxLength: 14 });  // 'what-it-costs'
wrSlug('Internationalization', { maxLength: 5 });     // 'inter', a first word too long is cut

Playground

what-it-costs-to-adopt

wrSlug('What it costs to adopt', { separator: '-', maxLength: 40 });

Customize

Text
Separator
Max length
40

Scripts

Nothing is transliterated and no diacritic is stripped: stripping marks would turn Russian й into и and break Devanagari. A URL percent-encodes such a slug, and every browser shows it decoded.

wrSlug('Привет, мир!');            // 'привет-мир'
wrSlug('Қазақстан Республикасы');  // 'қазақстан-республикасы'
wrSlug('Café au lait');            // 'café-au-lait'
wrSlug('नमस्ते दुनिया');              // 'नमस्ते-दुनिया'
wrSlug('ABC 123');            // 'abc-123', full-width forms fold into plain ones

Ties

Two texts can give one slug. Where slugs must differ, such as ids on one page, the caller breaks the tie.

wrSlug('Data & charts');  // 'data-charts'
wrSlug('Data / charts');  // 'data-charts'

const taken = new Set<string>();

function uniqueId(title: string): string {
  const slug = wrSlug(title) || 'section';
  let id = slug;
  for (let n = 2; taken.has(id); n++) id = `${slug}-${n}`;
  taken.add(id);
  return id;
}

Rules

InputResult
Letters Lower-cased with the default mapping, the same on the server and the client: Signal Forms gives signal-forms.
Punctuation, whitespace, symbolsA run of any length becomes one -, and none stays at either end.
DigitsKept: v16.9.0 gives v16-9-0.
Diacritics and combining marksKept: Café gives café.
Full-width forms and ligaturesFolded by NFKC: ABC gives abc.
No letter or digit, empty, null''

API

NameDescriptionTypeDefault
wrSlug(text, options?)The text NFKC-normalised and lower-cased, its words of letters, combining marks and digits joined by the separator. No transliteration. Not injective.(text: string | null | undefined, options?: WrSlugOptions) => string-
options.separatorWhat joins the words. '' runs them together.string'-'
options.maxLengthThe longest slug, in UTF-16 code units. Words that do not fit are dropped; a first word longer than the limit is cut between two graphemes.numberno limit

See also