import { wrSlug } from 'ngwr/utils';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.
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 cutPlayground
what-it-costs-to-adopt
wrSlug('What it costs to adopt', { separator: '-', maxLength: 40 });Customize
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 onesTies
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
| Input | Result |
|---|---|
| Letters | Lower-cased with the default mapping, the same on the server and the client: Signal Forms gives signal-forms. |
| Punctuation, whitespace, symbols | A run of any length becomes one -, and none stays at either end. |
| Digits | Kept: v16.9.0 gives v16-9-0. |
| Diacritics and combining marks | Kept: Café gives café. |
| Full-width forms and ligatures | Folded by NFKC: ABC gives abc. |
No letter or digit, empty, null | '' |
API
| Name | Description | Type | Default |
|---|---|---|---|
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.separator | What joins the words. '' runs them together. | string | '-' |
options.maxLength | The 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. | number | no limit |