All files / app/util graph.ts

32% Statements 8/25
0% Branches 0/15
26.08% Functions 6/23
57.14% Lines 8/14

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 61158x   158x                                 4x                       1x                     1x       1x       1x       1x            
import { filter, find, uniq } from 'lodash-es';
import { RefNode } from '../model/ref';
import { hasTag } from './tag';
 
export type GraphNode = RefNode & {
  unloaded?: boolean,
  notFound?: boolean,
  pinned?: boolean,
  x?: number,
  y?: number,
  fx?: number,
  fy?: number,
};
export type GraphLink = {
  source: string | GraphNode,
  target: string | GraphNode,
};
 
export function graphable(...nodes: GraphNode[]) {
  return nodes.filter(n => isGraphable(n));
}
 
export function isGraphable(node: GraphNode) {
  return node.published && !isInternal(node);
}
 
export function isInternal(node: GraphNode) {
  return hasTag('internal', node) && !hasTag('plugin/thread', node) && !hasTag('plugin/comment', node);
}
 
export function links(allNodes: GraphNode[], ...nodes: GraphNode[]) {
  return [
    ...nodes.flatMap(n => sources(n).filter(url => findNode(allNodes, url)).map(url => ({ source: url, target: n.url })) || []),
    ...nodes.flatMap(n => responses(n).filter(url => findNode(allNodes, url)).map(url => ({ source: n.url, target: url })) || []),
  ];
}
 
export function linkSources(allNodes: GraphNode[], url: string) {
  return filter(allNodes, n => !!n.sources?.includes(url)).map((n: GraphNode) => ({ source: url, target: n.url }));
}
 
export function references(...nodes: GraphNode[]): string[] {
  return uniq([...sources(...nodes), ...responses(...nodes)]);
}
 
export function sources(...nodes: GraphNode[]): string[] {
  return nodes.flatMap(r => r.sources || []);
}
 
export function responses(...nodes: GraphNode[]): string[] {
  return nodes.flatMap(r => r.responses || []);
}
 
export function unloadedReferences(allNodes: GraphNode[], ...nodes: GraphNode[]): string[] {
  return references(...graphable(...nodes)).filter(s => !findNode(allNodes, s));
}
 
export function findNode(nodes: GraphNode[], url: string) {
  return find(nodes, r => r.url === url);
}