Type information, constants, and utility functions related to the DOT language attributes, attribute values, and models for ts-graphviz.
🔗
It is part of the ts-graphviz library, which is split into modular packages to improve maintainability, flexibility, and ease of use.
- Type definitions for DOT language elements, including attributes and attribute values
- Constants representing common attribute names and values
- Comprehensive type guards for runtime type checking and TypeScript type narrowing
- Node reference utilities for parsing and converting complex node references
- Performance-optimized utilities for working with large model collections
- Seamless integration with @ts-graphviz/react for type-safe model filtering
- Dual-mode type handling - runtime validation or trusted user assertions
Import the necessary types, constants, or utility functions from the @ts-graphviz/common
package:
import {
NodeAttributesObject,
EdgeAttributesObject,
isNodeModel,
isEdgeModel,
toNodeRef
} from '@ts-graphviz/common';
The package excels at providing type safety for graph model operations:
const nodeAttr: NodeAttributesObject = {
label: 'Node label',
shape: 'ellipse',
};
const edgeAttr: EdgeAttributesObject = {
label: 'Edge label',
color: 'red',
};
The package provides powerful type guards for working with graph models with full TypeScript integration:
import {
isNodeModel,
isEdgeModel,
isRootGraphModel,
isSubgraphModel,
isAttributeListModel
} from '@ts-graphviz/common';
// Type-safe model checking
if (isNodeModel(someModel)) {
// TypeScript knows someModel is NodeModel
console.log(someModel.id);
}
if (isEdgeModel(someModel)) {
// TypeScript knows someModel is EdgeModel
console.log(someModel.targets);
}
Utilities for working with node references and compass directions:
import {
isNodeRef,
isNodeRefLike,
isCompass,
toNodeRef,
toNodeRefGroup
} from '@ts-graphviz/common';
// Check if a value is a valid node reference
if (isNodeRefLike('node1:port:n')) {
const nodeRef = toNodeRef('node1:port:n');
console.log(nodeRef); // { id: 'node1', port: 'port', compass: 'n' }
}
// Convert multiple node references
const targets = toNodeRefGroup(['node1', 'node2:port', 'node3::s']);
console.log(targets);
// [
// { id: 'node1' },
// { id: 'node2', port: 'port' },
// { id: 'node3', port: '', compass: 's' }
// ]
// Validate compass directions
if (isCompass('ne')) {
console.log('Valid compass direction');
}
The package provides additional utilities for complex type checking scenarios:
import {
isForwardRefNode,
isNodeRefGroupLike,
isNodeRef,
isNodeRefLike,
FilterableModel
} from '@ts-graphviz/common';
// Check for forward reference nodes
const forwardRef = { id: 'futureNode' };
if (isForwardRefNode(forwardRef)) {
console.log('Valid forward reference');
}
// Validate arrays of node references
const targets = ['node1', 'node2:port'];
if (isNodeRefGroupLike(targets)) {
const nodeRefs = toNodeRefGroup(targets);
// Process validated node references
}
For more examples and usage details, please refer to the ts-graphviz documentation.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
See CHANGELOG.md for more details.
This software is released under the MIT License, see LICENSE.