Skip to content

Commit ed6bac4

Browse files
committed
style: use tree builder class for managing state
1 parent 27ba18b commit ed6bac4

2 files changed

Lines changed: 44 additions & 51 deletions

File tree

src/commands/content-item/__mocks__/dependant-content-helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ContentDependancy } from '../../../common/content-item/content-dependan
33
function dependancy(id: string): ContentDependancy {
44
return {
55
_meta: {
6-
schema: 'http://bigcontent.io/cms/schema/v1/core#/definitions/content-link'
6+
schema: 'http://bigcontent.io/cms/schema/v1/core#/definitions/content-link',
7+
name: 'content-link'
78
},
89
contentType: 'https://dev-solutions.s3.amazonaws.com/DynamicContentTypes/Accelerators/blog.json',
910
id: id

src/commands/content-item/tree.ts

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ export const traverseRecursive = async (path: string, action: (path: string) =>
4848
}
4949
};
5050

51-
export const prepareContentForTree = async (
52-
repo: { basePath: string; repo: ContentRepository }
53-
): Promise<ContentDependancyTree> => {
51+
export const prepareContentForTree = async (repo: {
52+
basePath: string;
53+
repo: ContentRepository;
54+
}): Promise<ContentDependancyTree> => {
5455
const contentItems: RepositoryContentItem[] = [];
5556
const schemaNames = new Set<string>();
5657

@@ -92,51 +93,42 @@ const fstPipes = ['├', '├', '└'];
9293
const circularPipes = ['╗', '║', '╝'];
9394
const circularLine = '═';
9495

95-
export const addDependency = (
96-
item: ItemContentDependancies,
97-
evaluated: Set<ItemContentDependancies>,
98-
lines: string[],
99-
circularLinks: CircularLink[],
100-
evalThis: ParentReference[],
101-
fst: number,
102-
prefix: string
103-
): boolean => {
104-
const depth = evalThis.length - 1;
105-
const pipe = depth < 0 ? '' : fstPipes[fst] + '─ ';
106-
107-
const circularMatch = evalThis.find(parent => parent.item == item);
108-
if (circularMatch) {
109-
lines.push(`${prefix}${pipe}*** (${item.owner.content.label})`);
110-
circularLinks.push([circularMatch.line, lines.length - 1]);
111-
return false;
112-
} else if (evaluated.has(item)) {
113-
if (depth > -1) {
114-
lines.push(`${prefix}${pipe}(${item.owner.content.label})`);
96+
export class TreeBuilder {
97+
lines: string[] = [];
98+
circularLinks: CircularLink[] = [];
99+
100+
constructor(public evaluated: Set<ItemContentDependancies>) {}
101+
102+
addDependency(item: ItemContentDependancies, evalThis: ParentReference[], fst: number, prefix: string): boolean {
103+
const depth = evalThis.length - 1;
104+
const pipe = depth < 0 ? '' : fstPipes[fst] + '─ ';
105+
106+
const circularMatch = evalThis.find(parent => parent.item == item);
107+
if (circularMatch) {
108+
this.lines.push(`${prefix}${pipe}*** (${item.owner.content.label})`);
109+
this.circularLinks.push([circularMatch.line, this.lines.length - 1]);
110+
return false;
111+
} else if (this.evaluated.has(item)) {
112+
if (depth > -1) {
113+
this.lines.push(`${prefix}${pipe}(${item.owner.content.label})`);
114+
}
115+
return false;
116+
} else {
117+
this.lines.push(`${prefix}${pipe}${item.owner.content.label}`);
115118
}
116-
return false;
117-
} else {
118-
lines.push(`${prefix}${pipe}${item.owner.content.label}`);
119-
}
120119

121-
evalThis.push({ item, line: lines.length - 1 });
122-
evaluated.add(item);
123-
124-
const filteredItems = item.dependancies.filter(dep => dep.resolved);
125-
filteredItems.forEach((dep, index) => {
126-
const subFst = firstSecondThird(index, filteredItems.length);
127-
const subPrefix = depth == -1 ? '' : fst === 2 ? ' ' : '│ ';
128-
addDependency(
129-
dep.resolved as ItemContentDependancies,
130-
evaluated,
131-
lines,
132-
circularLinks,
133-
[...evalThis],
134-
subFst,
135-
prefix + subPrefix
136-
);
137-
});
138-
return true;
139-
};
120+
evalThis.push({ item, line: this.lines.length - 1 });
121+
this.evaluated.add(item);
122+
123+
const filteredItems = item.dependancies.filter(dep => dep.resolved);
124+
filteredItems.forEach((dep, index) => {
125+
const subFst = firstSecondThird(index, filteredItems.length);
126+
const subPrefix = depth == -1 ? '' : fst === 2 ? ' ' : '│ ';
127+
this.addDependency(dep.resolved as ItemContentDependancies, [...evalThis], subFst, prefix + subPrefix);
128+
});
129+
return true;
130+
}
131+
}
140132

141133
export const fillWhitespace = (original: string, current: string, char: string, targetLength: number): string => {
142134
let position = original.length;
@@ -160,14 +152,14 @@ export const fillWhitespace = (original: string, current: string, char: string,
160152
};
161153

162154
export const printTree = (item: ItemContentDependancies, evaluated: Set<ItemContentDependancies>): boolean => {
163-
let lines: string[] = [];
164-
const circularLinks: CircularLink[] = [];
155+
const builder = new TreeBuilder(evaluated);
165156

166-
const result = addDependency(item, evaluated, lines, circularLinks, [], 0, '');
157+
const result = builder.addDependency(item, [], 0, '');
167158

168159
if (!result) return false;
169160

170-
lines = lines.map(line => line + ' ');
161+
const circularLinks = builder.circularLinks;
162+
const lines = builder.lines.map(line => line + ' ');
171163
const modifiedLines = [...lines];
172164

173165
// Render circular references.

0 commit comments

Comments
 (0)