From eea5e49a96125079ea0b21988bfe00609d29be34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Tue, 3 Dec 2019 23:35:27 -0500 Subject: [PATCH] app: Recherche ignore les accents --- app/src/components/TermInput.js | 18 +++++++++++++++--- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/components/TermInput.js b/app/src/components/TermInput.js index 60346f3..08889e4 100644 --- a/app/src/components/TermInput.js +++ b/app/src/components/TermInput.js @@ -1,4 +1,5 @@ import React, {useState, useRef} from 'react'; +import * as diacritics from 'diacritics'; /** * Codes des touches du clavier par nom. @@ -12,6 +13,19 @@ const keys = Object.assign(Object.create(null), { 'down': 40, }); +/** + * Vérifie si un terme est similaire à un préfixe saisi. + * + * @param prefix Préfixe saisi. + * @param term Terme à vérifier. + * @return Vrai si le terme est considéré comme similaire au préfixe. + */ +const termMatchesPrefix = (prefix, {name}) => +{ + return diacritics.remove(name.toLowerCase()) + .startsWith(diacritics.remove(prefix.toLowerCase())); +}; + /** * Zone de saisie des termes de recherche. * @@ -141,9 +155,7 @@ const TermInput = ({terms, availableTerms, setTerms}) => ) ) // Filtre ceux dont le nom correspond à la saisie - .filter(({name}) => - name.toLowerCase().startsWith(nextValue.toLowerCase()) - ) + .filter(termMatchesPrefix.bind(null, nextValue)) ); } }; diff --git a/package-lock.json b/package-lock.json index 8715ca6..dd7a433 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2508,6 +2508,11 @@ "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=", "dev": true }, + "diacritics": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/diacritics/-/diacritics-1.3.0.tgz", + "integrity": "sha1-PvqHMj67hj5mls67AILUj/PW96E=" + }, "diffie-hellman": { "version": "5.0.3", "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", diff --git a/package.json b/package.json index 8638469..0d29be6 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@babel/runtime": "^7.7.4", + "diacritics": "^1.3.0", "eslint": "^6.7.2", "eslint-plugin-react": "^7.17.0", "react": "^16.12.0",