All files / src/util package.ts

100% Statements 26/26
100% Branches 16/16
100% Functions 9/9
100% Lines 24/24

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                1x 2x   1x   4x 1x     3x                 1x     10x   23x   13x                 1x 10x   6x 37x 12x                     1x             6x 6x   6x       13x 13x 11x 11x       6x    
import { PackageHighlight, PackageTagConfig } from "../api/config";
import { KEYWORD_IGNORE_LIST } from "../constants/keywords";
 
export interface TagObject extends PackageTagConfig {}
 
/**
 * Reduces package tags to only return highlight tags
 */
export const highlightsFrom = (packageTags?: PackageTagConfig[]) => {
  if (!packageTags || packageTags.length < 1) return [];
 
  return packageTags.reduce(
    (accum: PackageHighlight[], tag: PackageTagConfig): PackageHighlight[] => {
      if (tag.highlight) {
        return [...accum, tag.highlight];
      }
 
      return accum;
    },
    []
  );
};
 
/**
 * Maps packageTags to an array of TagObjects, which can be rendered by the PackageTags Component
 */
export const mapPackageTags = (
  packageTags?: PackageTagConfig[]
): TagObject[] => {
  return (packageTags ?? [])
    .filter((tag) => {
      return Boolean(tag.keyword?.label);
    })
    .map((tag) => ({
      ...tag,
      id: tag.keyword?.label!,
    }));
};
 
/**
 * Maps keywords to an array of TagObjects, which can be rendered by the PackageTags component
 */
export const mapPackageKeywords = (keywords?: string[]): TagObject[] => {
  if (!keywords || keywords.length < 1) return [];
 
  return keywords
    .filter((label) => Boolean(label) && !KEYWORD_IGNORE_LIST.has(label))
    .map((label) => ({
      id: label,
      keyword: {
        label,
      },
    }));
};
 
/**
 * Maps packageTags and keywords to a list of TagObjects, using mapPackageTags and mapPackageKeywords
 */
export const tagObjectsFrom = ({
  packageTags,
  keywords,
}: {
  packageTags?: PackageTagConfig[];
  keywords?: string[];
}): TagObject[] => {
  const tagObjects = new Array<TagObject>();
  const tagLabels = new Set<string>();
 
  for (const tag of [
    ...mapPackageTags(packageTags),
    ...mapPackageKeywords(keywords),
  ]) {
    const label = tag.keyword!.label.toLowerCase();
    if (!tagLabels.has(label)) {
      tagObjects.push(tag);
      tagLabels.add(label);
    }
  }
 
  return tagObjects;
};