Skip to content

Commit

Permalink
GitGraph: refactored hasOverlappingCommits function
Browse files Browse the repository at this point in the history
Changed argument names from commit1 and commit2 to
commitA and commitB respectively to prevent confusion
with seq number values.

Replaced Array filter method with array some method
so that as soon as one overlap is found, function is
finished.

Used Object.entries instead of Object.keys to reduce
number of variables needed and make function easier
to read.
  • Loading branch information
guypursey committed Oct 7, 2023
1 parent c3c5c29 commit d09c459
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions packages/mermaid/src/diagrams/git/gitGraphRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,29 +338,24 @@ const drawCommits = (svg, commits, modifyGraph) => {
};

/**
* Detect if there are other commits between commit1's x-position and commit2's x-position on the
* same branch as commit2.
* Detect if there are commits
* between commitA's x-position
* and commitB's x-position on the
* same branch as commitA
*
* @param {any} commit1
* @param {any} commit2
* @param {any} commitA
* @param {any} commitB
* @param allCommits
* @returns {boolean} If there are commits between commit1's x-position and commit2's x-position
* @returns {boolean}
* If there are commits between
* commitA's x-position
* and commitB's x-position
*/
const hasOverlappingCommits = (commit1, commit2, allCommits) => {
const keys = Object.keys(allCommits);
const overlappingComits = keys.filter((key) => {
return (
// Find commits on the same branch as commit1,
// after commit1 but before commit2
// to avoid collision
allCommits[key].branch === commit1.branch &&
allCommits[key].seq > commit1.seq &&
allCommits[key].seq < commit2.seq
);
});

return overlappingComits.length > 0;
};
const hasOverlappingCommits = (commitA, commitB, allCommits) =>
Object.entries(allCommits).some(
(commitX) =>
commitX.branch === commitA.branch && commitX.seq > commitA.seq && commitX.seq < commitB.seq
);

/**
* This function find a lane in the y-axis that is not overlapping with any other lanes. This is
Expand Down

0 comments on commit d09c459

Please sign in to comment.