MNEMO / client /src /lib /tree.js
Akeno
Deploy to Hugging Face Spaces
d63472e
Raw
History Blame Contribute Delete
783 Bytes
export function buildCardTree(cards) {
const map = new Map();
const roots = [];
cards
.slice()
.sort((a, b) => a.order - b.order || a.title.localeCompare(b.title))
.forEach((card) => {
map.set(card._id, { ...card, children: [] });
});
map.forEach((card) => {
if (card.parentId && map.has(card.parentId)) {
map.get(card.parentId).children.push(card);
} else {
roots.push(card);
}
});
return { roots, map };
}
export function getAncestorChain(cardId, cardMap) {
if (!cardId || !cardMap.has(cardId)) {
return [];
}
const chain = [];
let current = cardMap.get(cardId);
while (current) {
chain.unshift(current);
current = current.parentId ? cardMap.get(current.parentId) : null;
}
return chain;
}