export interface Graph { nodes: GraphNode[]; nextId: number; } export interface GraphNodeInput { name: string; description: string; } export interface GraphNode extends GraphNodeInput { id: number; } export interface GraphNodePayload extends GraphNode { ref: string; } export function toPayload(node: GraphNode): GraphNodePayload { return { ...node, ref: `/nodes/${node.id}`, } } export function findSingle(graph: Graph, id: number) { return graph.nodes.find(n => n.id === id); } export function append(collection: Graph, item: GraphNodeInput): GraphNode { const newItem: GraphNode = {...item, id: collection.nextId}; collection.nextId++; collection.nodes.push(newItem); return newItem; } export function remove(collection: Graph, id: number): void { const index = collection.nodes.findIndex(i => i.id === id); if(index === -1) return; collection.nodes.splice(index, 1); }