-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add DAG format visitors implementations #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dpnova
wants to merge
10
commits into
sha1n:master
Choose a base branch
from
calctree:print-visitor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e11e9f4
feat: first cut at a little visualisation util for the dag.
dpnova 2fb38b9
feat: use a generic traversal function instead of a single use print
dpnova aec9b66
feat: include extra visitor params to help with dag context while tra…
dpnova d83be1c
feat: a print visitor
dpnova 683ee0c
Merge branch 'master' into print-visitor
dpnova eeaa26c
use new traverse state type and clean up tests
dpnova 98ff661
update names based on feedback
dpnova 703b191
Merge branch 'master' into print-visitor
dpnova 1888401
update tests to use renamed functions
dpnova 867677f
Merge branch 'print-visitor' of github.com:calctree/dagraph into prin…
dpnova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { Identifiable, DAGVisitor } from '../index'; | ||
|
|
||
| /** | ||
| * Creates a visitor that accumulates a string representation of the graph structure using indentation. | ||
| * | ||
| * @param labelFn optional function to generate a label for each node. Defaults to node.id. | ||
| * @param indent optional string to use for indentation. Defaults to 2 spaces. | ||
| * @returns a DAGVisitor that pushes lines to the context array. | ||
| */ | ||
| export function createIndentFormatter<T extends Identifiable>( | ||
| labelFn: (n: T) => string = n => n.id, | ||
| indent = ' ' | ||
| ): DAGVisitor<T, string[]> { | ||
| return (node, { depth }, lines) => { | ||
| lines.push(`${indent.repeat(depth)}${labelFn(node)}`); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a visitor that accumulates a tree-like string representation of the graph structure | ||
| * using unicode box-drawing characters (├──, └──, │). | ||
| * | ||
| * @param labelFn optional function to generate a label for each node. Defaults to node.id. | ||
| * @returns a DAGVisitor that pushes lines to the context array. | ||
| */ | ||
| export function createTreeAsciiFormatter<T extends Identifiable>( | ||
| labelFn: (n: T) => string = n => n.id | ||
| ): DAGVisitor<T, string[]> { | ||
| const isLastChild: boolean[] = []; | ||
|
|
||
| return (node, { depth, index, total }, lines) => { | ||
| const isLast = index === total - 1; | ||
| isLastChild[depth] = isLast; | ||
|
|
||
| let prefix = ''; | ||
| if (depth > 0) { | ||
| for (let i = 1; i < depth; i++) { | ||
| prefix += isLastChild[i] ? ' ' : '│ '; | ||
| } | ||
|
|
||
| const connector = isLast ? '└── ' : '├── '; | ||
| lines.push(`${prefix}${connector}${labelFn(node)}`); | ||
| } else { | ||
| lines.push(labelFn(node)); | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import createDAG, { createIndentFormatter, createTreeAsciiFormatter } from '..'; | ||
|
|
||
dpnova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| describe('visitors', () => { | ||
| describe('createPrintVisitor', () => { | ||
| test('should print graph with default indentation', () => { | ||
| const dag = createDAG(); | ||
| const a = { id: 'A' }; | ||
| const b = { id: 'B' }; | ||
| const c = { id: 'C' }; | ||
|
|
||
| // A -> B -> C | ||
| dag.addEdge(a, b); | ||
| dag.addEdge(b, c); | ||
|
|
||
| const lines: string[] = []; | ||
| dag.traverse(createIndentFormatter(), lines); | ||
|
|
||
| expect(lines).toEqual(['A', ' B', ' C']); | ||
| }); | ||
|
|
||
| test('should support custom indentation', () => { | ||
| const dag = createDAG(); | ||
| const a = { id: 'A' }; | ||
| const b = { id: 'B' }; | ||
|
|
||
| dag.addEdge(a, b); | ||
|
|
||
| const lines: string[] = []; | ||
| dag.traverse( | ||
| createIndentFormatter(n => n.id, '----'), | ||
| lines | ||
| ); | ||
|
|
||
| expect(lines).toEqual(['A', '----B']); | ||
| }); | ||
|
|
||
| test('should support custom label function', () => { | ||
| const dag = createDAG<{ id: string; val: number }>(); | ||
| const a = { id: 'A', val: 1 }; | ||
| const b = { id: 'B', val: 2 }; | ||
|
|
||
| dag.addEdge(a, b); | ||
|
|
||
| const lines: string[] = []; | ||
| dag.traverse( | ||
| createIndentFormatter(n => `Value: ${n.val}`), | ||
| lines | ||
| ); | ||
|
|
||
| expect(lines).toEqual(['Value: 1', ' Value: 2']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createTreeVisitor', () => { | ||
| test('should print graph with tree structure', () => { | ||
| const dag = createDAG(); | ||
| const a = { id: 'A' }; | ||
| const b = { id: 'B' }; | ||
| const c = { id: 'C' }; | ||
| const d = { id: 'D' }; | ||
| const e = { id: 'E' }; | ||
|
|
||
| // A -> B -> C | ||
| // A -> D -> E | ||
| dag.addEdge(a, b); | ||
| dag.addEdge(b, c); | ||
| dag.addEdge(a, d); | ||
| dag.addEdge(d, e); | ||
|
|
||
| // Roots: A | ||
| // Children of A: B, D (in that order because B added first) | ||
|
|
||
| const lines: string[] = []; | ||
| dag.traverse(createTreeAsciiFormatter(), lines); | ||
|
|
||
| const expected = ['A', '├── B', '│ └── C', '└── D', ' └── E']; | ||
|
|
||
| expect(lines).toEqual(expected); | ||
| }); | ||
|
|
||
| test('should handle multiple roots', () => { | ||
| const dag = createDAG(); | ||
| const a = { id: 'A' }; | ||
| const b = { id: 'B' }; | ||
| const c = { id: 'C' }; | ||
|
|
||
| dag.addNode(a); | ||
| dag.addNode(b); | ||
| dag.addEdge(b, c); | ||
|
|
||
| // A | ||
| // B -> C | ||
|
|
||
| const lines: string[] = []; | ||
| dag.traverse(createTreeAsciiFormatter(), lines); | ||
|
|
||
| const expected = ['A', 'B', '└── C']; | ||
|
|
||
| expect(lines).toEqual(expected); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.