# wrSlug

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

Source: https://ngwr.dev/reference/utils/wr-slug  
Kind: Util  
Added in v16.9  
Optional

## Installation

Import the class. This entry point has no stylesheet.

```typescript
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.

```typescript
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.

```typescript
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

```typescript
wrSlug('What it costs to adopt', { separator: '-', maxLength: 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.

```typescript
wrSlug('Привет, мир!');            // 'привет-мир'
wrSlug('Қазақстан Республикасы');  // 'қазақстан-республикасы'
wrSlug('Café au lait');            // 'café-au-lait'
wrSlug('नमस्ते दुनिया');              // 'नमस्ते-दुनिया'
wrSlug('ＡＢＣ １２３');            // '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.

```typescript
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: `ＡＢＣ` 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` |

## See also

- [wrRandomId](https://ngwr.dev/reference/utils/wr-random-id) — Short prefixed random id, for \<label for> and aria-describedby pairs.
- [wrInitials](https://ngwr.dev/reference/utils/wr-initials) — Up to two initials of a name, one grapheme each, or the family name with initials.
- [wrTruncate](https://ngwr.dev/reference/pipes/wr-truncate) — Shorten a string and end it with an ellipsis.
