feat: add multi-source breed encyclopedia and two-hour refresh sessions
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
const { secure } = require("./api");
|
||||
let cached;
|
||||
let pending;
|
||||
function loadBreeds(force = false) {
|
||||
if (pending) return pending;
|
||||
if (cached && !force) return Promise.resolve(cached);
|
||||
pending = secure("/breeds")
|
||||
.then((rows) => {
|
||||
cached = rows;
|
||||
return rows;
|
||||
})
|
||||
.finally(() => {
|
||||
pending = null;
|
||||
});
|
||||
return pending;
|
||||
}
|
||||
function groupBreeds(rows, query = "") {
|
||||
const q = query.trim().toLowerCase().replace(/\s/g, "");
|
||||
const groups = new Map();
|
||||
for (const row of rows) {
|
||||
if (
|
||||
q &&
|
||||
![
|
||||
row.name,
|
||||
row.aliases,
|
||||
row.english_name,
|
||||
row.pinyin,
|
||||
row.pinyin_initials,
|
||||
].some((value) =>
|
||||
(value || "").toLowerCase().replace(/\s/g, "").includes(q),
|
||||
)
|
||||
)
|
||||
continue;
|
||||
const letter = row.initial || "#";
|
||||
if (!groups.has(letter)) groups.set(letter, []);
|
||||
groups.get(letter).push(row);
|
||||
}
|
||||
return [...groups]
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([letter, items]) => ({
|
||||
letter,
|
||||
anchor: letter === "#" ? "letter-other" : "letter-" + letter,
|
||||
items,
|
||||
}));
|
||||
}
|
||||
module.exports = { loadBreeds, groupBreeds };
|
||||
Reference in New Issue
Block a user