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

@ -3020,17 +3020,8 @@ export class RdDActor extends Actor {
case 'chance-actuelle': case 'chance actuelle':
return carac.chance;
}
const keys = Object.entries(carac)
.filter(it => it[0].includes(name) || Grammar.toLowerCaseNoAccent(it[1].label).includes(name))
.map(it => it[0]);
if (keys.length > 1) {
const names = keys.reduce((a, b) => `${a}<br>${b}`);
ui.notifications.info(`Plusieurs caractéristiques possibles:<br>${names}<br>La première sera choisie.`);
}
if (keys.length > 0) {
return carac[keys[0]];
}
return undefined; // Per default
let entry = Misc.findFirstLike(name, Object.entries(carac), it => it[1].label, 'caractéristiques');
return entry.length>0 ? carac[entry[0]] : undefined;
}
/* -------------------------------------------- */

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');
}
/* -------------------------------------------- */

View File

@ -1,3 +1,4 @@
import { Grammar } from "./grammar.js";
import { RdDDice } from "./rdd-dice.js";
/**
@ -131,4 +132,22 @@ export class Misc {
static isElectedUser() {
return game.user.id == Misc.connectedGMOrUser();
}
/* -------------------------------------------- */
static findFirstLike(value, elements, mapper = it=>it.name, description = 'valeurs') {
value = Grammar.toLowerCaseNoAccent(value);
const subset = elements.filter(it => Grammar.toLowerCaseNoAccent(mapper(it)).includes(value));
if (subset.length == 0) {
return undefined;
}
let single = subset.find(it => Grammar.toLowerCaseNoAccent(mapper(it)) == value);
if (!single) {
single = subset[0];
if (subset.length > 1) {
const choices = subset.map(it => mapper(it)).reduce((a, b) => `${a}<br>${b}`);
ui.notifications.info(`Plusieurs choix de ${description} possibles:<br>${choices}<br>Le premier sera choisi: ${mapper(single)}`);
}
}
return single;
}
}