removeDiacritics function
Removes the accents of a text (Été → Ete), to search or sort without them. ø, ß or æ stay.
ts
import { removeDiacritics } from 'utils';Signature
ts
export function removeDiacritics(input: string): stringParameters
| Name | Description |
|---|---|
input | The text. |
Returns
The text without combining marks.
Example
ts
removeDiacritics('Crème brûlée à Évian'); // 'Creme brulee a Evian'Tests
Line coverage: 100 %.
- ✅ removes the diacritics of "Crème brûlée à Évian"
- ✅ removes the diacritics of "Ñandú"
- ✅ removes the diacritics of "søren straße"
- ✅ removes the diacritics of ""
Source
src/string/remove-diacritics.ts (on GitHub)
Used by
slugify: Turns a text into a URL- and id-friendly slug: accents removed, lower-case words joined with-.
Source code
ts
const DIACRITICS_PATTERN = /\p{M}/gv;
/**
* Removes the accents of a text (`Été` → `Ete`), to search or sort without them. `ø`, `ß` or `æ` stay.
*
* @param input - The text.
* @returns The text without combining marks.
* @example
* removeDiacritics('Crème brûlée à Évian'); // 'Creme brulee a Evian'
*/
export function removeDiacritics(input: string): string {
return input.normalize('NFD').replaceAll(DIACRITICS_PATTERN, '');
}Test file
ts
import { removeDiacritics } from './remove-diacritics';
describe(removeDiacritics, () => {
it.for([
['Crème brûlée à Évian', 'Creme brulee a Evian'],
['Ñandú', 'Nandu'],
['søren straße', 'søren straße'],
['', ''],
] as const)('removes the diacritics of %j', ([input, expected]) => {
expect(removeDiacritics(input)).toBe(expected);
});
});Keywords: remove, diacritics, strings, function