This commit is contained in:
parent
db4b99aee3
commit
1dee8b528f
30 changed files with 3491 additions and 2444 deletions
38
website/src/features/games/mhwilds/utils.js
Normal file
38
website/src/features/games/mhwilds/utils.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
const MHWILDS_IMG_PATH = "/static/img/mhwilds";
|
||||
|
||||
export function assetPath(name) {
|
||||
return encodeURI(`${MHWILDS_IMG_PATH}/${name}.png`);
|
||||
}
|
||||
|
||||
export function normalizeText(value) {
|
||||
return String(value || "").trim().toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\s+/g, " ");
|
||||
}
|
||||
|
||||
export function getConditionValues(conditions = []) {
|
||||
return [...new Set(conditions.flatMap((condition) => condition.values || []))];
|
||||
}
|
||||
|
||||
export function getUniqueConditionValues(items, property, translate) {
|
||||
return [...new Set(items.flatMap((item) => getConditionValues(item[property])))]
|
||||
.filter((value) => value !== "none")
|
||||
.sort((a, b) => translate(a).localeCompare(translate(b), "fr"));
|
||||
}
|
||||
|
||||
export function getFilteredMhwildsItems({ category, mhwilds, filters, t }) {
|
||||
const items = category === "monsters" ? mhwilds.monsters : mhwilds.endemic;
|
||||
const activeFilters = filters[category];
|
||||
const filterKey = category === "monsters" ? "weaknesses" : "locations";
|
||||
const selected = activeFilters[filterKey];
|
||||
const search = normalizeText(activeFilters.name);
|
||||
|
||||
return items.filter((item) => {
|
||||
const nameMatches = !search || normalizeText(t(item.name)).includes(search) || normalizeText(item.name).includes(search);
|
||||
if (!nameMatches) return false;
|
||||
if (!selected.length) return true;
|
||||
|
||||
const values = getConditionValues(item[filterKey]);
|
||||
return activeFilters.logic === "or" && selected.length >= 2
|
||||
? selected.some((value) => values.includes(value))
|
||||
: selected.every((value) => values.includes(value));
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue