Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-gordon committed Jan 9, 2024
1 parent 353cb9d commit da22443
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
8 changes: 8 additions & 0 deletions graph-selector/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ module.exports = {
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-unused-vars": [
"warn", // or "error"
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
env: {
browser: true,
Expand Down
70 changes: 43 additions & 27 deletions graph-selector/src/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
import { Graph } from "./types";

type StringifyOptions = {
/**
* Whether to compact the output by removing newlines and spaces
*/
compact?: boolean;
};

const defaultOptions: StringifyOptions = {
compact: false,
};

/**
* Convets a graph to graph-selector DSL
*/
export function stringify(graph: Graph) {
export function stringify(graph: Graph, _options: StringifyOptions = defaultOptions) {
const lines: string[] = [];
for (const node of graph.nodes) {
const { label: _label, id: _id, classes: _classes, ..._data } = node.data;

// In compact mode, we will store where edges should be added

// Loop over nodes
for (const { data: node } of graph.nodes) {
// Escape label
const label = escapeLabel(_label);
const labelStr = escapeLabel(node.label);

// Only include ID if it's not the same as the label
let id = "";
if (_id !== _label) {
id = `#${_id}`;
let idStr = "";
if (node.id !== node.label) {
idStr = `#${node.id}`;
}

// Only include classes if there are any
let classes = "";
if (_classes)
classes = _classes
let classesStr = "";
if (node.classes)
classesStr = node.classes
.split(" ")
.map((c) => `.${c}`)
.join("");

// Only include data if there is any
const data = stringifyData(_data);
const data = stringifyData(node);

const features = [id, classes, data].filter(Boolean).join("");
const line = [label, features].filter(Boolean).join(" ");
const features = [idStr, classesStr, data].filter(Boolean).join("");
const line = [labelStr, features].filter(Boolean).join(" ");
lines.push(line);

// get all edges whose source is this node
const edges = graph.edges.filter((edge) => edge.source === _id);
for (const edge of edges) {
const edges = graph.edges.filter((edge) => edge.source === node.id);
for (const { target, data: edge } of edges) {
// get target node
const target = graph.nodes.find((node) => node.data.id === edge.target);
if (!target) continue;

const { label: edgeLabel, id: edgeId, classes: edgeClasses, ...edgeData } = edge.data;
const targetNode = graph.nodes.find((node) => node.data.id === target);
if (!targetNode) continue;

let label = escapeLabel(edgeLabel);
let label = escapeLabel(edge.label);

let id = edgeId;
if (id && id !== edgeLabel) {
id = `#${edgeId}`;
let id = edge.id;
if (id && id !== edge.label) {
id = `#${edge.id}`;
}

let classes = "";
if (edgeClasses) {
classes = edgeClasses
if (edge.classes) {
classes = edge.classes
.split(" ")
.map((c) => `.${c}`)
.join("");
}

const data = stringifyData(edgeData);
const data = stringifyData(edge);

const features = [id, classes, data].filter(Boolean).join("");
label = [features, label].filter(Boolean).join(" ");
Expand All @@ -65,7 +76,10 @@ export function stringify(graph: Graph) {
if (label) label = `${label}: `;

// link by id, if id is not the same as label, else label
const link = target.data.id !== target.data.label ? `#${target.data.id}` : target.data.label;
const link =
targetNode.data.id !== targetNode.data.label
? `#${targetNode.data.id}`
: targetNode.data.label;

// wrap link in ()
const wrappedLink = `(${link})`;
Expand All @@ -86,11 +100,13 @@ export function stringify(graph: Graph) {
export function stringifyData(data: Record<string, string | number | boolean>) {
return Object.entries(data)
.map(([key, value]) => {
if (["id", "label", "classes"].includes(key)) return "";
if (typeof value === "boolean") return `[${key}]`;
if (typeof value === "number") return `[${key}=${value}]`;
if (typeof value === "string") return `[${key}="${value.replace(/"/g, '\\"')}"]`;
throw new Error(`Invalid data type for property "${key}": ${typeof value}`);
})
.filter(Boolean)
.join("");
}

Expand Down

0 comments on commit da22443

Please sign in to comment.