Skip to content

Commit

Permalink
Merge pull request #53 from tone-row/stringify-compact
Browse files Browse the repository at this point in the history
Add Stringify Compact Mode
  • Loading branch information
rob-gordon authored Jan 9, 2024
2 parents 353cb9d + 673b673 commit 2619120
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 36 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
2 changes: 1 addition & 1 deletion graph-selector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graph-selector",
"version": "0.9.12",
"version": "0.10.0",
"description": "Parse indented text (flowchart.fun syntax) into a graph",
"source": "src/graph-selector.ts",
"main": "dist/graph-selector.js",
Expand Down
4 changes: 4 additions & 0 deletions graph-selector/src/getIndentSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getIndentSize(line: string) {
const match = line.match(/^\s*/);
return match ? match[0].length : 0;
}
6 changes: 1 addition & 5 deletions graph-selector/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { matchAndRemovePointers } from "./matchAndRemovePointers";
// @ts-ignore
import { strip } from "@tone-row/strip-comments";
import { ParseError } from "./ParseError";
import { getIndentSize } from "./getIndentSize";

// TODO: these types could probably be improved to match the target types (in ./types.ts) more closely

Expand Down Expand Up @@ -399,11 +400,6 @@ export function parse(text: string): Graph {
};
}

function getIndentSize(line: string) {
const match = line.match(/^\s*/);
return match ? match[0].length : 0;
}

function findParent(indentSize: number, ancestors: Ancestors): Ancestor {
let parent: Ancestor = null;
let i = indentSize - 1;
Expand Down
179 changes: 179 additions & 0 deletions graph-selector/src/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,183 @@ Step 5 #n9
Step 7 #n10
(#n8)`);
});

it("can also return a compact version", () => {
const result = stringify(
{
edges: [
{
data: {
classes: "",
id: "",
label: "",
},
source: "n1",
target: "n2",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n2",
target: "n3",
},
{
data: {
classes: "",
id: "",
label: "label",
},
source: "n3",
target: "n4",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n3",
target: "n6",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n3",
target: "n9",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n4",
target: "n10",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n6",
target: "n7",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n7",
target: "n8",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n9",
target: "n10",
},
{
data: {
classes: "",
id: "",
label: "",
},
source: "n10",
target: "n8",
},
],
nodes: [
{
data: {
classes: "",
id: "n1",
label: "Start",
},
},
{
data: {
classes: "",
id: "n2",
label: "Step 1",
},
},
{
data: {
classes: "",
id: "n3",
label: "Step 2",
},
},
{
data: {
classes: "",
id: "n4",
label: "Step 3",
},
},
{
data: {
classes: "",
id: "n6",
label: "Step 4",
},
},
{
data: {
classes: "",
id: "n7",
label: "Step 6",
},
},
{
data: {
classes: "",
id: "n8",
label: "Finish",
},
},
{
data: {
classes: "",
id: "n9",
label: "Step 5",
},
},
{
data: {
classes: "",
id: "n10",
label: "Step 7",
},
},
],
},
{
compact: true,
},
);

expect(result).toBe(`Start #n1
Step 1 #n2
Step 2 #n3
label: Step 3 #n4
Step 7 #n10
(#n8)
Step 4 #n6
Step 6 #n7
Finish #n8
Step 5 #n9
(#n10)`);
});
});
Loading

1 comment on commit 2619120

@vercel
Copy link

@vercel vercel bot commented on 2619120 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.