mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
22 lines
701 B
TypeScript
22 lines
701 B
TypeScript
|
|
import { Category } from "./types"
|
||
|
|
|
||
|
|
export function visitCategoryTree(category: Category, visitor: (category: Category) => void): void {
|
||
|
|
visitor(category)
|
||
|
|
category.children.forEach(child => visitCategoryTree(child, visitor))
|
||
|
|
}
|
||
|
|
|
||
|
|
export function flattenCategoryTree(category: Category): Category[] {
|
||
|
|
const categories: Category[] = []
|
||
|
|
visitCategoryTree(category, c => categories.push(c))
|
||
|
|
return categories
|
||
|
|
}
|
||
|
|
|
||
|
|
export function categoryUnreadCount(category?: Category): number {
|
||
|
|
if (!category) return 0
|
||
|
|
|
||
|
|
return flattenCategoryTree(category)
|
||
|
|
.flatMap(c => c.feeds)
|
||
|
|
.map(f => f.unread)
|
||
|
|
.reduce((total, current) => total + current, 0)
|
||
|
|
}
|