From a7014baab2413740a5ffe69ada77eaa37a014445 Mon Sep 17 00:00:00 2001 From: danielesreis Date: Sat, 3 Oct 2020 20:23:40 -0300 Subject: [PATCH 1/3] Add removeAccents snippet --- snippets/removeAccents.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/removeAccents.md diff --git a/snippets/removeAccents.md b/snippets/removeAccents.md new file mode 100644 index 000000000..3766fe329 --- /dev/null +++ b/snippets/removeAccents.md @@ -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' +``` From 2e61a40d1549ebc65f23cee9903bd0475fda5588 Mon Sep 17 00:00:00 2001 From: danielesreis Date: Sun, 4 Oct 2020 10:40:01 -0300 Subject: [PATCH 2/3] Improves the removeAccents snippet --- snippets/removeAccents.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/snippets/removeAccents.md b/snippets/removeAccents.md index 3766fe329..6b49ba389 100644 --- a/snippets/removeAccents.md +++ b/snippets/removeAccents.md @@ -5,25 +5,11 @@ 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. +- Converts the string to a normalized Unicode format. +- The diacritical marks are represented by an Unicode range and are replaced by empty strings. ```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; -} +const removeAccents = string => string.normalize("NFD").replace(/[\u0300-\u036f]/g, "") ``` ```js From ec93bb3c565ef1935673ace344aa7016c3f24852 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 4 Oct 2020 16:42:30 +0300 Subject: [PATCH 3/3] Update removeAccents.md --- snippets/removeAccents.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/removeAccents.md b/snippets/removeAccents.md index 6b49ba389..ea5d0ec1d 100644 --- a/snippets/removeAccents.md +++ b/snippets/removeAccents.md @@ -5,11 +5,11 @@ tags: string,beginner Removes accents from strings. -- Converts the string to a normalized Unicode format. -- The diacritical marks are represented by an Unicode range and are replaced by empty strings. +- Use `String.prototype.normalize()` to convert the string to a normalized Unicode format. +- Use `String.prototype.replace()` to replace diacritical marks in the given Unicode range by empty strings. ```js -const removeAccents = string => string.normalize("NFD").replace(/[\u0300-\u036f]/g, "") +const removeAccents = str => str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); ``` ```js