refactor(core): remove primtive typeguards
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { nextTick } from 'vue'
|
||||
import { isDef, isFunction, isGraphNode, isString } from '.'
|
||||
import { isDef, isGraphNode } from '.'
|
||||
import type {
|
||||
EdgeAddChange,
|
||||
EdgeChange,
|
||||
@@ -26,7 +26,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) {
|
||||
let parentStyles: Styles = {}
|
||||
|
||||
if (isFunction(parent.style)) {
|
||||
if (typeof parent.style === 'function') {
|
||||
parentStyles = { ...parent.style(parent) }
|
||||
} else if (parent.style) {
|
||||
parentStyles = { ...parent.style }
|
||||
@@ -36,7 +36,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
parentStyles.height = parentStyles.height ?? `${parent.dimensions.height}px`
|
||||
|
||||
if (extendWidth > 0) {
|
||||
if (isString(parentStyles.width)) {
|
||||
if (typeof parentStyles.width === 'string') {
|
||||
const currWidth = Number(parentStyles.width.replace('px', ''))
|
||||
parentStyles.width = `${currWidth + extendWidth}px`
|
||||
} else {
|
||||
@@ -45,7 +45,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
}
|
||||
|
||||
if (extendHeight > 0) {
|
||||
if (isString(parentStyles.height)) {
|
||||
if (typeof parentStyles.height === 'string') {
|
||||
const currWidth = Number(parentStyles.height.replace('px', ''))
|
||||
parentStyles.height = `${currWidth + extendHeight}px`
|
||||
} else {
|
||||
@@ -57,7 +57,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
const xDiff = Math.abs(updateItem.position.x)
|
||||
parent.position.x = parent.position.x - xDiff
|
||||
|
||||
if (isString(parentStyles.width)) {
|
||||
if (typeof parentStyles.width === 'string') {
|
||||
const currWidth = Number(parentStyles.width.replace('px', ''))
|
||||
parentStyles.width = `${currWidth + xDiff}px`
|
||||
} else {
|
||||
@@ -71,7 +71,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
const yDiff = Math.abs(updateItem.position.y)
|
||||
parent.position.y = parent.position.y - yDiff
|
||||
|
||||
if (isString(parentStyles.height)) {
|
||||
if (typeof parentStyles.height === 'string') {
|
||||
const currWidth = Number(parentStyles.height.replace('px', ''))
|
||||
parentStyles.height = `${currWidth + yDiff}px`
|
||||
} else {
|
||||
@@ -84,7 +84,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
||||
parent.dimensions.width = Number(parentStyles.width.toString().replace('px', ''))
|
||||
parent.dimensions.height = Number(parentStyles.height.toString().replace('px', ''))
|
||||
|
||||
if (isFunction(parent.style)) {
|
||||
if (typeof parent.style === 'function') {
|
||||
parent.style = (p) => {
|
||||
const styleFunc = parent.style as StyleFunc
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { markRaw } from 'vue'
|
||||
import { ErrorCode, VueFlowError, clampPosition, isNumber, isParentSelected } from '.'
|
||||
import { ErrorCode, VueFlowError, clampPosition, isParentSelected } from '.'
|
||||
import type {
|
||||
Actions,
|
||||
CoordinateExtent,
|
||||
@@ -108,10 +108,10 @@ function getParentExtent(
|
||||
|
||||
if (
|
||||
parent &&
|
||||
isNumber(parent.computedPosition.x) &&
|
||||
isNumber(parent.computedPosition.y) &&
|
||||
isNumber(parent.dimensions.width) &&
|
||||
isNumber(parent.dimensions.height)
|
||||
typeof parent.computedPosition.x !== 'undefined' &&
|
||||
typeof parent.computedPosition.y !== 'undefined' &&
|
||||
typeof parent.dimensions.width !== 'undefined' &&
|
||||
typeof parent.dimensions.height !== 'undefined'
|
||||
) {
|
||||
return [
|
||||
[parent.computedPosition.x + left, parent.computedPosition.y + top],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { isNumber, rectToBox } from '.'
|
||||
import { rectToBox } from '.'
|
||||
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, Rect, ViewportTransform, XYPosition } from '~/types'
|
||||
import { Position } from '~/types'
|
||||
|
||||
@@ -132,7 +132,7 @@ export function isEdgeVisible({
|
||||
}
|
||||
|
||||
export function getEdgeZIndex(edge: GraphEdge, findNode: Actions['findNode'], elevateEdgesOnSelect = false) {
|
||||
const hasZIndex = isNumber(edge.zIndex)
|
||||
const hasZIndex = typeof edge.zIndex === 'number'
|
||||
let z = hasZIndex ? edge.zIndex! : 0
|
||||
|
||||
const source = findNode(edge.source)
|
||||
|
||||
@@ -13,20 +13,4 @@ export function getEventPosition(event: MouseEvent | TouchEvent, bounds?: DOMRec
|
||||
}
|
||||
}
|
||||
|
||||
export function isString(val: any): val is string {
|
||||
return typeof val === 'string'
|
||||
}
|
||||
|
||||
export function isFunction(val: any): val is Function {
|
||||
return typeof val === 'function'
|
||||
}
|
||||
|
||||
export function isBoolean(val: any): val is boolean {
|
||||
return typeof val === 'boolean'
|
||||
}
|
||||
|
||||
export function isNumber(val: any): val is number {
|
||||
return typeof val === 'number'
|
||||
}
|
||||
|
||||
export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user