All files / util/bookmarks/src routeBookmarkGroups.ts

92.45% Statements 49/53
87.5% Branches 42/48
100% Functions 12/12
92.15% Lines 47/51

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 13019x                       87x 87x 57x       29x 29x   2x     27x             1x     26x 26x 26x 26x         30x   25x       1x   25x         18x 18x       67x             11x         11x 11x 11x 9x 9x   9x             1x         1x 1x 1x 1x   1x 1x           2x       2x 2x 2x 2x           6x       6x 6x 6x 5x 4x     6x 6x    
export const ROUTE_BOOKMARK_GROUPS = "ROUTE_BOOKMARK_GROUPS_V1";
 
export interface RouteBookmarkGroups {
  [sectionId: number]: number[];
}
 
interface StoredRouteBookmarkGroups {
  version: 1;
  groups: RouteBookmarkGroups;
}
 
function readStoredGroups(): RouteBookmarkGroups {
  const contents = localStorage.getItem(ROUTE_BOOKMARK_GROUPS);
  if (contents === null) {
    return {};
  }
 
  let stored: StoredRouteBookmarkGroups;
  try {
    stored = JSON.parse(contents);
  } catch {
    throw new Error("Stored route groups are malformed.");
  }
 
  if (
    !stored ||
    stored.version !== 1 ||
    !stored.groups ||
    typeof stored.groups !== "object" ||
    Array.isArray(stored.groups)
  ) {
    throw new Error("Stored route groups have an unsupported format.");
  }
 
  return Object.keys(stored.groups).reduce((groups, sectionId) => {
    const numericSectionId = Number(sectionId);
    const routeIds = stored.groups[sectionId];
    if (
      Number.isInteger(numericSectionId) &&
      numericSectionId >= 0 &&
      String(numericSectionId) === sectionId &&
      Array.isArray(routeIds) &&
      routeIds.every(routeId => Number.isInteger(routeId) && routeId > 0)
    ) {
      groups[numericSectionId] = Array.from(
        new Set(routeIds)
      );
    } else {
      throw new Error("Stored route groups contain malformed records.");
    }
    return groups;
  }, {} as RouteBookmarkGroups);
}
 
function writeStoredGroups(groups: RouteBookmarkGroups) {
  const stored: StoredRouteBookmarkGroups = { groups, version: 1 };
  localStorage.setItem(ROUTE_BOOKMARK_GROUPS, JSON.stringify(stored));
}
 
export function fetchStoredRouteBookmarkGroups(): RouteBookmarkGroups {
  return readStoredGroups();
}
 
export function addStoredRouteToBookmarkGroup(
  sectionId: number,
  routeId: number
): RouteBookmarkGroups {
  Iif (!Number.isInteger(sectionId) || sectionId < 0 ||
      !Number.isInteger(routeId) || routeId <= 0) {
    throw new Error("Valid section and route IDs are required.");
  }
 
  const groups = readStoredGroups();
  const routeIds = groups[sectionId] || [];
  if (!routeIds.includes(routeId)) {
    groups[sectionId] = [...routeIds, routeId];
    writeStoredGroups(groups);
  }
  return groups;
}
 
export function removeStoredRouteFromBookmarkGroup(
  sectionId: number,
  routeId: number
): RouteBookmarkGroups {
  Iif (!Number.isInteger(sectionId) || sectionId < 0 ||
      !Number.isInteger(routeId) || routeId <= 0) {
    throw new Error("Valid section and route IDs are required.");
  }
 
  const groups = readStoredGroups();
  groups[sectionId] = (groups[sectionId] || []).filter(id => id !== routeId);
  Eif (groups[sectionId].length === 0) {
    delete groups[sectionId];
  }
  writeStoredGroups(groups);
  return groups;
}
 
export function removeStoredRouteBookmarkGroup(
  sectionId: number
): RouteBookmarkGroups {
  Iif (!Number.isInteger(sectionId) || sectionId < 0) {
    throw new Error("A valid section ID is required.");
  }
 
  const groups = readStoredGroups();
  delete groups[sectionId];
  writeStoredGroups(groups);
  return groups;
}
 
export function removeStoredRouteFromAllBookmarkGroups(
  routeId: number
): RouteBookmarkGroups {
  Iif (!Number.isInteger(routeId) || routeId <= 0) {
    throw new Error("A valid route ID is required.");
  }
 
  const groups = readStoredGroups();
  Object.keys(groups).forEach(sectionId => {
    groups[sectionId] = groups[sectionId].filter(id => id !== routeId);
    if (groups[sectionId].length === 0) {
      delete groups[sectionId];
    }
  });
  writeStoredGroups(groups);
  return groups;
}