A case-insensitive accent folding functions to replace accented characters with their unaccented equivalents or hightlight matched terms in a string, ignoring accents.
Install with npm:
npm install accent-folding
or with pnpm:
pnpm install accent-folding
or even with yarn:
yarn add accent-folding
Replaces accented characters in a string with their unaccented equivalents.
- Handles various Unicode characters, including fullwidth ASCII
- Preserves original string formatting in the output
import AccentFolding from 'accent-folding';
const af = new AccentFolding();
af.replace('Fulanilo López'); // --> "Fulanilo Lopez"
Highlights matched terms in a string, ignoring accents.
- Accent-insensitive matching
- Customizable highlight wrapping (can use any HTML tag)
- Preserves original string formatting in the output
- Handles various Unicode characters, including fullwidth ASCII
- wraps string fragment in
<b>
html tag by default.
- Search functionality in applications where accents should be ignored
- Highlighting matched terms in search results
import AccentFolding from 'accent-folding';
const af = new AccentFolding();
af.highlightMatch('Fulanilo López', 'lo'); // --> "Fulani<b>lo</b> <b>Ló</b>pez"
Use the 3d argument to specify the wrapping html tag (strong, em, span etc.):
af.highlightMatch('Fulanilo López', 'lo', 'strong'); // --> "Fulani<strong>lo</strong> <strong>Ló</strong>pez"
The AccentFolding
class allows you to extend or override the default accent map by providing a custom map to the constructor.
To extend the accent map with new mappings, pass an object with the new mappings to the constructor. For example:
import AccentFolding from 'accent-folding';
const customAccentMap = { 'ö': 'oe', '✝': 't' };
const accentFolder = new AccentFolding(customAccentMap);
console.log(accentFolder.replace('Föhn')); // Outputs: Foehn
console.log(accentFolder.replace('✝illa')); // Outputs: tilla
Node.js version 14.7 or higher
Install with npm:
npm install accent-folding@1
Example:
const accentFoldedHighlight = require('accent-folding');
accentFoldedHighlight('Fulanilo López', 'lo'); // --> "Fulani<b>lo</b> <b>Ló</b>pez"
accentFoldedHighlight('Fulanilo López', 'lo', 'strong'); // --> "Fulani<strong>lo</strong> <strong>Ló</strong>pez"
See the Roadmap for planned features and future improvements.
Contributions are welcome! Please feel free to submit a Pull Request.