Modern Diacritics
A modern way to latinize/ascii-fold strings and normalize symbols. Particularly useful for writings search filters.
- Modern fork of @andrewrk's node-diacritics with many new features
- Dual-published as ESM and CJS modules
- Normalizes similar symbols like quotation marks
- Diacritic Removal and Symbol Normalization also available as separate functions
- Provides slugify function with built-in latinization!
latinize("Iлtèrnåtïonɑlíƶatï߀ԉ");
Installation
npm install modern-diacritics
# or
yarn add modern-diacritics
Usage
latinize
The supplied is latinized with normalized symbols.
import { latinize } from "modern-diacritics";
latinize("Hêƚƚó, ’worƚd‘!");
latinize
uses removeDiacritics
and normalizeSymbols
internally. They are available separatly for applications where you may not wants to fully latinize strings. Options are passed along internally where applicable.
Options:
latinize("Hêƚƚó, ’worƚd‘!", { symbols: false });
latinize("Hêƚƚó, ’worƚd‘!", { lowerCase: true });
latinize(" Hêƚƚó, ’worƚd‘! ", { trim: true });
normalizeSymbols
Normalizes symbols in the supplied string and trims whitespace at the start and end of the string (can be disabled, see Options).
import { normalizeSymbols } from "modern-diacritics";
normalizeSymbols(" “Hauptstraße” ");
Options:
normalizeSymbols(" “Hauptstraße” ", { trim: false });
normalizeSymbols(" “Hauptstraße 42” ", { forceSingleSpace: true });
normalizeSymbols(" “Hauptstraße 42” ", { replaceWhiteSpace: "_" });
normalizeSymbols(" “Hauptstraße 42” ", {
replaceWhiteSpace: "_",
trim: false
});
removeDiacritics
Provies simplified diacritic removal, which does not further latinize strings or normalize symbols.
import { removeDiacritics } from "modern-diacritics";
removeDiacritics("Crêpes");
Options:
removeDiacritics("Crêpes", { lowerCase: true });
slugify
The supplied string is latinized and then turned into a slug:
import { slugify } from "modern-diacritics";
slugify("HêLLó, worLd!");
Whitespace as well as underscores and parenthesis are replaced with dashes. All other symbols will be removed! slugify
uses the lowerCase
and replaceWhiteSpace
options of normalizeSymbols
. trim
is not used and spaces will be transformed to dashes.
Options:
slugify
supports normalizeSymbols
's trim
and forceSingleSpace
options. For backwards compatibility these two options use false
as their default value.
Special Thanks
Contributors
Changelog
See CHANGELOG.md
Planned features
- Custom replacer lists/maps
- Adding more symbols to normalize (feel free to submit suggestions)