Skip already processed lines (#2568)

This commit is contained in:
Erik Jan de Wit 2022-05-04 15:09:16 +02:00 committed by GitHub
parent 1c23a0f1a7
commit c2df6e7dfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 19 deletions

View file

@ -147,4 +147,44 @@ describe("ExecutionList", () => {
expect(diff).toBeInstanceOf(LevelChange); expect(diff).toBeInstanceOf(LevelChange);
expect((diff as LevelChange).parent).toBeUndefined(); expect((diff as LevelChange).parent).toBeUndefined();
}); });
it("Construct list", () => {
//given
const list = [
{ id: "0", level: 0, index: 0 },
{ id: "1", level: 1, index: 0 },
{ id: "2", level: 0, index: 1 },
{ id: "3", level: 1, index: 0 },
{ id: "4", level: 0, index: 2 },
{ id: "5", level: 1, index: 0 },
];
//when
const result = new ExecutionList(list);
//then
expect(result.expandableList).toEqual([
{
executionList: [{ id: "1", index: 0, level: 1 }],
id: "0",
index: 0,
isCollapsed: false,
level: 0,
},
{
executionList: [{ id: "3", index: 0, level: 1 }],
id: "2",
index: 1,
isCollapsed: false,
level: 0,
},
{
executionList: [{ id: "5", index: 0, level: 1 }],
id: "4",
index: 2,
isCollapsed: false,
level: 0,
},
]);
});
}); });

View file

@ -34,38 +34,40 @@ export class ExecutionList {
constructor(list: AuthenticationExecutionInfoRepresentation[]) { constructor(list: AuthenticationExecutionInfoRepresentation[]) {
this.list = list as ExpandableExecution[]; this.list = list as ExpandableExecution[];
this.expandableList =
this.transformToExpandableList(0, 0, { const exList = {
executionList: [], executionList: [],
isCollapsed: false, isCollapsed: false,
}).executionList || []; };
this.transformToExpandableList(0, exList);
this.expandableList = exList.executionList;
} }
private transformToExpandableList( private transformToExpandableList(
level: number,
currIndex: number, currIndex: number,
execution: ExpandableExecution execution: ExpandableExecution
) { ) {
for (let index = currIndex; index < this.list.length; index++) { for (let index = currIndex; index < this.list.length; index++) {
const ex = this.list[index]; const ex = this.list[index];
const level = ex.level || 0;
const nextRowLevel = this.list[index + 1]?.level || 0; const nextRowLevel = this.list[index + 1]?.level || 0;
const isLeaf = level > nextRowLevel;
const hasChild = level < nextRowLevel;
if (ex.level === level && nextRowLevel <= level) { if (isLeaf) {
execution.executionList?.push(ex); execution.executionList?.push(ex);
} else if (ex.level === level && nextRowLevel > level) { return index;
const expandable = this.transformToExpandableList(
nextRowLevel,
index + 1,
{
...ex,
executionList: [],
isCollapsed: false,
} }
);
execution.executionList?.push(expandable); if (ex.level === level && !hasChild) {
execution.executionList?.push(ex);
} else {
const subLevel = { ...ex, executionList: [], isCollapsed: false };
index = this.transformToExpandableList(index + 1, subLevel);
execution.executionList?.push(subLevel);
} }
} }
return execution; return this.list.length;
} }
order(list?: ExpandableExecution[]) { order(list?: ExpandableExecution[]) {