All files / apps/huella-legal/layers/articles/server/mappers articleSearch.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 5/5
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26              2x   2x 10x 7x   5x     7x                  
import type { Entry, Page } from "@joinery/content";
import type { ArticleSearch } from "../../shared/types/ArticleSearch";
import type { ArticleSummary } from "../../shared/types/ArticleSummary";
import type { Category } from "../../shared/types/Category";
import { toArticleSummary } from "./articleSummary";
import { toFacets } from "./facet";
 
const SUGGESTED_SUBJECTS = 4;
 
export const toSuggestedSubjects = (index: ArticleSummary[]): Category[] =>
    toFacets(index.flatMap(({ category }) => category ?? []))
        .sort((a, b) => b.count - a.count)
        .slice(0, SUGGESTED_SUBJECTS)
        .map(({ value }) => value);
 
// WordPress reports zero pages for zero matches, which would put the first page past the end
export const toArticleSearch = (query: string, page: Page<Entry>): ArticleSearch => ({
    items: page.items.map(toArticleSummary),
    page: page.page,
    perPage: page.perPage,
    total: page.total,
    totalPages: Math.max(1, page.totalPages),
    query,
    subjects: [],
});