Skip to content

Commit

Permalink
feat(rstream-dot): add serialize()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename walk() => traverse()

- add serialize() function
- rename WalkState => TraversalState
- update default rank direction from TB => LR
  • Loading branch information
postspectacular committed Apr 4, 2023
1 parent 0f1b922 commit f3bb5ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/rstream-dot/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface Node {
body?: string;
}

export interface WalkState {
export interface TraversalState {
subs: Map<ISubscribable<any>, Node>;
rels: Node[][];
id: number;
Expand Down
24 changes: 18 additions & 6 deletions packages/rstream-dot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Stream } from "@thi.ng/rstream/stream";
import { StreamSync } from "@thi.ng/rstream/sync";
import { truncate } from "@thi.ng/strings/truncate";
import { map } from "@thi.ng/transducers/map";
import type { DotOpts, Node, NodeType, WalkState } from "./api.js";
import type { DotOpts, Node, NodeType, TraversalState } from "./api.js";

export * from "./api.js";

Expand Down Expand Up @@ -46,10 +46,10 @@ const subValue = (sub: ISubscribable<any>) => {
return res ? truncate(64, "...")(res) : res;
};

export const walk = (
export const traverse = (
subs: ISubscribable<any>[],
opts?: Partial<DotOpts>,
state?: WalkState
state?: TraversalState
) => {
opts || (opts = {});
state || (state = { id: 0, subs: new Map(), rels: [] });
Expand All @@ -67,7 +67,7 @@ export const walk = (
state.id++;
const children = getChildren(sub);
if (children.length) {
walk(children, opts, state);
traverse(children, opts, state);
for (let c of children) {
const childNode = state.subs.get(c);
childNode && state.rels.push([desc, childNode]);
Expand All @@ -77,9 +77,9 @@ export const walk = (
return state;
};

export const toDot = (state: WalkState, opts?: Partial<DotOpts>) => {
export const toDot = (state: TraversalState, opts?: Partial<DotOpts>) => {
opts = {
dir: "TB",
dir: "LR",
font: "sans-serif",
fontsize: 10,
text: "white",
Expand All @@ -103,3 +103,15 @@ export const toDot = (state: WalkState, opts?: Partial<DotOpts>) => {
"}",
].join("\n");
};

/**
* Syntax sugar for the composition {@link traverse} and {@link toDot},
* serializing the traversable graph topology to Graphviz DOT format.
*
* @param subs
* @param opts
*/
export const serialize = (
subs: ISubscribable<any>[],
opts?: Partial<DotOpts>
) => toDot(traverse(subs, opts), opts);

0 comments on commit f3bb5ab

Please sign in to comment.