All files / packages/content/src/core contentKey.ts

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

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        260x   4x 65x     210x   65x   87x     4x 65x      
import { hash } from "ohash";
import type { EntryQuery, Resource } from "./domain/content";
import type { VendorConfig } from "./registry";
 
const toWordChars = (part: string): string => part.replaceAll("_", "_u").replaceAll("-", "_d");
 
const queryKey = (query?: EntryQuery): string => {
    const entries = Object.entries({
        ...query,
        term: query?.term && `${query.term.resource}=${query.term.id}`,
    }).filter(([, value]) => value !== undefined);
 
    entries.sort(([a], [b]) => a.localeCompare(b));
 
    return entries.map(([key, value]) => `${key}=${encodeURIComponent(String(value))}`).join(",");
};
 
export const contentKey = (vendor: VendorConfig, resource: Resource, query?: EntryQuery): string =>
    [vendor.name, resource, hash({ ...vendor, options: undefined }), hash(queryKey(query))]
        .map(toWordChars)
        .join("_");