All files / src/utils/typeGuards onyxmet.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 1/1
100% Lines 11/11

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x                                                                                     1x 1x 20x 20x 13x 12x 9x 8x 7x   20x  
/**
 * Type guard to validate if an object matches the SearchResultItem structure.
 * Checks for the presence of required properties in Onyxmet search results.
 * Required properties:
 * - label: Product name/title
 * - image: Product image URL or identifier
 * - description: Product description text
 * - href: Product URL or path
 *
 * @param product - The object to validate
 * @returns Type predicate indicating if the object is a valid SearchResultItem
 * @category Typeguards
 *
 * @example
 * ```typescript
 * // Valid search result item
 * const validItem = {
 *   label: "Sodium Chloride",
 *   image: "nacl.jpg",
 *   description: "High purity NaCl",
 *   href: "/products/nacl"
 * };
 * if (isSearchResultItem(validItem)) {
 *   console.log("Valid item:", validItem.label);
 * }
 *
 * // Invalid search result item (missing properties)
 * const invalidItem = {
 *   label: "Sodium Chloride",
 *   image: "nacl.jpg"
 *   // Missing description and href
 * };
 * if (!isSearchResultItem(invalidItem)) {
 *   console.log("Invalid item - missing required properties");
 * }
 *
 * // Invalid search result item (wrong type)
 * const wrongType = "not an object";
 * if (!isSearchResultItem(wrongType)) {
 *   console.log("Invalid item - not an object");
 * }
 * ```
 * @source
 */
export function isSearchResultItem(product: unknown): product is OnyxMetSearchResultItem {
  return (
    typeof product === 'object' &&
    product !== null &&
    'label' in product &&
    'image' in product &&
    'description' in product &&
    'href' in product
  );
}