Useful snippets for text in JS
Remove accents and diacritics in a string
Section titled “Remove accents and diacritics in a string”str.normalize("NFD").replace(/\p{Diacritic}/gu, "");Related
Section titled “Related”Tokenize natural languages
Section titled “Tokenize natural languages”const segmenterFr = new Intl.Segmenter("fr", { granularity: "word" });const string1 = "Que ma joie demeure";const iterator1 = segmenterFr.segment(string1)[Symbol.iterator]();console.log(iterator1.next().value.segment);see mdn ↗
Related
Section titled “Related”- https://github.com/meilisearch/charabia ↗
- https://github.com/Brooooooklyn/pinyin ↗
- https://github.com/Anush008/tokenizers ↗
Country code to flag emoji
Section titled “Country code to flag emoji”function getFlagEmoji(countryCode) { const codePoints = countryCode .toUpperCase() .split("") .map((char) => 127397 + char.charCodeAt()); return String.fromCodePoint(...codePoints);}