Add removeAccents snippet

This commit is contained in:
danielesreis
2020-10-03 20:23:40 -03:00
parent 7081232e8e
commit a7014baab2

31
snippets/removeAccents.md Normal file
View File

@ -0,0 +1,31 @@
---
title: removeAccents
tags: string,beginner
---
Removes accents from strings.
- Uses a set of known accents for all vowels.
- Iterates through the characters, replacing the accentued ones for its unaccentuated form.
```js
const removeAccents = (string) => {
const accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖòóôõöÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü';
const noAccents = 'AAAAAAaaaaaaOOOOOOoooooEEEEeeeeIIIIiiiiUUUUuuuu';
let splitted = string.split('');
splitted = splitted.map((char) => {
const pos = accents.indexOf(char);
return (pos >= 0 ? noAccents[pos] : char);
});
const newString = splitted.join('');
return newString;
}
```
```js
removeAccents('Antoine de Saint-Exupéry'); // 'Antoine de Saint-Exupery'
```