All files / src/api/package util.ts

42.85% Statements 9/21
25% Branches 2/8
33.33% Functions 2/6
45% Lines 9/20

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      1x 2x     1x         2x 2x 2x 2x     1x               1x                                  
import { CatalogPackage, Packages } from "./packages";
import { API_PATHS } from "../../constants/url";
 
export const getFullPackageName = (name: string, scope?: string) => {
  return scope ? `${scope}/${name}` : name;
};
 
export const getAssetsPath = (
  name: string,
  version: string,
  scope?: string
) => {
  const prefix = `${API_PATHS.PACKAGES_PREFIX}/`;
  const body = getFullPackageName(name, scope);
  const suffix = `/v${version}`;
  return `${prefix}${body}${suffix}`;
};
 
export const sanitizeVersion = (ver: string) => {
  let sanitized = ver;
  if (sanitized.startsWith("~") || sanitized.startsWith("^")) {
    sanitized = sanitized.substring(1);
  }
  return sanitized;
};
 
export const findPackage = (
  catalog: Packages,
  pkg: string
): CatalogPackage | undefined => {
  const packages = catalog.packages.filter((p) => p.name === pkg);
 
  // return the most recently published version
  if (packages.length > 1) {
    return packages.sort((p1, p2) => {
      const date1 = new Date(p1.metadata.date);
      const date2 = new Date(p2.metadata.date);
      return date2.getTime() - date1.getTime();
    })[0];
  }
 
  return packages[0];
};