Ajout de Misc.findFirstLike

Méthode de recherche de valeur "proche" dans une liste.

Cherche un élément correspondant strictement, sinon cherche
les éléments contenant la valeur, et retourne le premier.

Notification si plusieurs valeurs peuvent correspondre.
This commit is contained in:
Vincent Vandemeulebrouck
2021-10-08 23:25:50 +02:00
parent a8707370ea
commit 8c3e7e2445
3 changed files with 22 additions and 25 deletions

View File

@ -209,20 +209,7 @@ export class RdDItemCompetence extends Item {
/* -------------------------------------------- */
static findCompetence(list, name) {
name = Grammar.toLowerCaseNoAccent(name);
const competences = list.filter(it => Grammar.toLowerCaseNoAccent(it.name).includes(name) && (it.type == "competence" || it.type == "competencecreature"));
if (competences.length == 0) {
return undefined;
}
let competence = competences.find(it => Grammar.toLowerCaseNoAccent(it.name) == name);
if (!competence) {
competence = competences[0];
if (competences.length > 1) {
const names = competences.map(it => it.name).reduce((a, b) => `${a}<br>${b}`);
ui.notifications.info(`Plusieurs compétences possibles:<br>${names}<br>La première sera choisie: ${competence.name}`);
}
}
return competence;
return Misc.findFirstLike(name, list, it => it.name, 'compétences');
}
/* -------------------------------------------- */