refactor(core): remove primtive typeguards

This commit is contained in:
braks
2023-10-30 13:36:07 +01:00
committed by Braks
parent 785f147236
commit 3b175a553e
10 changed files with 35 additions and 54 deletions
+10 -10
View File
@@ -1,5 +1,5 @@
import { markRaw } from 'vue'
import { isDef, isString, warn } from '.'
import { isDef, warn } from '.'
import type {
Actions,
Box,
@@ -145,7 +145,7 @@ export function parseEdge(edge: Edge, defaults: Partial<GraphEdge> = {}): GraphE
focusable: edge.focusable ?? defaults.focusable,
data,
events: markRaw(events),
label: (edge.label && !isString(edge.label) ? markRaw(edge.label) : edge.label) || defaults.label,
label: (edge.label && typeof edge.label !== 'string' ? markRaw(edge.label) : edge.label) || defaults.label,
interactionWidth: edge.interactionWidth || defaults.interactionWidth,
} as GraphEdge)
: defaults
@@ -159,7 +159,7 @@ function getConnectedElements<T extends Node = Node>(
edges: Edge[],
dir: 'source' | 'target',
): T[] {
const id = isString(nodeOrId) ? nodeOrId : nodeOrId.id
const id = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const connectedIds = new Set()
@@ -186,9 +186,9 @@ export function getOutgoers(...args: any[]) {
}
const [nodeOrId, elements] = args
const node: Node = isString(nodeOrId) ? { id: nodeOrId } : nodeOrId
const nodeId = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const outgoers = elements.filter((el: Element) => isEdge(el) && el.source === node.id)
const outgoers = elements.filter((el: Element) => isEdge(el) && el.source === nodeId)
return outgoers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.target))
}
@@ -205,9 +205,9 @@ export function getIncomers(...args: any[]) {
}
const [nodeOrId, elements] = args
const node: Node = isString(nodeOrId) ? { id: nodeOrId } : nodeOrId
const nodeId = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const incomers = elements.filter((el: Element) => isEdge(el) && el.target === node.id)
const incomers = elements.filter((el: Element) => isEdge(el) && el.target === nodeId)
return incomers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.source))
}
@@ -408,7 +408,7 @@ export function getNodesInside(
export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, edges: E[]) {
const nodeIds = new Set()
if (isString(nodesOrId)) {
if (typeof nodesOrId === 'string') {
nodeIds.add(nodesOrId)
} else if (nodesOrId.length >= 1) {
nodesOrId.forEach((n) => nodeIds.add(n.id))
@@ -420,7 +420,7 @@ export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, ed
export function getConnectedNodes<N extends Node | { id: string } | string>(nodes: N[], edges: Edge[]) {
const nodeIds = new Set()
nodes.forEach((node) => nodeIds.add(isString(node) ? node : node.id))
nodes.forEach((node) => nodeIds.add(typeof node === 'string' ? node : node.id))
const connectedNodeIds = edges.reduce((acc, edge) => {
if (nodeIds.has(edge.source)) {
@@ -434,7 +434,7 @@ export function getConnectedNodes<N extends Node | { id: string } | string>(node
return acc
}, new Set())
return nodes.filter((node) => connectedNodeIds.has(isString(node) ? node : node.id))
return nodes.filter((node) => connectedNodeIds.has(typeof node === 'string' ? node : node.id))
}
export function getTransformForBounds(