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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 192x 192x 349x 529x 2x 527x 2x 525x 525x 250x 250x 335x 525x 525x 525x 525x 854x 833x 21x 293x 727x 3x 724x 453x 451x 13x 1x 130x 12x 724x 329x 395x 724x 724x 849x 724x 724x 724x 849x 724x 724x 724x 444x 153x 2x 2x 151x 151x 153x 291x 724x 293x 266x 266x 266x 266x 376x 376x 1091x | import { isEqual, pick } from 'lodash-es';
import { Category } from 'ish-core/models/category/category.model';
import { CategoryTree } from './category-tree.model';
export class CategoryTreeHelper {
/**
* Create a new empty tree with no nodes.
*/
static empty(): CategoryTree {
return {
edges: {},
nodes: {},
rootIds: [],
categoryRefs: {},
};
}
/**
* Create a new tree with a single category node.
*/
static single(category: Category): CategoryTree {
if (!category) {
throw new Error('falsy input');
}
if (!category.uniqueId) {
throw new Error('category has no uniqueId');
}
// add edges from categoryPath
const edges: { [id: string]: string[] } = {};
if (category.categoryPath && category.categoryPath.length >= 2) {
const path = category.categoryPath;
for (let i = 0; i < path.length - 1; i++) {
edges[path[i]] = [path[i + 1]];
}
}
// set category as root if it has just one element in categoryPath
const rootIds = category.categoryPath && category.categoryPath.length === 1 ? [category.uniqueId] : [];
const nodes = { [category.uniqueId]: { ...category } };
const categoryRefs = { [category.categoryRef]: category.uniqueId };
return {
edges,
nodes,
rootIds,
categoryRefs,
};
}
/**
* Select {@link Category} for update
*/
static updateStrategy(current: Category, incoming: Category): Category {
if (!current || current.completenessLevel <= incoming.completenessLevel) {
return incoming;
}
return {
...current,
// use images from incoming category with lower completeness level if the current category has no images
images: current.images?.length ? current.images : incoming.images,
};
}
/**
* Perform check for equality. Order of items is ignored.
*/
static equals(tree1: CategoryTree, tree2: CategoryTree): boolean {
return (
tree1 &&
tree2 &&
CategoryTreeHelper.rootIdsEqual(tree1.rootIds, tree2.rootIds) &&
CategoryTreeHelper.edgesEqual(tree1.edges, tree2.edges) &&
CategoryTreeHelper.categoriesEqual(tree1.nodes, tree2.nodes)
);
}
/**
* Merge two trees to a new tree.
* Updates category nodes according to updateStrategy.
*/
static merge(current: CategoryTree, incoming: CategoryTree): CategoryTree {
if (!current || !incoming) {
throw new Error('falsy input');
}
return {
edges: CategoryTreeHelper.mergeEdges(current.edges, incoming.edges),
nodes: CategoryTreeHelper.mergeNodes(current.nodes, incoming.nodes),
rootIds: CategoryTreeHelper.mergeRootIDs(current.rootIds, incoming.rootIds),
categoryRefs: CategoryTreeHelper.mergeCategoryRefs(current.categoryRefs, incoming.nodes),
};
}
/**
* Helper method for adding a single category to a tree.
*/
static add(tree: CategoryTree, category: Category): CategoryTree {
const singleCategoryTree = CategoryTreeHelper.single(category);
return CategoryTreeHelper.merge(tree, singleCategoryTree);
}
/**
* Extract a sub tree.
*/
static subTree(tree: CategoryTree, uniqueId: string): CategoryTree {
if (!uniqueId) {
return tree;
}
const select = (e: string) => e.startsWith(uniqueId);
return {
rootIds: tree.rootIds.filter(select),
edges: pick(tree.edges, ...Object.keys(tree.edges).filter(select)),
nodes: pick(tree.nodes, ...Object.keys(tree.nodes).filter(select)),
categoryRefs: {},
};
}
private static mergeRootIDs(current: string[], incoming: string[]): string[] {
// node with more available rootIDs is trustworthy
if (incoming && incoming.length > current.length) {
return CategoryTreeHelper.removeDuplicates([...incoming, ...current]);
} else {
return CategoryTreeHelper.removeDuplicates([...current, ...incoming]);
}
}
private static mergeNodes(
current: { [id: string]: Category },
incoming: { [id: string]: Category }
): { [id: string]: Category } {
const nodes = { ...current };
Object.keys(incoming).forEach(key => {
nodes[key] = { ...CategoryTreeHelper.updateStrategy(current[key], incoming[key]) };
});
return nodes;
}
private static mergeCategoryRefs(
current: { [id: string]: string },
incoming: { [id: string]: Category }
): { [id: string]: string } {
const refs = { ...current };
Object.keys(incoming).forEach(key => {
refs[incoming[key]?.categoryRef] = key;
});
return refs;
}
private static mergeEdges(
current: { [id: string]: string[] },
incoming: { [id: string]: string[] }
): { [id: string]: string[] } {
const edges = { ...current };
Object.keys(incoming).forEach(key => {
if (current[key]) {
let master: string[];
let slave: string[];
// node with more available edges is trustworthy
if (incoming[key] && incoming[key].length > current[key].length) {
master = incoming[key];
slave = current[key];
} else {
master = current[key];
slave = incoming[key];
}
// add edges from both and remove duplicates
edges[key] = CategoryTreeHelper.removeDuplicates([...master, ...slave]);
} else {
edges[key] = [...incoming[key]];
}
});
return edges;
}
private static rootIdsEqual(t1: string[], t2: string[]) {
return t1.length === t2.length && t1.every(e => t2.includes(e));
}
private static edgesEqual(t1: { [id: string]: string[] }, t2: { [id: string]: string[] }) {
return isEqual(t1, t2);
}
private static categoriesEqual(t1: { [id: string]: Category }, t2: { [id: string]: Category }) {
const keys1 = Object.keys(t1);
const keys2 = Object.keys(t2);
return (
keys1.length === keys2.length &&
keys1.every(id => keys2.includes(id)) &&
keys1.every(id => isEqual(t1[id], t2[id]))
);
}
private static removeDuplicates<T>(input: T[]): T[] {
return input.filter((value, index, array) => array.indexOf(value) === index);
}
}
|